Skip to content

Instantly share code, notes, and snippets.

@nashby
Created April 12, 2011 20:18
Show Gist options
  • Save nashby/916300 to your computer and use it in GitHub Desktop.
Save nashby/916300 to your computer and use it in GitHub Desktop.
Bitmap <-> BitmapSource converter
public Bitmap GetBitmap(BitmapSource source)
{
Bitmap bmp = new Bitmap
(
source.PixelWidth,
source.PixelHeight,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb
);
BitmapData data = bmp.LockBits
(
new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size),
ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb
);
source.CopyPixels
(
Int32Rect.Empty,
data.Scan0,
data.Height * data.Stride,
data.Stride
);
bmp.UnlockBits(data);
return bmp;
}
public BitmapSource GetBitmapSource(Bitmap bitmap)
{
BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap
(
bitmap.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions()
);
return bitmapSource;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment