繁体   English   中英

还有其他更快的方法可以从 c# 中的原始像素数据创建图像吗?

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

目前我正在使用此代码。

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);

你可以使用这样的东西,它会更快。 注意我有点不确定你的字节数组。 但是,这足以让您入门。

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);
}

免责声明,这在很大程度上未经测试。

此外,请确保在完成位图后处理它们

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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