HighTechTalks DotNet Forums  

Image.Save() throws GDI+ exception

Dotnet Framework (Drawing) microsoft.public.dotnet.framework.drawing


Discuss Image.Save() throws GDI+ exception in the Dotnet Framework (Drawing) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
SharpCoderMP
 
Posts: n/a

Default Image.Save() throws GDI+ exception - 11-04-2007 , 04:08 PM






scenario (i call it wise image save):
PART 1
1. open bitmap from file
2. check if image uses alpha transparency - that is literally iterate
through each pixel and check if there is any pixel with alpha value
other than 255
3a. if image has transparent pixels save to stream as PNG
3b. if image is fully opaque save to stream as JPEG to save space
4. convert stream int byte array and to xml string.

PART 2
5. deserialize stream from xml string into bitmap object.
6. display, but do not modify bitmap
7. save again using PART 1 method.

now the problem is at step 7. If the image is saved as a JPEG (step 3b)
then when i try to save it again i get GDI+ exception
(System.Runtime.InteropServices.ExternalException) . i would gladly paste
the exception message if only i knew it's english version - the one i
get is in polish. error code has value of -2147467259 so it is useful
for nothing

any ideas what's going on? why serialized JPEG image is deserialized and
loaded correctly but can not be saved again?

Reply With Quote
  #2  
Old   
SharpCoderMP
 
Posts: n/a

Default Re: Image.Save() throws GDI+ exception - 11-04-2007 , 04:30 PM






I forgot to write that this exception is thrown only when:
1. i save image to memory stream
2. convert memory stream into byte array
4. serialize byte array into xml string
5. deserialize byte array from xml string
6. load image from deserialized stream

when i save directly to a file there is no problem. all works fine.

serialization code is similar to this:
using (MemoryStream ms = new MemoryStream())
using (StringWriter sw = new StringWriter())
{
using (EncoderParameters ep = new EncoderParameters())
{
//Get the list of available encoders
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
//find the encoder with the image/jpeg mime-type
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
{
ici = codec;
break;
}
}
ep.Param[0] = new EncoderParameter(
Encoder.Quality, compressionRatio);
image.Save(ms, ici, ep);
}
XmlSerializer xs = new XmlSerializer(typeof(byte[]));
xs.Serialize(sw, ms.ToArray());
sw.Flush();
xmlString = sw.ToString();
}

using (StringReader sr = new StringReader(xmlString))
{
XmlSerializer xs = new XmlSerializer(typeof(byte[]));
using (MemoryStream ms
= new MemoryStream((byte[])xs.Deserialize(sr)))
image = (Bitmap)Bitmap.FromStream(ms);
}

SharpCoderMP wrote:
Quote:
scenario (i call it wise image save):
PART 1
1. open bitmap from file
2. check if image uses alpha transparency - that is literally iterate
through each pixel and check if there is any pixel with alpha value
other than 255
3a. if image has transparent pixels save to stream as PNG
3b. if image is fully opaque save to stream as JPEG to save space
4. convert stream int byte array and to xml string.

PART 2
5. deserialize stream from xml string into bitmap object.
6. display, but do not modify bitmap
7. save again using PART 1 method.

now the problem is at step 7. If the image is saved as a JPEG (step 3b)
then when i try to save it again i get GDI+ exception
(System.Runtime.InteropServices.ExternalException) . i would gladly paste
the exception message if only i knew it's english version - the one i
get is in polish. error code has value of -2147467259 so it is useful
for nothing

any ideas what's going on? why serialized JPEG image is deserialized and
loaded correctly but can not be saved again?

Reply With Quote
  #3  
Old   
Michael Phillips, Jr.
 
Posts: n/a

Default Re: Image.Save() throws GDI+ exception - 11-05-2007 , 09:08 AM



Quote:
any ideas what's going on? why serialized JPEG image is deserialized and
loaded correctly but can not be saved again?
Have you tried rewinding your streams to position 0 before using the image
save method?

I have seen inconsistancy with MS API's regarding streams.

Some assume nothing and seek to position 0 before they operate on the
stream.

Others assume the stream is at position 0 and fail, if otherwise.


"SharpCoderMP" <csharp_mp (AT) interia (DOT) pl.NFSPM> wrote

Quote:
I forgot to write that this exception is thrown only when:
1. i save image to memory stream
2. convert memory stream into byte array
4. serialize byte array into xml string
5. deserialize byte array from xml string
6. load image from deserialized stream

when i save directly to a file there is no problem. all works fine.

serialization code is similar to this:
using (MemoryStream ms = new MemoryStream())
using (StringWriter sw = new StringWriter())
{
using (EncoderParameters ep = new EncoderParameters())
{
//Get the list of available encoders
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
//find the encoder with the image/jpeg mime-type
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
{
ici = codec;
break;
}
}
ep.Param[0] = new EncoderParameter(
Encoder.Quality, compressionRatio);
image.Save(ms, ici, ep);
}
XmlSerializer xs = new XmlSerializer(typeof(byte[]));
xs.Serialize(sw, ms.ToArray());
sw.Flush();
xmlString = sw.ToString();
}

using (StringReader sr = new StringReader(xmlString))
{
XmlSerializer xs = new XmlSerializer(typeof(byte[]));
using (MemoryStream ms
= new MemoryStream((byte[])xs.Deserialize(sr)))
image = (Bitmap)Bitmap.FromStream(ms);
}

SharpCoderMP wrote:
scenario (i call it wise image save):
PART 1
1. open bitmap from file
2. check if image uses alpha transparency - that is literally iterate
through each pixel and check if there is any pixel with alpha value
other than 255
3a. if image has transparent pixels save to stream as PNG
3b. if image is fully opaque save to stream as JPEG to save space
4. convert stream int byte array and to xml string.

PART 2
5. deserialize stream from xml string into bitmap object.
6. display, but do not modify bitmap
7. save again using PART 1 method.

now the problem is at step 7. If the image is saved as a JPEG (step 3b)
then when i try to save it again i get GDI+ exception
(System.Runtime.InteropServices.ExternalException) . i would gladly paste
the exception message if only i knew it's english version - the one i
get is in polish. error code has value of -2147467259 so it is useful
for nothing

any ideas what's going on? why serialized JPEG image is deserialized and
loaded correctly but can not be saved again?



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.