【发布时间】:2019-09-07 00:06:44
【问题描述】:
我不知道如何重新绘制我的图片框。这只是一个演示。生产代码太耗时,无法放入绘制事件。我需要一种方法来捕获使用图形方法在 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;
}
}
【问题讨论】:
-
g = pictureBox1.CreateGraphics();- Winforms 图形基本规则#1:永远不要使用control.CreateGraphics!永远不要尝试缓存Graphics对象!使用Graphics g = Graphics.FromImage(bmp)或在控件的Paint事件中使用e.Graphics参数绘制Bitmap bmp987654331@ 您绘制的控件。所有绘图都应该在Paint事件中,在那里使用e.Graphics! - 另外:你的 Paint 事件代码没用。 -
..((缓存当然是使用 DrawToBitmap 或使用位图中的 Graphics 对象))
-
PrintPageEventArgs 的使用很奇怪。