[英]How do I repaint my picturebox when the picture disappears?
我不知道如何重新粉刷我的图片框。 这只是一个演示。 生产代码太耗时,无法放入绘制事件。 我需要的是一种使用图形方法捕获在 pucturebox 中绘制的图像的方法,以便我可以在需要时快速重新绘制它。
这是一个演示:
public partial class Form1 : Form
{
Image Outout;
public Form1()
{
InitializeComponent();
button1.Click += Button1_Click;
}
private void Button1_Click(Object sender, EventArgs e)
{
PrintPageEventArgs eOutput;
Graphics g;
string OutputText;
Font PrintFont;
OutputText = "CERTIFICATION";
PrintFont = new Font("Arial", 16, FontStyle.Bold);
g = pictureBox1.CreateGraphics();
eOutput = new PrintPageEventArgs(g, new Rectangle(new Point(25, 25), new Size(new Point(825, 1075))), new Rectangle(new Point(0, 0), new Size(new Point(850, 1100))), new PageSettings());
eOutput.Graphics.DrawString(OutputText, PrintFont, Brushes.Black, 0, 0);
Outout = pictureBox1.Image;
pictureBox1.Paint += PictureBox1_Paint;
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
pictureBox1.Image = Outout;
}
}
感谢 TaW 为我指明了正确的方向。 我发布的示例来自 ERP 系统,实际问题使用了十多个对象,图形来自多页报告。 所以在paint事件中绘制将不起作用。 对于初学者来说,保留要绘制的东西的列表是没有意义的,因为当零件编号发生变化时,所有东西都需要绘制。 此外,报告生成器需要运行两次,第一次只是为了计算页数,第二次是实际绘制页面。 此外,由于其局限性,我无法使用 PrintDocument 的 PrintPreview。 但是您知道如何使用Graphics g = Graphics.FromImage(bmp)
。 这就是我所需要的。
@LarsTech,您说得对,这是 PrintPageEventArgs 的一种奇怪用法。 当嵌入在几个事件和几个对象中的问题需要减少形式以呈现问题的表示时,这只是一个副作用。 如果它减少的太小,所提出的解决方案不能扩展,因此,不起作用。 如果没有减少到足够的程度,就很难理解实际问题,因为人们会针对不同的方面提出解决方案,其中一些是由于问题的减少而人为的。
回答
绘制由图片框创建的图形不是持久的。 然而,按照 TaW 的建议,在位图上绘图效果很好。 谢谢您的帮助!
PrintPageEventArgs eOutput;
Graphics g;
string OutputText;
Font PrintFont;
Bitmap Output;
OutputText = "CERTIFICATION";
PrintFont = new Font("Times New Roman", 24, FontStyle.Regular);
Output = new Bitmap(850, 1100);
g = Graphics.FromImage(Output);
eOutput = new PrintPageEventArgs(g, new Rectangle(new Point(25, 25), new Size(new Point(825, 1075))), new Rectangle(new Point(0, 0), new Size(new Point(850, 1100))), new PageSettings());
eOutput.Graphics.DrawString(OutputText, PrintFont, Brushes.Black, 0, 0);
pictureBox1.Image = Output;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.