Graphics.DrawImage 会无意中修剪图像吗?
我使用的代码接受位图并将其转换为 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);
}
我对上面的代码有两个问题:
- 对于特定的图像,我认为 这剪掉了最右边的 200 tempImage2 右侧的像素。 是 这可能吗? 这怎么可能 发生了,我该如何阻止它? A 我的朋友建议说 可能与步幅有关 正在使用的 TIFF 文件。
- 有没有 将图像转换为 24 的更快方法 内存中的BPP?
I'm using code that takes a bitmap and converts it to 24 BPP so that I can use it in a program that specifically requires that file format. Here is the code:
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);
}
I have two questions about the code above:
- With a particular image, I think
that this clipped the rightmost 200
pixels right off of tempImage2. Is
this possible? How might this
happen, and how can I stop it? A
friend of mine suggested that it
might have to do with the stride of
the TIFF file being used. - Is there a
faster way to convert an image to 24
BPP in memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更好的方法是使用 Bitmap.Clone 方法。 这需要 PixelFormat 作为参数:
A better way is to use the Bitmap.Clone method. This takes a PixelFormat as a parameter: