【问题标题】:Save panel from winforms as pdf将winforms中的面板另存为pdf
【发布时间】:2021-05-07 16:59:22
【问题描述】:

有谁知道如何将我的面板保存为 pdf 格式? 现在我有了这段代码,但这会保存整个表单,我只需要 Panel1。

此代码返回整个表单的 pdf,但这不是必需的。 我已经在互联网上检查过,但找不到任何东西......

我还搜索了代码以查看是否可以找到指向表单的内容,但我也找不到任何内容。

谁能帮我保存我的面板而不是我的整个winform?

private System.IO.Stream streamToPrint;

    string streamType;

    [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    private static extern bool BitBlt
    (
        IntPtr hdcDest, // handle to destination DC
        int nXDest, // x-coord of destination upper-left corner
        int nYDest, // y-coord of destination upper-left corner
        int nWidth, // width of destination rectangle
        int nHeight, // height of destination rectangle
        IntPtr hdcSrc, // handle to source DC
        int nXSrc, // x-coordinate of source upper-left corner
        int nYSrc, // y-coordinate of source upper-left corner
        System.Int32 dwRop // raster operation code
    );
    private void printDocument1_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);
    }
    public void StartPrint(Stream streamToPrint, string streamType)
    {

        this.printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);

        this.streamToPrint = streamToPrint;

        this.streamType = streamType;

        System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();

        PrintDialog1.AllowSomePages = true;
        PrintDialog1.ShowHelp = true;
        PrintDialog1.Document = printDocument1;
        DialogResult result = PrintDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            printDocument1.Print();
        }
    }
    private void btnSave_Click(object sender, EventArgs e)
    {
        String filename = System.IO.Path.GetTempFileName();

        Graphics g1 = this.CreateGraphics();
        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);
        MyImage.Save(filename, ImageFormat.Png);
        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);
        }
    }

【问题讨论】:

  • 将 WinForm 控件转换为位图以供打印机输出通常是一个错误。屏幕和打印机设备的特性是如此不同,以至于在屏幕上起作用的东西在纸上不起作用。将数据存储在独立于设备的模型中。使用 WinForm API 绘制到屏幕,使用PrintDocument 方法绘制到打印机。你永远不应该使用CreateGraphicsGetHdc。多年来,Windows 一直提供足够的 PDF 驱动程序。

标签: c# .net visual-studio winforms


【解决方案1】:

@皇家空军。 打印面板为pdf,可以参考以下代码。

public partial class Form1 : Form
    {
        Bitmap MemoryImage;
        private PrintDocument printDocument1 = new PrintDocument();
        private PrintPreviewDialog previewdlg = new PrintPreviewDialog();

        public Form1()
        {
            InitializeComponent();
            
            printDocument1.PrintPage += new PrintPageEventHandler(printdoc1_PrintPage);

        }
        public void GetPrintArea(Panel pnl)
        {
            MemoryImage = new Bitmap(pnl.Width, pnl.Height);
            pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            if (MemoryImage != null)
            {
                e.Graphics.DrawImage(MemoryImage, 0, 0);
                base.OnPaint(e);
            }
        }
        void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
        {
            Rectangle pagearea = e.PageBounds;
            e.Graphics.DrawImage(MemoryImage, (pagearea.Width / 2) - (this.panel1.Width / 2), this.panel1.Location.Y);
        }
        public void Print(Panel pnl)
        {
            Panel pannel = pnl;
            GetPrintArea(pnl);
            previewdlg.Document = printDocument1;
            previewdlg.ShowDialog();
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            Print(this.panel1);
        }
    }

结果如图:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 2023-03-30
    • 2020-09-29
    相关资源
    最近更新 更多