Created
April 12, 2011 20:18
-
-
Save nashby/916300 to your computer and use it in GitHub Desktop.
Bitmap <-> BitmapSource converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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