首页 > 解决方案 > c# - 以高质量打印winform?

问题描述

这个问题可能听起来很熟悉,但我搜索了互联网并找不到解决方案。

我知道打印我们可以使用Crystal Report,但我放弃了这个想法,因为它有某些缺点。以下是一些缺点:-

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

我正在选择的替代方案

现在我已经在 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 的质量。

标签: c#.netprintingvisual-studio-2008crystal-reports

解决方案


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

仅当您可以大量放大单个控件时,使用DrawToBitmap才会有所帮助。对于整个形式,它无济于事。

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

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


推荐阅读