Resizing Drawing.Image then saving it

F

funcSter

Hey, got a prob which is driving me nuts!

I'm trying to resize the resolution of an image as well as it's pyhsical byte size.

I've got:

byte[] bytImage = null;
System.Drawing.Image imgImage = null;
System.Drawing.Image imgNewImage = null;

using(FileStream fs = File.OpenRead(@"C:\Images\Test.jpg"))
{
bytImage = new byte[fs.Length];
fs.Read(bytImage, 0, (int)fs.Length);
imgImage = System.Drawing.Image.FromStream(fs);
fs.Close();
}

imgNewImage = new Bitmap(imgImage, 200, 200);

imgNewImage.Save(@"C:\Images\Test_1.jpg"))

What's goin wrong is this:
Test.jpg is originally 400 x 600, with PixelFormat = Format24bppRgb
Test_1.jpg is 200 x 200 BUT with PixelFormat = Format32bppARgb

So I tend to get my resized images file bigger in byte size than the original!

:( How do I rectify this? Or is there a better alternative to resizing an Image?
 
S

Sushant Bhatia

This is the code I wrote to get it to work. My original file
(Test.jpg) was a 640 * 480 file of size 82.4KB. After running my code,
my new file (Test10.jpg) was a 200 * 200 file of size 6.62KB.

/*
* Created by SharpDevelop.
* User: Sushant
* Date: 11/12/2004
* Time: 9:23 AM
*
* To change this template use Tools | Options | Coding | Edit
Standard Headers.
*/

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

class MainClass
{
public static int Main(string[] args)
{
Image oldImage = Image.FromFile(@"Test.jpg");
Image thumb = new Bitmap(200, 200,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics oGraphic = Graphics.FromImage(thumb);

oGraphic.CompositingQuality = CompositingQuality.HighSpeed;
oGraphic.SmoothingMode = SmoothingMode.HighSpeed;
oGraphic.InterpolationMode = InterpolationMode.Low;

Rectangle rect = new Rectangle(0,0, 200, 200);
oGraphic.DrawImage(oldImage, rect);
thumb.Save("Test10.jpg", ImageFormat.Jpeg);

return 0;
}
}
 
F

funcSter

Thanks for your help!

Shushant, your code was really helpful!

One more question with regards to this topic. Shushant, maybe you can
help me with this again!

If I want to resize a Gif:

Image oNewImage = new Bitmap(200, 200,
System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

Graphics oGraphics = Graphics.FromImage(oNewImage);

I would get an error saying something like
"Graphics object canont be created from an image that has an indexed
pixel format"

:( how now?
 
S

Sushant Bhatia

Hi Luanne

I tried my code with a gif file and i got no error. The only changes
I made was to the name of the file. For instance, the line

Image oldImage = Image.FromFile(@"Test.jpg");

was changed to

Image oldImage = Image.FromFile(@"test.gif");

Notice that the file name is changed from Test.jpg to test.gif.

So I started with a gif file 316 * 207 at 12.7KB and got a JPG file of
200 * 200 at 8.72KB.

Seems to work for me with the modification to the file name. Did u
remember to change that?


Just to be sure, I posted the code again with the change I made. Let
me know if there is anything else? Perhaps you can post a link to the
image files your using. That way I can test it out.






using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

class MainClass
{
public static int Main(string[] args)
{
Image oldImage = Image.FromFile(@"test.gif");
Image thumb = new Bitmap(200, 200,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics oGraphic = Graphics.FromImage(thumb);

oGraphic.CompositingQuality = CompositingQuality.HighSpeed;
oGraphic.SmoothingMode = SmoothingMode.HighSpeed;
oGraphic.InterpolationMode = InterpolationMode.Low;

Rectangle rect = new Rectangle(0,0, 200, 200);
oGraphic.DrawImage(oldImage, rect);
thumb.Save("Test10.jpg", ImageFormat.Jpeg);

return 0;
}
}
 

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