2009-01-15 85 views
0

我使用的代碼需要一個位圖並將其轉換爲24位BPP,以便我可以在專門需要該文件格式的程序中使用它。下面是代碼:Can Graphics.DrawImage是否無意裁剪圖像?

using (Bitmap tempImage = new Bitmap(pageToScan.FullPath)) 
    { 
     if (tempImage.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb) 
     { 
      using (Bitmap tempImage2 = new Bitmap(tempImage.Size.Width, tempImage.Size.Height, 
      System.Drawing.Imaging.PixelFormat.Format24bppRgb)) 
      { 
      using (Graphics g = Graphics.FromImage(tempImage2)) 
      { 
       g.DrawImage(tempImage, new Point(0, 0)); 
      } 
      RecognizeBitmap(pageToScan, tempImage2); //Thanks to Tim on this refactoring. 
      } 
     } 
     else 
      RecognizeBitmap(pageToScan, tempImage); 
    } 

我對上面的代碼中的兩個問題:

  1. 與特定的形象,我覺得 這刮到了最右邊的200個 像素馬上tempImage2的。 這可能嗎? 怎麼可能發生,我該如何阻止它?我的一位朋友 建議它 可能與正在使用的TIFF文件的步幅 有關。
  2. 是否有 更快地將圖像轉換爲24 BPP在內存的方式?

回答

1

更好的方法是使用Bitmap.Clone方法。這需要PixelFormat作爲參數:

using (Bitmap tempImage = new Bitmap(pageToScan.FullPath))  
{   
    if (tempImage.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb) 
    { 
     Rectangle r = new Rectangle(0, 0, tempImage.Width, tempImage.Height); 
     RecognizeBitmap(pageToScan, tempImage.Clone(r, PixelFormat.Format24bppRgb);   
    } 
    else     
    { 
     RecognizeBitmap(pageToScan, tempImage);  
    } 
} 
+0

很酷。我應該在tempImage.Clone周圍使用嗎? – Chris 2009-01-19 15:56:27