Solved

Using c# to resize an image and reduce file size

Posted on 2011-09-09
5
661 Views
Last Modified: 2013-12-17
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
Comment
Question by:canuckconsulting
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
Visio_Guy earned 500 total points
ID: 36509752
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
 

Author Comment

by:canuckconsulting
ID: 36535073
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
 
LVL 11

Expert Comment

by:Visio_Guy
ID: 36535303
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
 

Author Comment

by:canuckconsulting
ID: 36535411
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
 

Author Closing Comment

by:canuckconsulting
ID: 36978799
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now