2017-04-08 145 views
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; 
    } 
} 

回答

0

不知道你如何到达5

的步幅是像素的单排的宽度(扫描线),圆最多四个字节的边界。

因为它是3×3,3四舍五入到四字节边界为4

+0

这不是4个字节。这是4位。 – anonymous

+0

我不知道你的意思是“4字节”还是“4位”。如果您使用[AForge CreateGrayscaleImage](http://www.aforgenet.com/framework/docs/html/657b935b-9e20-6a2b-d0ae-5e59e23e17df.htm),它将使用每像素8位256(2^8 )灰色阴影,意思是每像素一个字节和每行三个字节,四舍五入为4. –

+0

颜色深度应该是多少? – anonymous