1

I have a BitmapFrame from deserialization. I need to convert this to BitmapImage. How to do that? I used this code:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/31808363-6b00-43dd-8ea8-0917a35d62ad/how-to-convert-stream-to-bitmapsource-and-how-to-convert-bitmapimage-to-bitmapsource-in-wpf?forum=wpf

The problem is that BitmapImage does not have a Source property, only StreamSource or UriSource.

serialization part:

public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            MemoryStream stream = new MemoryStream();
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(image.UriSource));
            encoder.QualityLevel = 30;
            encoder.Save(stream);
            stream.Flush();
            info.AddValue("Image", stream.ToArray());
...

deserialization :

public ImageInfo(SerializationInfo info, StreamingContext context)
        {
            //Deserialization Constructorbyte[] encodedimage = (byte[])info.GetValue("Image", typeof(byte[]));
            if (encodedimage != null)
            {
                MemoryStream stream = new MemoryStream(encodedimage);
                JpegBitmapDecoder decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default);
                Image = new BitmapImage();
                Image.BeginInit();
                //Image.StreamSource = ...  decoder.Frames[0];
                Image.EndInit();
                Image.Freeze();
            }
...

I would need something effective instead of the comment above...

0

1 Answer 1

1

Besides that you don't really need this conversion (because you can use a BitmapFrame wherever you otherwise use a BitmapImage), you could directly decode a BitmapImage from an encoded bitmap in a byte array.

It is not necessary to explicitly use a BitmapDecoder. When you assign a Stream to the BitmapImage's StreamSource property, the frameworks uses the appropriate decoder automatically. You must take care to set BitmapCacheOption.OnLoad when the Stream should be closed immediately after the BitmapImage was created.

Image = new BitmapImage();
using (var stream = new MemoryStream(encodedimage))
{
    Image.BeginInit();
    Image.CacheOption = BitmapCacheOption.OnLoad;
    Image.StreamSource = stream;
    Image.EndInit();
}
Image.Freeze();
3
  • Thanks, it works great! By the way when I serialize images with the above GetObjectData method I get around 300-400KB / image (serialized). All of the images are at most 150 pixel wide (with ratio of 4:3 or 16:9) with qualitylevel=30. I think this is rather big, am I wrong?
    – Vic
    Nov 26, 2018 at 23:47
  • That is way too much for 150x100 or so pixels. It should be a few KB only. Are you sure you're measuring the encoded byte array size? Does it decrease with lower QualityLevel?
    – Clemens
    Nov 27, 2018 at 0:12
  • Oooops, I have encoded the UriSource of the image, so the full sized image instead of the 150x... thumbnail. Now it's 5-7KB/image :) Thanks for your help again!
    – Vic
    Nov 27, 2018 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.