error when uploading a .gif image

monfu

Newcomer
Joined
Apr 25, 2003
Messages
7
Location
Malta
Dear all

I have the following code:-

System.Drawing.Image src_image = System.Drawing.Image.FromStream(imgStream);

Bitmap bitmap = new Bitmap(image_width, image_height, src_image.PixelFormat);
Graphics new_g = Graphics.FromImage(bitmap);
new_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
new_g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
new_g.SmoothingMode = SmoothingMode.HighQuality;
new_g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
new_g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;

new_g.DrawImage(src_image, 0, 0, bitmap.Width, bitmap.Height);
new_g.Save();
src_image.Dispose();

System.Drawing.Imaging.Encoder Enc = System.Drawing.Imaging.Encoder.Transformation;
EncoderParameters EncParms = new EncoderParameters(1);

EncParms.Param = new EncoderParameter[]
{
new EncoderParameter(Enc,(long)EncoderValue.CompressionLZW),
new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L)
};

ImageCodecInfo ici ;

ici = GetProperEncoder(image_name);

string newFullPath = path + image_name;

bitmap.Save(newFullPath, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
new_g.Dispose();

which is working fine when i upload a "jpg" image. However, when i tried to upload a ".gif" image, it is not working fine. It is giving me the following error:-

"A Graphics object cannot be created from an image that has an indexed pixel format."

I am not saving the image prior to uploading, just putting it in a stream and saving the image after the manipulation is done.

Thanks for your help and time

Johann
 
Gif images are 8-bit, 256 color images that contain their own palette.
Use source images that are 16-bits or higher. With that bit depth, each pixel is defined usually by an RGB value, with no need for a custom palette.

Just convert the .gif image you are using as the source to a jpg or png.

If don't have access to the source image, there might be a way to load in a gif, ask IcePlug.
 
IcePlug is one of the members right?

My problem is that i have a memory stream, and the image is not saved on the server. I will have to save it I guess to manipulate it
 
Well yes, but you can't get a graphics object from .gif file to manipulate it, that's the problem.

Yes, Iceplug is a member
 
Hello Diesel

I managed to do something like this:-

Bitmap org = (Bitmap)Image.FromStream(imgStream);
Bitmap bm=new Bitmap(width, height);
return bm;

and this is working fine since i am saving the gif image correctly. However, my problem now is that the background, instead of transparent, is appearing as black. Have any ideas how to solve this?
 
Back
Top