2017-04-08 28 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