首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用打印预览打印分组框?

如何使用打印预览打印分组框?
EN

Stack Overflow用户
提问于 2020-01-11 18:27:10
回答 1查看 454关注 0票数 0

下面的代码在打印预览中显示了分组框的一半:

代码语言:javascript
复制
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
  //  Bitmap bmp = new Bitmap(groupBox1.ClientRectangle.Width, groupBox1.ClientRectangle.Height);
  //  groupBox1.DrawToBitmap(bmp, groupBox1.ClientRectangle);
  //  e.Graphics.DrawImage(bmp, 0, 0);

  Bitmap bmp = new Bitmap(groupBox1.Width, groupBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  groupBox1.DrawToBitmap(bmp, new Rectangle(0, 0, groupBox1.Width, groupBox1.Height));
  e.Graphics.DrawImage((Image) bmp, 0, 0);
}

private void button1_Click(object sender, EventArgs e) {
  PrintPreviewDialog ppd = new PrintPreviewDialog();
  PrintDocument Pd = new PrintDocument();

  Pd.PrintPage += this.printDocument1_PrintPage;
  ppd.Document = Pd;
  ppd.ShowDialog();
}
EN

回答 1

Stack Overflow用户

发布于 2020-01-12 00:40:56

你需要在页面的边界内进行绘制。

代码语言:javascript
复制
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    var g = e.Graphics;
    var srcRect = new Rectangle(0, 0, groupBox1.Width, groupBox1.Height);
    var desRect = new Rectangle(e.PageBounds.X, e.PageBounds.Y, e.PageBounds.Width, srcRect.Height);
    //Or to draw within the margin
    //var desRect = new Rectangle(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Width, srcRect.Height);

    using (var bmp = new Bitmap(srcRect.Width, srcRect.Height))
    {
        groupBox1.DrawToBitmap(bmp, srcRect);
        g.DrawImage(bmp, desRect, srcRect, GraphicsUnit.Pixel);
    }
}

此外,创建PrintDocument对象并在窗体的构造函数或Load事件中注册其PrintPage事件,以避免在button1_Click事件中重复此操作。别忘了丢弃一次性物品。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59693557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档