![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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? |
#3
| |||
| |||
|
|
any ideas what's going on? why serialized JPEG image is deserialized and loaded correctly but can not be saved again? |
|
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? |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |