0

I'm converting a Bitmap to WritableBitmap, but they both have many formats, I can't figure out the corresponding relations.

I want to cover all convert conditions, so is there any official docs refer?

private System.Windows.Media.PixelFormat ConvertPixelFormats(PixelFormat pixelFormat)
{
    System.Windows.Media.PixelFormat format;
    switch (pixelFormat)
    {
        case PixelFormat.Format16bppRgb555:
            format = PixelFormats.Bgr555;
            break;
        case PixelFormat.Format16bppRgb565:
            format = PixelFormats.Bgr565;
            break;
        case PixelFormat.Format24bppRgb:
            format = PixelFormats.Bgr24;
            break;
        case PixelFormat.Format32bppRgb:
            format = PixelFormats.Bgr32;
            break;
        case PixelFormat.Format32bppPArgb:
            format = PixelFormats.Pbgra32;
            break;
        case PixelFormat.Format32bppArgb:
            format = PixelFormats.Bgra32;
            break;
        case PixelFormat.Format8bppIndexed:
            format = PixelFormats.Gray8;
            break;
    }

    return format;
}
Vincent
  • 3,124
  • 3
  • 21
  • 40
  • Why are you converting a Bitmap to WritableBitmap? They belong to different APIs. You can use BitmapImage to load a picture. – shingo Sep 16 '22 at 05:08
  • @shingo We are showing Bitmaps frequently to `Image`'s Source(just like receive a camera stream). So we convert `Bitmap` to `ImageSource`. But this cause the memory always rising. So we convert `Bitmap` to `WritableBitmap`, this can solve the memory bug. – Vincent Sep 16 '22 at 05:26
  • I mean you can load an image file with `BitmapImage`, it's a subclass of `ImageSource`, why do you have to convert from `Bitmap`? How do you get the `Bitmap` object? From a third party library? – shingo Sep 16 '22 at 05:36

0 Answers0