If you have byte[]
pixels and the width and height, you can use BitmapData
to write bytes to the bitmap, since you also know the format. Here is an example:
//Your actual bytes byte[] bytes = {255, 0, 0, 0, 0, 255}; var width = 2; var height = 1; //Make sure to clean up resources var bitmap = new Bitmap(width, height); var data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); Marshal.Copy(bytes, 0, data.Scan0, bytes.Length); bitmap.UnlockBits(data);
This is a very fast operation.
You will need to import these three namespaces at the top of your C # file, at a minimum:
using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices;
source share