Last active
August 23, 2019 01:40
-
-
Save IngIeoAndSpare/01891bf34f8b996da4cf951c861b1106 to your computer and use it in GitHub Desktop.
c# convert image to bitmap
This file contains 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
private Bitmap convertImageToBitmap(Image targetImage) | |
{ | |
Rectangle m_Rect = new Rectangle(0, 0, targetImage.Width, targetImage.Height); | |
Bitmap pic = new Bitmap(targetImage.Width, targetImage.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); | |
byte[] imageData = convertImageToByteArray(targetImage); | |
lock (targetImage) | |
{ | |
System.Drawing.Imaging.BitmapData bmpData = pic.LockBits( | |
m_Rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, | |
pic.PixelFormat | |
); | |
System.Runtime.InteropServices.Marshal.Copy(imageData, 0, bmpData.Scan0, imageData.Length); | |
pic.UnlockBits(bmpData); | |
} | |
return pic; | |
} | |
private byte[] convertImageToByteArray(Image sourceImage) | |
{ | |
ImageConverter imageConverter = new ImageConverter(); | |
byte[] xByte = (byte[])imageConverter.ConvertTo(sourceImage, typeof(byte[])); | |
return xByte; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment