how to convert bitmapsource to bitmap?

mc 6,056 Reputation points
2025-10-08T10:33:30.2966667+00:00

I have gif and there is serveral images in it.

I want to get bitmap and I searched it but it is format of Format8Indexceed. which can not save or edit.

and I can not use SetPixel of the bitmap .

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Answer accepted by question author
  1. Danny Nguyen (WICLOUD CORPORATION) 3,500 Reputation points Microsoft External Staff
    2025-10-09T07:47:46.5433333+00:00

    Hi @mc ,

    In WPF, BitmapSource and System.Drawing.Bitmap belong to two different imaging systems — WPF uses the System.Windows.Media.Imaging namespace, while GDI+ (System.Drawing) uses Bitmap. To convert between them, you’ll need to copy the pixel data manually or through an interop stream.

    If your source is a BitmapSource (for example, a frame from a GifBitmapDecoder), you can write it into a memory stream using a BmpBitmapEncoder, then construct a System.Drawing.Bitmap from that stream:

    using (var stream = new MemoryStream())
    {
        var encoder = new BmpBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
        encoder.Save(stream);
     
        stream.Position = 0;
        using (var bitmap = new System.Drawing.Bitmap(stream))
        {
            // you can now use SetPixel, Save, etc.
        }
    }
    

    This approach preserves the image data in a standard bitmap format that GDI+ can understand, even if your original BitmapSource was in an indexed format like Format8bppIndexed.

    If you’re dealing with frames from a GIF, WPF often decodes them as indexed bitmaps (Format8bppIndexed), which don’t support direct pixel editing in System.Drawing.Bitmap. The encoder approach above ensures the result is in a full-color format (e.g. 24-bit or 32-bit), making SetPixel and other drawing APIs usable.

    You can check out the WPF Image Formats under WPF Imaging Overview for a good explanation of how BitmapSource, pixel formats, and encoding.

    I hope this helps. Please reach out if you need any clarification.

    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Starry Night 600 Reputation points
    2025-10-09T05:34:44.1833333+00:00

    how to convert bitmapsource to bitmap?

    For this, you can refer to the following code:

    private System.Drawing.Bitmap BitmapFromSource(BitmapSource bitmapsource)
    {
       System.Drawing.Bitmap bitmap;
       using (MemoryStream outStream = new MemoryStream())
      {
         BitmapEncoder enc = new BmpBitmapEncoder();
         enc.Frames.Add(BitmapFrame.Create(bitmapsource));
         enc.Save(outStream);
         bitmap = new System.Drawing.Bitmap(outStream);
     }
         return bitmap;
    }
    
    0 comments No comments

  2. sophia lily 0 Reputation points
    2025-10-09T08:19:00.67+00:00

    You can convert a BitmapSource to a System.Drawing.Bitmap using a MemoryStream or by copying pixel data. The simplest and most reliable way is via an encoder, such as BmpBitmapEncoder.

    Example: Convert BitmapSourceBitmap

    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Windows.Media.Imaging;
    
    public static Bitmap BitmapFromSource(BitmapSource bitmapsource)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            BmpBitmapEncoder encoder = new BmpBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bitmapsource));
            encoder.Save(stream);
    
            using (var tempBitmap = new Bitmap(stream))
            {
                return new Bitmap(tempBitmap);
            }
        }
    }
    

    Why This Works

    BitmapSource (WPF) and Bitmap (GDI+) use different systems.

    Using BmpBitmapEncoder converts the format into something Bitmap can load.

    This avoids unsupported formats like Format8bppIndexed.

    SetPixel Issue?

    If you’re getting indexed format errors (8bppIndexed), this conversion gives you a 24bpp or 32bpp bitmap, so SetPixel will work afterward.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.