Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF BitmapImage Serialization/Deserialization
    primarykey
    data
    text
    <p>I've been trying to Serialize and Deserialize BitmapImages. I've been using methods which supposedly works which I found in this thread: <a href="https://stackoverflow.com/questions/3886849/error-in-my-byte-to-wpf-bitmapimage-conversion">error in my byte[] to WPF BitmapImage conversion?</a></p> <p>Just to iterate what is going on, here is part of my Serialization code: </p> <pre><code>using (MemoryStream ms = new MemoryStream()) { // This is a BitmapImage fetched from a dictionary. BitmapImage image = kvp.Value; PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(image)); encoder.Save(ms); byte[] buffer = ms.GetBuffer(); // Here I'm adding the byte[] array to SerializationInfo info.AddValue((int)kvp.Key + "", buffer); } </code></pre> <p>And here is the deserialization code:</p> <pre><code>// While iterating over SerializationInfo in the deserialization // constructor I pull the byte[] array out of an // SerializationEntry using (MemoryStream ms = new MemoryStream(entry.Value as byte[])) { ms.Position = 0; BitmapImage image = new BitmapImage(); image.BeginInit(); image.StreamSource = ms; image.EndInit(); // Adding the timeframe-key and image back into the dictionary CapturedTrades.Add(timeframe, image); } </code></pre> <p>Also, I'm not sure if it matters but earlier when I populated my dictionary I encoded Bitmaps with PngBitmapEncoder to get them into BitmapImages. So not sure if double-encoding has something to do with it. Here's the method that does that:</p> <pre><code>// Just to clarify this is done before the BitmapImages are added to the // dictionary that they are stored in above. private BitmapImage BitmapConverter(Bitmap image) { using (MemoryStream ms = new MemoryStream()) { image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); BitmapImage bImg = new BitmapImage(); bImg.BeginInit(); bImg.StreamSource = new MemoryStream(ms.ToArray()); bImg.EndInit(); ms.Close(); return bImg; } } </code></pre> <p>So the problem is, serialization and deserialization works fine. No errors, and the dictionary has entries with what seems to be BitmapImages, however their width/height and some other properties are all set to '0' when I look at them in debugging-mode. And of course, nothing is shown when I try to display the images.</p> <p>So any ideas as to why they aren't properly deserialized?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload