How to resize an image file

P

Parrot

I have been using the System.Bitmap function and System.Drawing function to
reduce the size of an image file in my program as follows:

System.Drawing.Image img = bt.GetThumbnailImage(1000, height1, null,
IntPtr.Zero);

However, this creates a horrible resolution image. Is there any other
method that can be used to reduce the size of an image file programmatically
similar to what you can do in Photoshop using C#?
 
F

Family Tree Mike

I have been using the System.Bitmap function and System.Drawing function to
reduce the size of an image file in my program as follows:

System.Drawing.Image img = bt.GetThumbnailImage(1000, height1, null,
IntPtr.Zero);

However, this creates a horrible resolution image. Is there any other
method that can be used to reduce the size of an image file programmatically
similar to what you can do in Photoshop using C#?

Look at the remarks in the MSDN documentation for the
GetThumbnailImage(). You are doing exactly what it says not to do.

From
http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx

The GetThumbnailImage method works well when the requested thumbnail
image has a size of about 120 x 120 pixels. If you request a large
thumbnail image (for example, 300 x 300) from an Image that has an
embedded thumbnail, there could be a noticeable loss of quality in the
thumbnail image. It might be better to scale the main image (instead of
scaling the embedded thumbnail) by calling the DrawImage method.
 
P

Parrot

The DrawImage function does not save the file - it only writes it to a
location on the screen. I want to be able to save the image file in a
smaller size. I have people who upload huge image files to my website and I
want to be able to cut them down to size before I store them in my database.
 
F

Family Tree Mike

The DrawImage function does not save the file - it only writes it to a
location on the screen. I want to be able to save the image file in a
smaller size. I have people who upload huge image files to my website and I
want to be able to cut them down to size before I store them in my database.

Graphics.DrawImage _can_ draw to the screen. There is a different mode
of using it to draw to an image. The code (freehand, not checked) is:

Image bmTN = new Bitmap(120, 120);
Graphics g = Graphics.FromImage(bmTN);
g.DrawImage(bm, 0, 0, 120, 120);
bmTN.Save("Somefile.jpg", Imaging.ImageFormat.Jpeg);

This assumes bm is the image you want to "thumbnail".
 
J

Jeff Johnson

I have been using the System.Bitmap function and System.Drawing function to
reduce the size of an image file in my program as follows:

System.Drawing.Image img = bt.GetThumbnailImage(1000, height1, null,
IntPtr.Zero);

However, this creates a horrible resolution image. Is there any other
method that can be used to reduce the size of an image file
programmatically
similar to what you can do in Photoshop using C#?

Just use the overload of the Bitmap constructor to create a new Bitmap at
the desired size.
 
P

Parrot

Mike;
Thanks very much for your help. Your suggested code worked like a charm. I
never would have figured it out without this forum and your help. Thanks
again.
Dave
 
P

Peter Duniho

Parrot said:
Mike;
Thanks very much for your help. Your suggested code worked like a charm. I
never would have figured it out without this forum and your help. Thanks
again.

His code is just an example. In particular, it's very important that
you add the necessary calls to Dispose(), or use the "using" statement,
to ensure that both the Bitmap and Graphics instances are properly
disposed of when you're done with them.

Pete
 
P

Peter Duniho

Jeff said:
Just use the overload of the Bitmap constructor to create a new Bitmap at
the desired size.

And don't forget to dispose the Bitmap instance after you're done saving it.
 
J

Jeff Johnson

And don't forget to dispose the Bitmap instance after you're done saving
it.

Yes, preferrably by slapping the whole thing in a using() block and letting
the system do it for you.
 
P

Parrot

Peter;

I did do the Dispose calls on both the bitmap and the image. I found if I
didn't that the files would be tied up by the system and was not able to
access them again.
Dave
 
P

Parrot

Peter;
I took your suggestion and selected overload # 11 of the Bitmap call and the
following code worked just fine: This code reduces large files to a width of
350 with the height determined by the ratio.

System.Drawing.Bitmap bt = new System.Drawing.Bitmap(pic);
decimal width = bt.Width;

decimal pct = decimal.Divide(350, width);

decimal height = bt.Height * pct;

int height1 = Convert.ToInt32(height);

System.Drawing.Bitmap btt = new System.Drawing.Bitmap(bt, 350, height1);

btt.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);

bt.Dispose();

btt.Dispose();

System.IO.FileInfo f = new System.IO.FileInfo(pic);
f.Delete();

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top