1

I am trying to write a function that converts a BitmapSource to a BitmapImage. The only way I could find to do this was to create a temporary bmp file write and read from the file to create a new BitmapImage. This works fine until you try to delete the file or re-use the function. There are some file handling errors where the file will still be in use until the BitmapImage is freed.

1 - Is there a better way of converting BitmapSource to BitmapImages?

2 - What can I do to return the BitmapImage without freeing it?

public BitmapImage ConvertSourceToBitMapImage(BitmapSource bitmapSource)
{
    BitmapImage bmi = null;
    try
    {
        string filePath = @"C:\GitRepository\ReceiptAPP\ReceiptApplication\bin\Debug\testing.bmp";
        using (var fileStream = new FileStream(filePath, FileMode.Create))
        {
            BitmapEncoder encoder = new BmpBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
            encoder.Save(fileStream);
            fileStream.Close();
        }
        Uri path = new Uri(filePath);
        bmi = new BitmapImage(path);
        File.Delete(filePath);
    }
    catch(Exception ex)
    {
    }
    return bmi;
}

enter image description here

Clemens
  • 123,504
  • 12
  • 155
  • 268
Sari Rahal
  • 1,897
  • 2
  • 32
  • 53
  • Why would you want to do that at all? The conversion is nowhere necessary. Besides that, a better way than a temporary file would of course be a MemoryStream. – Clemens Nov 29 '21 at 14:20
  • Does this answer your question? [Faster way to convert BitmapSource to BitmapImage](https://stackoverflow.com/questions/24863594/faster-way-to-convert-bitmapsource-to-bitmapimage) – NineBerry Nov 29 '21 at 15:09
  • You should actually not be using BitmapImage at all in your model or view model. Use BitmapSource objects when you want to deal with bitmaps. – Clemens Nov 29 '21 at 15:15
  • @Clemens I agree, but we do not know the reason why OP wants to do this conversion. It could be a third party library that requires a `BitmapImage` object as input. So we need more context, before suggesting to simply use `BitmapSource`. – Lars Kristensen Nov 30 '21 at 07:58
  • @LarsKristensen Quite the opposite. Unless OP gives an explicit reason why they require the conversion, we should always assume they made the simple mistake of using BitmapImage where BitmapSource would be sufficient. If you look at their previous question, you can be pretty sure that this is the case here. – Clemens Nov 30 '21 at 08:50
  • Yes, this was a restriction b/c of the amount of code in the service. I agree that using BitmapSources would have been a much better way but when the project was started, we didn't know better. Now just paying for our sins. Thanks guys. – Sari Rahal Nov 30 '21 at 13:34

1 Answers1

2

Encoding to and decoding from a MemoryStream is a lot more efficient than a file.

Note however that there is no use case for a conversion from BitmapSource to BitmapImage. A BitmapSource can directly be used as the Source of an Image element or as the ImageSource of an ImageBrush. You never explicitly need a BitmapImage.

public static BitmapImage ConvertBitmapSourceToBitmapImage(
    BitmapSource bitmapSource)
{
    // before encoding/decoding, check if bitmapSource is already a BitmapImage

    if (!(bitmapSource is BitmapImage bitmapImage))
    {
        bitmapImage = new BitmapImage();

        BmpBitmapEncoder encoder = new BmpBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmapSource));

        using (MemoryStream memoryStream = new MemoryStream())
        {
            encoder.Save(memoryStream);
            memoryStream.Position = 0;

            bitmapImage.BeginInit();
            bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
            bitmapImage.StreamSource = memoryStream;
            bitmapImage.EndInit();
        }
    }

    return bitmapImage;
}
Clemens
  • 123,504
  • 12
  • 155
  • 268