缩放和打印 Bitmap 对象

发布于 2024-12-20 10:29:58 字数 1151 浏览 3 评论 0原文

我有一个通过使用 DrawToBitmap 方法绘制多个控件而创建的 Bitmap 对象。我现在想打印位图。但是,位图太大,无法放在单个页面上,因此必须按比例缩小。我尝试使用以下 DrawImage 重载来实现此目的:

public void PrintPageHandler(object sender, PrintPageEventArgs e)
{
    Bitmap bitmap = GetBitmap();
    Rectangle destRect = new Rectangle(
        e.MarginBounds.X,
        e.MarginBounds.Y,
        e.MarginBounds.Width,
        e.MarginBounds.Width * bitmap.Height / bitmap.Width);
    e.Graphics.DrawImage(
                     bitmap,
                     destRect,
                     0,
                     0,
                     bitmap.Width,
                     bitmap.Height,
                     System.Drawing.GraphicsUnit.Pixel);
}

请注意,destRect 的宽度和高度是这样构造的,因为位图的宽度远大于高度(即宽度始终是限制尺寸)。

我的问题是打印时图像最终变得非常模糊。我是否错误地缩放了这个?我感觉 e.MarginBounds 和图像尺寸之间的 GraphicsUnit 不匹配可能存在一些问题。任何帮助将不胜感激。

[更新]

我尝试使用下面评论中给出的方法调整位图大小,但图像打印仍然模糊。为了进行测试,我将原始位图和调整大小的位图保存到文件中,在 Windows 照片查看器中打开它们,并尝试从那里打印它们。调整大小后的图像打印起来很模糊,就像在我的 c# 应用程序中一样,但原始图像打印得很漂亮;无论 Windows 照片查看器使用什么算法将大小调整为单个页面,都不会导致图像变得模糊。

我想知道,Windows 照片查看器在调整打印大小时是否会增加像素密度?也许这就是为什么在代码中调整它的大小会导致它变得模糊;原始像素密度不足以清晰显示缩小后的图像。

I have a Bitmap object created by drawing several controls with the DrawToBitmap method. I would now like to print the bitmap. However, the bitmap is too large to fit on a single page and so it must be scaled down. I'm trying to do that using the following overload of DrawImage:

public void PrintPageHandler(object sender, PrintPageEventArgs e)
{
    Bitmap bitmap = GetBitmap();
    Rectangle destRect = new Rectangle(
        e.MarginBounds.X,
        e.MarginBounds.Y,
        e.MarginBounds.Width,
        e.MarginBounds.Width * bitmap.Height / bitmap.Width);
    e.Graphics.DrawImage(
                     bitmap,
                     destRect,
                     0,
                     0,
                     bitmap.Width,
                     bitmap.Height,
                     System.Drawing.GraphicsUnit.Pixel);
}

Note that the destRect width and height are constructed like this because the bitmap is much wider than it is tall (i.e. width is always the limiting dimension).

My problem is that the image ends up being very blurry when it's printed. Am I scaling this incorrectly? I have a feeling there may be some issue with a GraphicsUnit mismatch between e.MarginBounds and the image dimensions. Any help would be appreciated.

[UPDATE]

I tried resizing the bitmap using the method given in the comment below, but the image still prints blurry. For testing, I saved both the original and resized bitmap to files, opened them in Windows Photo Viewer, and tried to print them from there. The resized image prints blurry like it does from within my c# application, but the original image prints beautifully; whatever algorithm Windows Photo Viewer uses to resize to a single page did not cause the image to get blurred.

I wonder, could Windows Photo Viewer be increasing the pixel density when it resizes for printing? Maybe that's why resizing it in code is causing it to get blurred; the origin pixel density is insufficient to display the scaled down image clearly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻想少年梦 2024-12-27 10:29:58

看起来您没有保留纵横比。您需要计算原始图像的宽度与高度的比率,并确保缩放输出图像,使其尺寸具有相同的比率。

基本上:
1 - 计算纵横比。
2 - 找到目标尺寸的最大尺寸。
3 - 调整输出大小以使最大尺寸匹配,并将较小尺寸设置为较大尺寸乘以比率。

编辑
检查graphics.dpiX和.DpiY属性,看看您的打印机在水平方向和垂直方向上是否有不同的DPI。如果它们不同,您将必须对尺寸进行一些额外的调整。

It doesn't look like you are preserving the aspect ratio. You need to calculate the ratio of the width to height of the original image and make sure to scale the output image so that it's dimensions have the same ratio.

Basically:
1 - Calculate the aspect ratio.
2 - Find the largest dimension of the target size.
3 - Resize the output so that the largest dimensions matches, and set the smaller dimension to the larger one multiplied by the ratio.

EDIT
Check the graphics.dpiX and .DpiY proeprties to see if your printer has a different DPI going horizontally from vertically. If they are different you will have to apply some additional adjustments to the dimensions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文