简体   繁体   中英

Is there any other faster method is there to create a image from a raw pixel data in c#?

At present i am using this code.

ftStatus = myFtdiDevice.Read(one_sec_RGBbuf, 614400, ref one_sec_No_of_bytes_read);
Bitmap pic = new Bitmap(2048, 100, PixelFormat.Format24bppRgb);

int arrayIndex = 0;
for (int x = 0; x < 100; x++)
{
    for (int y = 0; y < 2048; y++)
    {
        pic.SetPixel(y, x, Color.FromArgb(one_sec_RGBbuf[arrayIndex + 2], one_sec_RGBbuf[arrayIndex + 1], one_sec_RGBbuf[arrayIndex]));
        arrayIndex += 3;
    }
}

string p_name = one_count.ToString();
Array.Clear(one_sec_RGBbuf, 0, one_sec_RGBbuf.Length);
pic.Save(p_name + ".bmp", ImageFormat.Bmp);

You could use something like this, which will be magnitudes faster. Note I am a little unsure about your byte array. However this would be enough to get you started.

var width = 2048;
var height = 100;

var bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
// lock the array for direct access
var data = bmp.LockBits(
   new Rectangle(0, 0, width, height),
   ImageLockMode.ReadWrite,
   PixelFormat.Format32bppPArgb);
    
var length = width * height;

try
{

   var span = new Span<int>((int*)data.Scan0, length);

   for (int i = 0, arrayIndex = 0; i < length; i++, arrayIndex += 3)
      span[i] = 0xff << 24 |
                one_sec_RGBbuf[arrayIndex + 2] << 16 |
                one_sec_RGBbuf[arrayIndex + 1] << 8 |
                one_sec_RGBbuf[arrayIndex];     
}
finally
{
   // unlock the bitmap
   bmp.UnlockBits(data);
}

Disclaimer, this is untested to a large degree.

Also, make sure you dispose your Bitmaps after you finish with them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM