[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 720
  • Last Modified:

Using c# to resize an image and reduce file size

I have a .Net 4 Windows Forms project created with Visual Studio 2010 using c#.

I need to do allow a user to browse to an image (JPG) and when selected the program will modify it in three ways:

1) It will resize the dimensions of the image to a width of 450 and the height will be the correct setting to maintain the aspect ratio.  
2) The quality of the image will be modified to allow for the image to fall within 30kb.  
3) A thumbnail of the image will be created and saved to seperate file.

0
canuckconsulting
Asked:
canuckconsulting
  • 3
  • 2
1 Solution
 
Visio_GuyCommented:
This article shows a pretty straightforward way to do this:

C# Thumbnail Image With GetThumbnailImage
http://www.dotnetperls.com/getthumbnailimage

It does a bit of math to make the image fall within a certain maximum pixel dimension, then uses Image.GetThumbnailImage (which is built into .NET 4) to do the work. See: http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx

I don't know if it is possible or easy to dial-in the maximum file size for your image, but I imagine that you could experiment with a few images, and determine which maximum pixel size keeps you under 30K.

Also, with jpgs, you have compression options. In this thread, (search for a post made on 04-13-2006 that starts with 'Whoops. Here's the code:') one developer creates a Graphics object and uses a bunch of options to control various quality aspects:

Graphics g = Graphics.FromImage(bp);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

Open in new window


I was just looking for this myself, but haven't had time to try it out, so I can only point you to what I found, but not definitively explain what is going on.
0
 
canuckconsultingAuthor Commented:
Thanks, this seems to be the rght path!  The only issue i have is I am struggling to find the current size of the image.  From the following code:
 
var cover = Image.FromFile("S:\\images\\Books\\Haynes_517.jpg");
Graphics g = Graphics.FromImage(cover);

Open in new window

How can I get the current size (memory used) of the image?
0
 
Visio_GuyCommented:
Hi CC,

Your cover var is a System.Drawing.Image. It has Width and Height and Size properties in pixels that you can examine. If you're looking for file size, you could use System.IO.FileInfo, which has a Length property that returns the number of bytes for the file.
0
 
canuckconsultingAuthor Commented:
Is there a way to calculate the memory used by an image by it's Size property?  When I resize an image using the GetThumbnailImage() method I want to check the resulting memory used.  I don't want the overhead of saving it to disk to check System.IO.FileInfo.  Is there a way to test the size (not dimentions) of an object without having to save it?

Thanks again,

Scott
0
 
canuckconsultingAuthor Commented:
This worked great for thumbnail creation.  To check the size of the resultant image please see this link:

http://www.experts-exchange.com/Programming/Languages/.NET/Q_27389562.html
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now