Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using c# to resize an image and reduce file size

Posted on 2011-09-09
5
Medium Priority
?
702 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 1500 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

610 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