Image resizing ninjitsu

by Jesse 30. September 2008 13:10

Updated 4/22/09
Alain contacted me and stated that there's a possible flaw with leaving the bmp object hanging out there.  The file uploaded is never released after creation from the code below.  Thinking about this, it makes sense, so I added a bmp.Dispose() per his recommendation.

System.Drawing.Images has this cool method, GetThumbnailImage which sounds promising on paper until you use it -- then its a disappointment.  It takes a straight int value, which is whatever you come up with, plug it in and bam, you got an image ...that doesn't follow aspect ratio or anything that you might consider useful.  Well, I think there should be an overload on this method, one that DOES keep that aspect ratio, so I created one.  I'm sure others have, but oh well, I like mine.

First off, I came up with a number that was acceptable to handle an image that was big enough to even bother with -- 320x240 is TV resolution, so I thought that was a good start.  I also came up with a number of 100x100 for a good thumbnail size, but those are treated as maximum values, so no resulting value will exceed 100 in either width or height.  I determined finding a percentage was the best way to get this value right, everytime, regardless of how large the image is.  If you take the largest value, divide it out to get a percentage, there's no possible way the other value will exceed 100.  The formula, if you will, ends up being percent = (Desired Size) / (Largest Value) -- I'm off to start setting this up.  Also, other code will determine that the origional file meets our needs -- its larger than any thumbnail we will make and write up some code to do this...

System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(upload1.FileContent);
int width, height;
if (bmp.Height > 320 || bmp.Width > 240)
{ }
else
{
    width = bmp.Width;
    height = bmp.Height;
}

Remember again our thumbnail's desired size is 100x100 max, so technically we could say bmp.Height > 100 and bmp.width > 100 but for practicality purposes, 320x240 is good enough.  Now we need a bit of code to determine percentage.  Note that int will NOT fit the bill on this, so I picked float.  It's precise enough for what we want to accomplish, and leads me to the following method.

private float DeterminePercentageForResize(int height, int width)
{
     int highestValue;
     if (height > width)
         
highestValue = height;
    
else
         
highestValue = width;

     float percent = 100 / (float)highestValue;

     if (percent > 1 && percent != 0)
          
throw new Exception("Percent cannot be greater than 1 or equal to zero");
    
else 
          return percent;
}

the line that says float percent = 100 / (float)highestValue; is where our 100x100 comes in.  You could pull that out into a constant and change it to whatever you want, as long as it is NOT larger than the original.  We also throw an exception that our percentage cannot be 1 (otherwise, whats the point?) and 0 (multiply by 0 == 0, also meaningless and will probably make me angry).  I now have a float value which is actually desireable, because we need to multiply our percentage by the width/height of the original image ... 

{
float percent = DeterminePercentageForResize(bmp.Height, bmp.Width);

float floatWidth = (float)bmp.Width * percent;
float floatHeight = (float)bmp.Height * percent;

width = Convert.ToInt32(floatWidth);
height = Convert.ToInt32(floatHeight);
}

now we pass over our newly created integers into width/height as such ...

System.Drawing.Image thumb = bmp.GetThumbnailImage(width, height, thumbcall, IntPtr.Zero);
thumb.Save(FileLocation + upload1.FileName);

final code looks something like this... (reference reasons, "thumbnailcallback" comes from MSDN from one of their examples)

private void MakeMeAGoodThumbnail()
{

System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(upload1.FileContent);
int width, height;
if (bmp.Height > 320 || bmp.Width > 240)
{
     float percent = DeterminePercentageForResize(bmp.Height, bmp.Width);

     float floatWidth = (float)bmp.Width * percent; 
     float floatHeight = (float)bmp.Height * percent;

     width = Convert.ToInt32(floatWidth);
     height = Convert.ToInt32(floatHeight);
}

else
{
    width = bmp.Width;
    height = bmp.Height;
}

System.Drawing.Image thumb = bmp.GetThumbnailImage(width, height, ThumbnailCallback(), IntPtr.Zero);
thumb.Save(FileLocation + upload1.FileName);

//updated 4/22/2009 - from Alain's input, see update above.
bmp.Dispose();

}

private float DeterminePercentageForResize(int height, int width)
{
     int highestValue;
     if (height > width)
         
highestValue = height;
    
else
         
highestValue = width;

     float percent = 100 / (float)highestValue;

     if (percent > 1 && percent != 0)
          
throw new Exception("Percent cannot be greater than 1 or equal to zero");
    
else 
          return percent;
}

public bool ThumbnailCallback()
{
     return false;
}

Currently rated 4.2 by 5 people

  • Currently 4.2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.Net

Doing social networking right ...and wrong

by Jesse 25. September 2008 05:46

Recently there was a post out on a blog I read on technet asking "to social network or not" and I'd like to take this a step further.  There's no manual on -how-, much less a -right/wrong- way of social networking, but consider this a loose guideline.  Why?  I'm seeing two groups, right now, using social networking.  One is doing it right, the other, very wrong.  Just my opinion, but hear me out. More...

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Wind storm 2008

by Jesse 15. September 2008 10:22

The great thing about Ohio is we don't get all the "extreme" stuff everyone else gets.  Hurricanes, floods, earthquakes, volcanoes, tornadoes, etc we don't get all that much.  Last night, we got some stupid high winds, so much it took down a lot of trees and totally screwed us in the power department.  The message @ AEP (my power company) said that it might be a week before power is restored...and I live near downtown!  We haven't had power since Sunday around 3pm.  Other bonus, one of the big trees finally let go directly across the street, putting a huge stress on the poles in the area.  Look for the big fork in the middle of the picture.

What's better, the guy's house it fell on -- JUST left for vacation last night.  One of the guys has already called him and there's not much he can do.  So, as a funny, we're going to send him this as a christmas card "from your !@#hole neighbors, Merry Christmas!" with a small caption of "you thought the wind took that tree down?"

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

off topic

I haven't forgotten

by Jesse 12. September 2008 04:38

I thought long and hard about posting this up, mostly because it's deeply personal.  This morning I decided I would.  If you don't care, consider this your "not a tech post" warning.

9/11 did affect me, more so than others.  Years ago, while working at an oil change place and talked to an army specialist about what he did and "what it means to be American".  We talked about how my generation didn't have a huge ordeal like previous generations had and I made a comment that haunts me, even today, "I think it'll takes something big, HUGE to get my generation to wake up".  I hate the fact I was right.  I came across a site asking if the rememberance of 9/11 was forgotten by many and people have returned to their daily lives, to some capacity I agree, but the the comment I left, I realized that it wasn't a reply but more a poem of sorts, and I'm no poet. 

I haven't forgotten...
I haven't forgotten how people ignore truth ...even when its right in front of them, plain as day.
I haven't forgotten how some think life is a commodity.
I haven't forgotten how people believe a plane can't atomize when striking a well-built structure.
I haven't forgotten how my military friends WANT to go defend our freedom.
I haven't forgotten how some of them are not coming back.
I haven't forgotten how unrealistic people have become about our rights.
I haven't forgotten how to stand up for what is right.
I haven't forgotten how I felt 7 years ago.

and most importantly...
I haven't forgotten I'm still not afraid.  I am a citizen of the United States, not a subject to the world.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Government | off topic

Google's Chrome, no thanks

by Jesse 3. September 2008 09:10

Not that anyone asked for another browser but Google has taken it upon itself to create its own.  Let me just cut the crap and get right to it.  I'm going to look at this from a networking standpoint first, because well, that's where you end up no matter how pretty something looks.

Here's some normal IE8 beta 2 traffic

GET / HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/x-silverlight-2-b2, application/x-silverlight, */*
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; InfoPath.2; .NET CLR 3.5.30729; .NET CLR 3.0.30618; MS-RTC LM 8)
UA-CPU: x86
Accept-Encoding: gzip, deflate
Host:
www.cnn.com
Connection: Keep-Alive

here's some normal firefox 3 traffic

GET / HTTP/1.1
Host:
www.cnn.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie : <removed>

so far, so good, so what?  BEFORE you even hit that go button in Chrome, here's what's going on that you don't see as you're typing (in my case, www.cnn.com)

GET /complete/search?client=chrome&output=chrome&hl=en-US&q=www HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13
Cookie: <removed>
Accept-Language: en-US,en
Accept-Charset: ISO-8859-1,*,utf-8
Accept-Encoding: gzip,deflate,bzip2
Host: clients1.google.com
Connection: Keep-Alive

I'm sorry?  clients1.google.com?  Navigate to it and surprise, it's just a simple run of the mill google search page.  After this magic traffic to client1.google.com, I send out my request to cnn and I'm surprised again by what type its built on.  Wait for it...

GET / HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Cookie: <removed>
Accept-Language: en-US,en
Accept-Charset: ISO-8859-1,*,utf-8
Accept-Encoding: gzip,deflate,bzip2
Host:
www.cnn.com
Connection: Keep-Alive

How interesting, Apple has said they've got a big announcement later this week.  Hmmmmm.  Are we going to see a Goople phone?  Anyway, moving on to the rest of the application, I do happen to like a couple developer features they built into it, such as debug javascript, javascript console and task manager (even got a "geek stats" button in that task manager thats really cool). 

I don't see myself using this browser much really.  Yea it's another option, yea its from google, but ...so what?

Currently rated 3.5 by 11 people

  • Currently 3.545455/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

Apple

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Like the description says, at my core, I'm a scientist and engineer.  I came from humble beginnings on a 486DX2 Packard Hell playing doom2 on IPX to in a small time retail shop and got into hardware (ISO layers FTW!) and it was all downhill from there.  I'm infinitely curious about almost everything and always wanting to know.

According to personality tests (real ones) I classify under "Rational" more specifically, a Fieldmarshal.  I think there's something to that.

Some of the stuff I'm currently into/researching...

Sitefinity

Ninject

Subsonic 

Currently working on ...
i did the hundred


and some extra stuff

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's, their brother, their dog, cat, ferret nor gold fish's view in anyway.  At all.  Ever.

© Copyright 2007-2009

Month List