在C#中打印Win Form时如何提高打印质量
本文关键字:何提高 打印质量 Form Win 打印 | 更新日期: 2023-09-27 18:00:06
我正试图使用GDI打印我的表单,但当我打印它时,打印质量不是很好(不知道图像是否有别名?),表单大小是700x700,还有一个参数可以理解-光栅操作代码-,这里是代码正在使用。。。
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g1 = this.CreateGraphics();
System.Drawing.Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
Bitmap bmp = new Bitmap(MyImage);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = bmp.Width;
int height = bmp.Height;
if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
{
width = e.MarginBounds.Width;
height = bmp.Height * e.MarginBounds.Width / bmp.Width;
}
else
{
height = e.MarginBounds.Height;
width = bmp.Width * e.MarginBounds.Height / bmp.Height;
}
System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
e.Graphics.DrawImage(bmp, destRect, 0, 0, bmp.Width, bmp.Height, System.Drawing.GraphicsUnit.Pixel);
}
也许您对原始图像有问题。给我一个图片链接。检查图像大小。
尝试插入行g2.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
祝你好运!
结果被缩放和别名是正常的。与现代打印机的分辨率相比,源的像素太少。考虑使用WPF,它使用基于矢量的渲染,因此在缩放时不会出现丢失/失真。干杯