C# 从双二维数组创建位图

C# 从双二维数组创建位图,c#,arrays,image,pointers,bitmap,C#,Arrays,Image,Pointers,Bitmap,我有一个二维double[,]rawmimage数组,表示灰度图像,数组中的每个元素都有一个0~1的有理值,我需要 要将其转换为位图,我使用了以下代码: private Bitmap ToBitmap(double[,] rawImage) { int width = rawImage.GetLength(1); int height = rawImage.GetLength(0); Bitmap Image= new Bitmap(width, height)

我有一个二维
double[,]rawmimage
数组,表示灰度图像,数组中的每个元素都有一个0~1的有理值,我需要 要将其转换为位图,我使用了以下代码:

private Bitmap ToBitmap(double[,] rawImage)
{
     int width  = rawImage.GetLength(1);
     int height = rawImage.GetLength(0);

     Bitmap Image= new Bitmap(width, height);

     for (int i = 0; i < height; i++)
         for (int j = 0; j < YSize; j++)
              {
               double color = rawImage[j, i];
               int rgb = color * 255;
               Image.SetPixel(i, j, rgb , rgb , rgb);
              }

     return Image;
}
私有位图ToBitmap(双[,]rawImage)
{
int width=rawImage.GetLength(1);
int height=rawImage.GetLength(0);
位图图像=新位图(宽度、高度);
对于(int i=0;i
但它似乎太慢了。 我不知道是否有办法使用
short
数据类型的指针来完成上述工作


如何使用指针编写更快的代码来处理此函数?

这应该足够了。这个例子就是根据这一点编写的

private不安全位图ToBitmap(双[,]rawImage)
{
int width=rawImage.GetLength(1);
int height=rawImage.GetLength(0);
位图图像=新位图(宽度、高度);
BitmapData BitmapData=Image.LockBits(
新矩形(0,0,宽度,高度),
ImageLockMode.ReadWrite,
PixelFormat.Format32bppArgb
);
ColorARGB*开始位置=(ColorARGB*)bitmapData.Scan0;
对于(int i=0;iA=255;
位置->R=rgb;
位置->G=rgb;
位置->B=rgb;
}
图像。解锁位(位图数据);
返回图像;
}
公共结构ColorARGB
{
公共字节B;
公共字节G;
公共字节R;
公共字节A;
公共颜色argb(彩色)
{
A=颜色。A;
R=颜色。R;
G=颜色。G;
B=颜色。B;
}
公共颜色argb(字节a、字节r、字节g、字节b)
{
A=A;
R=R;
G=G;
B=B;
}
公共颜色ToColor()
{
返回颜色。来自argb(A,R,G,B);
}
}

SetPixel为每个像素锁定整个图像。如果锁定整个图像,则可以使用不安全代码写入图像中的每个值。我正在测试这段代码,一旦它工作正常,我会接受它。你应该在“double color=rawmimage[j,I];”行中交换索引“I,j”;因为在这种形式中会导致“索引超出范围异常”@SamehKamal是的,对不起,这是因为复制粘贴。我要换衣服。代码有用吗?是的,由于我正在处理非常高分辨率的图像,图像现在在不到一秒钟的时间内转换,大约需要20秒,非常感谢。@SamehKamal很棒,很高兴我能提供帮助!
private unsafe Bitmap ToBitmap(double[,] rawImage)
{
    int width = rawImage.GetLength(1);
    int height = rawImage.GetLength(0);

    Bitmap Image = new Bitmap(width, height);
    BitmapData bitmapData = Image.LockBits(
        new Rectangle(0, 0, width, height),
        ImageLockMode.ReadWrite,
        PixelFormat.Format32bppArgb
    );
    ColorARGB* startingPosition = (ColorARGB*) bitmapData.Scan0;


    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++)
        {
            double color = rawImage[i, j];
            byte rgb = (byte)(color * 255);

            ColorARGB* position = startingPosition + j + i * width;
            position->A = 255;
            position->R = rgb;
            position->G = rgb;
            position->B = rgb;
        }

    Image.UnlockBits(bitmapData);
    return Image;
}

public struct ColorARGB
{
    public byte B;
    public byte G;
    public byte R;
    public byte A;

    public ColorARGB(Color color)
    {
        A = color.A;
        R = color.R;
        G = color.G;
        B = color.B;
    }

    public ColorARGB(byte a, byte r, byte g, byte b)
    {
        A = a;
        R = r;
        G = g;
        B = b;
    }

    public Color ToColor()
    {
        return Color.FromArgb(A, R, G, B);
    }
}