Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, August 4, 2009 1:28 PM
HI,
If I open a (1024*768) 24bit image with C#, change its size to a
smaller one (400 * 400), and save it, the image is now 32 bit, and is
actually bigger in size that the original !
How can i keep the same bit depth?
Thanks
Sam
All replies (14)
Tuesday, August 4, 2009 3:14 PM âś…Answered | 1 vote
Use the Bitmap(int, int, PixelFormat) constructor. For example:
public static Bitmap Create24bpp(Image image, Size size) {
Bitmap bmp = new Bitmap(size.Width, size.Height,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics gr = Graphics.FromImage(bmp))
gr.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height));
return bmp;
}
Hans Passant.
Wednesday, August 5, 2009 3:04 PM
Hi,
Thanks for helping
Could you make it clearer ? I don't think I want to use the DrawImage method... Here's my code :
public void FitImage(string pPath, int pNewWidth, int pMaxHeight)
{
Image FullsizeImage = Image.FromFile(pPath);
// Prevent using images internal thumbnail
FullsizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
int NewHeight = FullsizeImage.Height*pNewWidth/FullsizeImage.Width;
if (NewHeight > pMaxHeight)
{
// Resize with height instead
pNewWidth = FullsizeImage.Width*pMaxHeight/FullsizeImage.Height;
NewHeight = pMaxHeight;
}
Image NewImage = FullsizeImage.GetThumbnailImage(pNewWidth, NewHeight, null, IntPtr.Zero);
// Clear handle to original file so that we can overwrite it if necessary
FullsizeImage.Dispose();
// Save resized picture
NewImage.Save(pPath + "test");
}
How should I modify it so that the bit depth remains the same ? Note that I work with jpg format (dont know if it makes a difference...)
ThanksSam
Wednesday, August 5, 2009 3:41 PM
Well, don't use GetThumbnailImage(). Have you even tried my code snippet?
Hans Passant.
Wednesday, August 5, 2009 3:46 PM
Use Han's code if you want to preserve the format. No matter what code you use, you will be using the DrawImage method, either in your visible code or in the hidden framework code. You're saving in the png format, which is probably a good idea. Saving in the default jpg format, which is 75% quality, would farther degrade the image.
Wednesday, August 5, 2009 4:40 PM
Sorry Hans, I just didn't understand your code.
Ok I've tried it, but I get an exception at the last line, when I attempt to save :
BuzzArt.ArtEngine.Tests.BuzzArtEngineTests.FitImage:
System.ArgumentException : Parameter is not valid.
Note that the path is correct. It works if I don't use the Bitmap.
public void FitImage(string pPath, int pNewWidth, int pMaxHeight)
{
Image FullsizeImage = Image.FromFile(pPath);
// Prevent using images internal thumbnail
FullsizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
int NewHeight = FullsizeImage.Height*pNewWidth/FullsizeImage.Width;
if (NewHeight > pMaxHeight)
{
// Resize with height instead
pNewWidth = FullsizeImage.Width*pMaxHeight/FullsizeImage.Height;
NewHeight = pMaxHeight;
}
Image NewImage = FullsizeImage;
//Image NewImage = FullsizeImage.GetThumbnailImage(pNewWidth, NewHeight, null, IntPtr.Zero);
Bitmap bmp = new Bitmap(pNewWidth, NewHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics gr = Graphics.FromImage(bmp))
gr.DrawImage(NewImage, new Rectangle(0, 0, bmp.Width, bmp.Height));
// Clear handle to original file so that we can overwrite it if necessary
FullsizeImage.Dispose();
// Save resized picture
NewImage.Save(pPath);
}
Any idea what the problem is ?
John, I'm not saving as png, or I'm not aware of it.... HOw do you do that ?Sam
Wednesday, August 5, 2009 4:45 PM | 1 vote
Save the Bitmap. Don't mix and match your code with Han's. Get the width and height of the bitmap and use them in the constructor.
Wednesday, August 5, 2009 5:11 PM | 1 vote
You have to use bmp.Save(), not NewImage.Save(). You are saving it as a PNG file because you didn't specify the format. Be sure to Dispose() it if you don't use it afterwards.
Hans Passant.
Thursday, August 6, 2009 10:12 AM
Hi,
Thanks for helping.
I have tested the code, and it works. The bit depth of 24 bits is preserved.
However I still don't understand why you are saying I'm saving as png. The saved image still has a .jpg extension.Sam
Thursday, August 6, 2009 10:18 AM
An image file doesn't automatically change format when you give it the wrong extension. You've got a .jpg file that contains a PNG image. You rarely notice, graphics programs look at the file content, not the file extension. Open the file with Notepad if you don't believe me, you'll see xPNG at the start.
Use the 2nd argument with Save() if you really want the file in the JPEG format.
Hans Passant.
Thursday, August 6, 2009 10:27 AM
Yes you are right :) Ok, we're all good now. Thanks a lot guys !Sam
Saturday, August 8, 2009 10:28 AM
Hi,
I have another problem related to the above. I don't think my way of resizing jpg pictures is good enough. In fact I've noticed that for relatively small files (< 1000 Kb), the new file is actually bigger !
Ideally I'd like the new file to be under 200 Kb. I'm working with photos of up to 5000 Kb. For these the resulting files is around 600 Kb, which is still too big. However when the orginal photos is, for instance 700 Kb big, the resized one is 900 Kb. See ? It's bigger !
Is there something wrong with my algorithm ?
ThanksSam
Saturday, August 8, 2009 12:09 PM
You're saving in the png format, which is probably a good idea. Saving in the default jpg format, which is 75% quality, would farther degrade the image.
Saturday, August 8, 2009 2:21 PM
Please start a new thread for your new question. Be sure to include important details like a snippet of your code, the amount by which you resize and a link to the image file.Hans Passant.
Saturday, August 8, 2009 6:25 PM
Hi,
No problem. I've created a new thread there : http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/f65bfb37-6756-4e7d-9d7f-ddbfa6eb576f
ThanksSam