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.