Solved

Using c# to resize an image and reduce file size

Posted on 2011-09-09
5
657 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

13 Experts available now in Live!

Get 1:1 Help Now