我有一个UserControl,这是override Paint()与我自己的图纸。我希望允许用户打印它。
因为我已经花了很多时间编写public void Draw(Graphics e),所以我希望重用这个方法,只传递PrintEventArgs.Graphics。我意识到这并不是那么简单。我甚至还得自己呼叫它。
有没有像OpenGL "Projection Matrix“这样的东西可以用来计算”最佳拟合“或"100%比例”类型的打印特征?
发布于 2012-08-28 00:38:39
Graphics对象具有矩阵类型的Transform属性,该属性可用于缩放、旋转等绘制的图形,其方式与OpenGL矩阵非常相似。
发布于 2012-08-28 01:20:44
我会将用户绘图移到一个单独的方法中,如下所示:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Rectangle client=new Rectangle(0, 0, ClientSize.Width-1, ClientSize.Height-1);
Render(e.Graphics, client);
}
public void Render(Graphics g, Rectangle client)
{
g.DrawEllipse(Pens.Blue, client); //test draw
//...
}然后从打印文档中调用它:
private void button1_Click(object sender, EventArgs e)
{
PrintPreviewDialog dlg=new PrintPreviewDialog();
PrintDocument doc=new PrintDocument();
doc.PrintPage+=(s, pe) =>
{
userControl11.Render(pe.Graphics, pe.PageBounds); // user drawing
pe.HasMorePages=false;
};
doc.EndPrint+=(s, pe) => { dlg.Activate(); };
dlg.Document=doc;
dlg.Show();
}结果是:

编辑1以保持打印输出中的像素数不变,然后将打印例程修改为:
doc.PrintPage+=(s, pe) =>
{
Rectangle client = new Rectangle(
pe.PageBounds.Left,
pe.PageBounds.Top,
userControl11.ClientSize.Width-1,
userControl11.ClientSize.Height-1 );
userControl11.Render(pe.Graphics, client);
pe.HasMorePages=false;
};https://stackoverflow.com/questions/12145763
复制相似问题