0
虽然从double[,]
到Bitmap
转换,如何正确设置图像的步幅?
Bitmap image = ImageDataConverter.ToBitmap(new double[,]
{
{ .11, .11, .11, },
{ .11, .11, .11, },
{ .11, .11, .11, },
});
常规给
data.Stride == 4
哪里这个值是从哪里来的?
由于double[,]
是3x3
,所以步幅应为5
。对?
我该如何解决这个问题?不仅是针对这个问题,而且还针对任何维度?
相关的源码
public class ImageDataConverter
{
public static Bitmap ToBitmap(double[,] input)
{
int width = input.GetLength(0);
int height = input.GetLength(1);
Bitmap output = Grayscale.CreateGrayscaleImage(width, height);
BitmapData data = output.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly,
output.PixelFormat);
int pixelSize = System.Drawing.Image.GetPixelFormatSize(output.PixelFormat)/8;
int offset = data.Stride - width * pixelSize;
double Min = 0.0;
double Max = 255.0;
unsafe
{
byte* address = (byte*)data.Scan0.ToPointer();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
double v = 255 * (input[x, y] - Min)/(Max - Min);
byte value = unchecked((byte)v);
for (int c = 0; c < pixelSize; c++, address++)
{
*address = value;
}
}
address += offset;
}
}
output.UnlockBits(data);
return output;
}
}
这不是4个字节。这是4位。 – anonymous
我不知道你的意思是“4字节”还是“4位”。如果您使用[AForge CreateGrayscaleImage](http://www.aforgenet.com/framework/docs/html/657b935b-9e20-6a2b-d0ae-5e59e23e17df.htm),它将使用每像素8位256(2^8 )灰色阴影,意思是每像素一个字节和每行三个字节,四舍五入为4. –
颜色深度应该是多少? – anonymous