c# - c# - 以高质量打印winform?
问题描述
这个问题可能听起来很熟悉,但我搜索了互联网并找不到解决方案。
我知道打印我们可以使用Crystal Report,但我放弃了这个想法,因为它有某些缺点。以下是一些缺点:-
- 需要在 PC 上安装,如果Visual Studio版本是 2017,那么您必须下载 200MB+ 安装程序。
- 如果您在一个部分中制作了某些对象,例如文本对象和线条,并且您必须在其中添加一些内容,那么您必须手动设置每个对象的位置,否则如果您集体选择所有对象并将其移动然后它们的原始位置和每个对象之间的间距会丢失。
- 目前我正在使用VS2008,它集成了水晶报表,它有一个众所周知的问题,即有时会更改每个文本对象的文本并出于某种原因在其中添加“i”。
我还尝试下载水晶报告的替代品。但是它的界面不是那么友好。
我正在选择的替代方案
现在我已经在 Windows 窗体上设计了我的报告。当我尝试打印时,与Crystal Reports打印相比,它的质量更差。这是它的代码
private System.IO.Stream streamToPrint;
string streamType;
private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = image.Width;
int height = image.Height;
if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
{
width = e.MarginBounds.Width;
height = image.Height * e.MarginBounds.Width / image.Width;
}
else
{
height = e.MarginBounds.Height;
width = image.Width * e.MarginBounds.Height / image.Height;
}
System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
}
private void btnPrint_Click(object sender, EventArgs e)
{
string fileName = Application.StartupPath + "\\PrintPage.jpg";
using (Graphics gfx = this.CreateGraphics())
{
using (Bitmap bmp = new Bitmap(this.Width, this.Height, gfx))
{
this.DrawToBitmap(bmp, new Rectangle(0, 0, this.Width, this.Height));
bmp.Save(fileName);
}
}
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StartPrint(fileStream, "Image");
fileStream.Close();
if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(fileName);
}
}
public void StartPrint(Stream streamToPrint, string streamType)
{
this.printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
this.streamToPrint = streamToPrint;
this.streamType = streamType;
System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
PrintDialog1.AllowSomePages = true;
PrintDialog1.ShowHelp = true;
PrintDialog1.Document = printDoc;
DialogResult result = PrintDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDoc.Print();
//docToPrint.Print();
}
}
private void Frm_Test_Shown(object sender, EventArgs e)
{
try
{
btnPrint_Click(sender, e);
}
catch (Exception ex) { clsUtility.ShowErrMsg(ex.Message); }
}
我理解它这样做的原因是因为图像和屏幕分辨率的问题(参考:https ://stackoverflow.com/a/12547806/2994869 )。人们提到的解决方法是将窗口窗体及其对象的大小增加 6 倍,但结果相同,但质量更差。
是否有任何解决方法或任何技巧可以打印 Windows 表单,以使质量接近 Crystal Report 的质量。
解决方案
为了获得高质量,您需要创建一个比屏幕分辨率更高的源,这是您可以从中获得的全部DrawToBitmap
。
仅当您可以大量放大单个控件时,使用DrawToBitmap
才会有所帮助。对于整个形式,它无济于事。
您将需要完全重新创建要打印的部件,并根据需要使用尽可能多的方法DrawString
和其他DrawXXX
方法。是的,很多工作。(但很有趣;-)
但是您显示的代码也因使用jpg
为软图像(即照片)创建的文件格式而出错。将其更改为png
并进行比较!
推荐阅读
- http - 如果我使用 io.TeeReader() 或 io.ReadAll(),我应该关闭请求正文吗?
- django - 限制租户访问基本 URL
- d3.js - 如何仅为d3多条图表中的最后一个条设置背景颜色
- types - F# 列表中的类型扩展名
- java - 为了使用 Java 多态性,我如何允许用户创建不同类型的对象?
- bash - 如何将变量放入 makefile 中的 $(shell) 命令中?
- azure-active-directory - Azure AD 使用逻辑应用获取成员
- r - 从右向左剪切一个字符串,直到遇到某个字符 R
- xcode - 如何修复 Xcode 问题 BUILD TARGET Runner OF PROJECT Runner WITH CONFIGURATION Debug
- php - 如果用户从单个类别购买“x”产品,则提供免费送货服务