Skip to content

Instantly share code, notes, and snippets.

@IngIeoAndSpare
Last active August 23, 2019 01:40
Show Gist options
  • Save IngIeoAndSpare/01891bf34f8b996da4cf951c861b1106 to your computer and use it in GitHub Desktop.
Save IngIeoAndSpare/01891bf34f8b996da4cf951c861b1106 to your computer and use it in GitHub Desktop.
c# convert image to bitmap
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