繁体   English   中英

c#-以高质量打印winform?

[英]c# - Print the winform in high quality?

这个问题听起来很耳熟,但是我已经搜索了互联网,却找不到解决方案。

我知道在打印时我们可以使用Crystal Report,但是由于某些缺点,我放弃了这个想法。 以下是一些缺点:

  • 需要在PC上安装,如果Visual Studio版本是2017,则必须下载200MB +的安装程序。
  • 如果您在某一部分中制作了某些对象(例如textobjectline) ,并且必须在其中添加一些内容,则必须手动设置每个对象的位置,否则,如果您集体选择所有对象并移动它,则它们的原始位置和每个对象之间的间距会丢失。
  • 目前,我正在使用VS2008,并且其中集成了Crystal Report,这存在一个众所周知的问题, 出于某种原因有时会更改每个文本对象的文本并在其中添加“ i”

我还尝试下载水晶报表替代版本 但是它的界面不是那么友好。

我选择的替代

现在,我已经在Windows Form上设计了我的报告。 当我尝试打印时,与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 )。 人们提到的解决方法是将Windows窗体及其对象的大小增加6倍,但结果相同,但质量仍然较差。

我有什么变通办法或任何技巧可以打印Windows窗体,以便使质量接近Crystal Report的质量。

为了获得高质量,您需要创建比屏幕分辨率更高的源,这是您可以从DrawToBitmap获得的。

仅当您可以将单个控件放大很多时,使用DrawToBitmap才有用。 对于整个表格,它无济于事。

您将需要完全重新创建要打印的部件,并根据需要使用尽可能多的DrawString和其他DrawXXX方法。 是的,很多工作。 (但是很有趣;-)

但是 ,您显示的代码也会使用jpg文件格式而jpg ,该文件格式是为柔和的图像(即照片)创建的。 将其更改为png并进行比较!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM