Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Bitmap <-> BitmapSource converter

View BitmapFromBitmapSource.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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;
}
View BitmapFromBitmapSource.cs
1 2 3 4 5 6 7 8 9 10 11 12
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
Something went wrong with that request. Please try again.