如何在c#中将二维数组转换为图像

Ahs*_*oot 5 c#

我在c#中有一个2D整数数组.

2-D阵列中的每个条目对应于像素值

如何将这个2-D数组制作成图像文件(在C#中)

谢谢

tbr*_*dge 11

这是一种非常快速,尽管不安全的方式:

[编辑]此示例耗时0.035毫秒

// Create 2D array of integers
int width = 320;
int height = 240;
int stride = width * 4;
int[,] integers = new int[width,height];

// Fill array with random values
Random random = new Random();
for (int x = 0; x < width; ++x)
{
    for (int y = 0; y < height; ++y)
    {
        byte[] bgra = new byte[] { (byte)random.Next(255), (byte)random.Next(255), (byte)random.Next(255), 255 };
        integers[x, y] = BitConverter.ToInt32(bgra, 0);
    }
}

// Copy into bitmap
Bitmap bitmap;
unsafe
{
    fixed (int* intPtr = &integers[0,0])
    {
        bitmap = new Bitmap(width, height, stride, PixelFormat.Format32bppRgb, new IntPtr(intPtr));
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

结果