Asked by:
Convert JPG image to PNG

Question
-
User-1103495211 posted
I try to convert a JPG image to a PNG image
But i got some errors.This is the script i use.
Till the thumbnailImage its a JPG, after it in the mstream i like to convert it to a PNG so i've got a better quality.
but i got an error.The code i use:
System.Drawing.Image thumbnailImage = dbImage.GetThumbnailImage(new_width, new_height, null, new System.IntPtr());
System.IO.MemoryStream mstream = new System.IO.MemoryStream(byteArray, 0, ConvertImageToByteArray(thumbnailImage, System.Drawing.Imaging.ImageFormat.Png).Length);
thumbnailImage.Save(mstream, System.Drawing.Imaging.ImageFormat.Png);So if i use System.Drawing.Imaging.ImageFormat.JPEG its going well!
But the quality is to bad, so i try to use PNG.The errors i get:
On the first row:
"Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."
On the second:
"{"A generic error occurred in GDI+."}"Also i tried:
system.Drawing.Image dbImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream(byteArray));
dbImage.Save(dbImage, System.Drawing.Imaging.ImageFormat.Png);But right here i need to use a path... , i dont like to save it first, should i use a memorystream ore something here?
Thursday, August 14, 2008 4:42 AM
All replies
-
User-417784260 posted
GetThumbNailImage does not produce a good quality image. I would create the thumb nail like this. ImageFormat.Png and ImageFormat.Jpg only affect how an image is saved it does nothing for quality.
Bitmap bm = (Bitmap)Bitmap.FromFile("SilverLight Dawn.bmp"); Bitmap bmThumb = new Bitmap(120, 80); Graphics g = Graphics.FromImage(bmThumb); g.DrawImage(bm, 0, 0, 120, 80); g.Dispose();
Thursday, August 14, 2008 6:16 AM -
User-1103495211 posted
Strange, why does getthumbnailimage give a bad image back ?Thursday, August 14, 2008 6:24 AM -
User-1103495211 posted
Just a question, its not going well...
I try this part:
System.Drawing.Image thumbnailImage = resize(dbImage, new_width, new_height); //dbImage.GetThumbnailImage(new_width, new_height, null, new System.IntPtr());
System.IO.MemoryStream mstream = new System.IO.MemoryStream(byteArray, 0, ConvertImageToByteArray(thumbnailImage, System.Drawing.Imaging.ImageFormat.Jpeg).Length);But i'll got an error on the last line:
Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.My function i use for the resize is:
public System.Drawing.Image resize(System.Drawing.Image img, int x, int y)
{
Bitmap bm = new Bitmap(x, y);
//Bitmap bmThumb = new Bitmap(x,y);
Graphics g = Graphics.FromImage(img);
g.DrawImage(bm, 0, 0, x, y);
g.Save();
g.Dispose();return img;
}I hope its ok ?
For putting the image to a byt i use (this code worked fine bevore!):
private static byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert,
System.Drawing.Imaging.ImageFormat formatOfImage)
{
byte[] Ret;try
{
using (MemoryStream ms = new MemoryStream())
{
imageToConvert.Save(ms, formatOfImage);
Ret = ms.ToArray();
}
}
catch (Exception) { throw; }return Ret;
}Thursday, August 14, 2008 7:15 AM -
User-1103495211 posted
My function doesnt work well.
It returns the same image back.. so it doesnt resize.. whats wrong?
{
Bitmap bm = new Bitmap(x, y);
//Bitmap bmThumb = new Bitmap(x,y);
Graphics g = Graphics.FromImage(img);
g.DrawImage(bm, 0, 0, x, y);
g.Save();
g.Dispose();return img;
}Thursday, August 14, 2008 7:38 AM -
User-1103495211 posted
Found it.. i returned the wrong image...
public System.Drawing.Image resize(System.Drawing.Image img, int x, int y)
{
System.Drawing.Image empty = new Bitmap(x, y);
Graphics g = Graphics.FromImage(empty);
g.DrawImage(img, 0, 0, x, y);
g.Save();return empty;
g.Dispose();
}Thursday, August 14, 2008 7:48 AM -
User-56641222 posted
Hello Everybody,
I am working on saving the images in the DB. As a part of it I have to preprocess the images, therefore internally in the system I use Bitmap object. After preprocessing (resizing etc.) I save it in the DB in JPG format. The question is how can I set the PG compression ratio? I would like to make it 75 %, but when I define the picture format, I can not set the quality level.
My code:
1 private static byte[] ConvertBitmapToByteArray(Bitmap BitmapToConvert)
2 {
3 Stream ImageMemoryStream = new MemoryStream();
4 5 // save the image to the MemoryStream as a Jpeg
6 // TODO: find out what is the default compression rate (probably 100 ?) 7 BitmapToConvert.Save(ImageMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg); // the ImageFormat has only 1 conctructor - it takes a Guid as parameter ...
8 9 // create new byte[] 10 byte[] retVal = new byte[ImageMemoryStream.Length];
11 12 // save the image binary from the MemoryStream to the byte[] 13 ImageMemoryStream.Position = 0;
14 ImageMemoryStream.Read(retVal, 0, (int)ImageMemoryStream.Length);
15 ImageMemoryStream.Close();
16 17 return retVal;
18 }Thanks in advance,
Jozef A. Habdnak
Tuesday, September 16, 2008 7:39 AM -
User587478106 posted
Does this help?
http://forums.asp.net/t/529816.aspx#531917edit: or this?
http://www.bobpowell.net/jpeg_compression.htm
Friday, September 19, 2008 11:15 AM