Solved

Using c# to resize an image and reduce file size

Posted on 2011-09-09
5
685 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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 …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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