我正在使用带位图的代码并将其转换为24 BPP,以便我可以在特定需要该文件格式的程序中使用它。这是代码:
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);
}
我对上面的代码有两个问题:
答案 0 :(得分: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);
}
}