2012-09-26 49 views
0

我正在從一個1bpp位圖複製到一個較小的1bpp位圖。我只想剪出一個區域,以便計算黑色像素的數量。我如何獲得比位圖寬度小的步幅?

我使用以下方法來進行復印:

private Bitmap Copy(Bitmap srcBitmap, Rectangle section) 
    { 
     BitmapData SourceLockedData; 
     BitmapData DestLockedData; 
     Rectangle DestRect; 
     byte[] SrcImageData; 
     byte[] DestImageData; 
     int ByteCount; 
     int WidthCount = 0; 
     int CurrentLine = 0; 
     int DestStride; 
     int SrcStride = 0; 

     // Create the new bitmap and associated graphics object 
     Bitmap Newbmp = new Bitmap(section.Width, section.Height, PixelFormat.Format1bppIndexed); 
     Newbmp.SetResolution(srcBitmap.HorizontalResolution, srcBitmap.VerticalResolution); 

     //Lock the bits 
     SourceLockedData = srcBitmap.LockBits(section, ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed); 

     SrcStride = SourceLockedData.Stride; 

     //Get a count of the number of bytes to copy. Remember, bytes are not pixels. 
     ByteCount = SourceLockedData.Stride * SourceLockedData.Height; 

     //Initialize the source byte array 
     SrcImageData = new byte[ByteCount]; 

     //Copy the data to the source byte array 
     Marshal.Copy(SourceLockedData.Scan0, SrcImageData, 0, ByteCount); 

     //Unlock the bits 
     srcBitmap.UnlockBits(SourceLockedData); 

     //Set a rectangle to the size of the New bitmap 
     DestRect = new Rectangle(new Point(0, 0), Newbmp.Size); 

     //Lock the bits 
     DestLockedData = Newbmp.LockBits(DestRect, ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed); 

     DestStride = DestLockedData.Stride; 

     //Get a count of the number of bytes to copy. Remember, bytes are not pixels. 
     ByteCount = DestLockedData.Stride * DestLockedData.Height; 

     //Initialize the source byte array 
     DestImageData = new byte[ByteCount]; 

     //Copy the data to the destination byte array 
     Marshal.Copy(DestLockedData.Scan0, DestImageData, 0, ByteCount); 

     //Unlock for now 
     Newbmp.UnlockBits(DestLockedData); 

     for (int ArrayIndex = 0; ArrayIndex < ByteCount; ArrayIndex++) 
     { 
      if (WidthCount == Newbmp.Width) 
      { 
       //increment the line and push the index by the stride 
       ArrayIndex = (++CurrentLine) * DestStride; 
       continue; 
      } 

      DestImageData[ArrayIndex] = SrcImageData[ArrayIndex]; 
      WidthCount++; 
     } 

     //Lock the bits again 
     DestLockedData = Newbmp.LockBits(DestRect, ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed); 

     //Copy data from byte array to IntPtr 
     Marshal.Copy(DestImageData, 0, DestLockedData.Scan0, ByteCount); 

     //Unlock bits 
     Newbmp.UnlockBits(DestLockedData); 

     // Return the bitmap 
     return Newbmp; 
    } 

我遇到的最大的問題是,無論是SourceLockedData.Stride和DestLockedData.Stride比各圖像的寬度。怎麼可能?從我所知道的關於跨度的所有信息來看,它是從一條掃描線數據到下一條掃描線數據的位數。它在數學上如何可能小於寬度?

我使用LockBits或BitmapData錯誤嗎? BitmapData不可信嗎?我是否應該手動計算步幅?

湯姆P.

+2

是不是'stride' _bytes_的數量而不是_bits_? –

回答

0

我想通了,步幅可以小於寬度,如果你正在處理RLE位圖。由於我加載的位圖是TIFF,它們是RLE8編碼的。

+0

如果你有任何形式的壓縮,那麼你不能依賴於一個常量的步伐。 –

+3

您在1bpp圖像上沒有RLE8壓縮,它僅在8bpp圖像上使用。步幅是以字節爲單位,而不是位,簡單地解釋了爲什麼它小於1bpp圖像的寬度。 –

相關問題