[英]c# - Print the winform in high quality?
This question may sound familiar but i have searched internet and couldn't find a solution to it. 这个问题听起来很耳熟,但是我已经搜索了互联网,却找不到解决方案。
I know for printing we can use Crystal Report but i am discarding that idea because of it's certain disadvantages. 我知道在打印时我们可以使用Crystal Report,但是由于某些缺点,我放弃了这个想法。 Here are some of the disadvantages:- 以下是一些缺点:
I also tried to download an alternate to crystal report . 我还尝试下载水晶报表的替代版本 。 But its interface is not that friendly. 但是它的界面不是那么友好。
Now i have designed my report on Windows Form. 现在,我已经在Windows Form上设计了我的报告。 When i am trying to print, its quality is worse as compared to Crystal Reports print. 当我尝试打印时,与Crystal Reports打印相比,其质量较差。 Here is the code for it 这是它的代码
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); }
}
I understand the reason why it is doing so because of the image and screen resolution thing (Ref : https://stackoverflow.com/a/12547806/2994869 ). 我了解这样做的原因是由于图像和屏幕分辨率的原因(请参阅: https : //stackoverflow.com/a/12547806/2994869 )。 The workaround people mentioned was to increase the size of windows form and it's object by 6 times but that had the same result still the quality is worse. 人们提到的解决方法是将Windows窗体及其对象的大小增加6倍,但结果相同,但质量仍然较差。
Is there any workaround or any trick that i can do to print a windows form so that quality is near to that of Crystal Report's. 我有什么变通办法或任何技巧可以打印Windows窗体,以便使质量接近Crystal Report的质量。
For high quality you need to create a source with higher resolution than the screen, which is all you can get from DrawToBitmap
. 为了获得高质量,您需要创建比屏幕分辨率更高的源,这是您可以从DrawToBitmap
获得的。
Using DrawToBitmap
will only help if you can enlarge a single control a lot. 仅当您可以将单个控件放大很多时,使用DrawToBitmap
才有用。 For the whole form it will not help. 对于整个表格,它无济于事。
You will need to fully re-create the parts you want to print and use as many DrawString
and other DrawXXX
methods as needed. 您将需要完全重新创建要打印的部件,并根据需要使用尽可能多的DrawString
和其他DrawXXX
方法。 Yes, a lot of work. 是的,很多工作。 (But fun ;-) (但是很有趣;-)
But the code you show also blunders by using a jpg
file format, which was created for soft images (ie photographs). 但是 ,您显示的代码也会使用jpg
文件格式而jpg
,该文件格式是为柔和的图像(即照片)创建的。 Change that to png
and compare! 将其更改为png
并进行比较!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.