Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting WriteableBitmap to Bitmap in C#

Is there any way for converting WriteableBitmap to Bitmap in C# ?

like image 470
GVillani82 Avatar asked Jun 25 '13 12:06

GVillani82


1 Answers

It's pretty straightforward, actually. Here's some code that should work. I haven't tested it and I'm writing it from the top of my head.

private System.Drawing.Bitmap BitmapFromWriteableBitmap(WriteableBitmap writeBmp)
{
  System.Drawing.Bitmap bmp;
  using (MemoryStream outStream = new MemoryStream())
  {
    BitmapEncoder enc = new BmpBitmapEncoder();
    enc.Frames.Add(BitmapFrame.Create((BitmapSource)writeBmp));
    enc.Save(outStream);
    bmp = new System.Drawing.Bitmap(outStream);
  }
  return bmp;
}

The WriteableBitmap inherits from a BitmapSource, which can be saved directly to a stream. Then, you build a Bitmap from this stream.

like image 159
red_sky Avatar answered Sep 20 '22 06:09

red_sky