Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert System.Windows.Media.ImageSource to System.Drawing.Bitmap

Tags:

c#

image

wpf

How can I convert a System.Windows.Media.ImageSource to a System.Drawing.Bitmap in C#?

like image 665
Jake Avatar asked Jul 29 '09 16:07

Jake


1 Answers

its older OP, but still it can come handy for some other people, as it took some time to find cleaner solution without dll interop or clipboard hacks.

this worked for me, you can use pngencoder to cut the image size before saving to file or rtf stream

private System.Drawing.Image ImageWpfToGDI(System.Windows.Media.ImageSource image) {
  MemoryStream ms = new MemoryStream();
  var encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
  encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(image as System.Windows.Media.Imaging.BitmapSource));
  encoder.Save(ms);
  ms.Flush();      
  return System.Drawing.Image.FromStream(ms);
}
like image 137
kimic Avatar answered Sep 20 '22 07:09

kimic