我正在努力使用无效方法重新绘制图形当窗体进入屏幕时图形未重绘我试图从图形创建图像,以便使用invalidate方法重绘它但是它不起作用,有人可以帮助编辑代码,并以简单的方式做,因为我还是初学者,非常感谢
private void squareButton_Click(object sender, EventArgs e)
{
// Declaring a new graphics object has been assigned null
Graphics objGraphics = null;
// This will create the picture graphics to be drawn in the picturebox
objGraphics = PictureBox1.CreateGraphics();
// This will redraw the picture box with a fill chosen after the systemcolors
objGraphics.Clear(SystemColors.ControlDark);
// This will draw the rectangle with a red pen 10,10 represent position and 50,50 reprsent the width and height
objGraphics.DrawRectangle(Pens.Red, 10, 10, 50, 50);
// This will draw the rectangle
objGraphics.Dispose();
Bitmap imgbit = (Bitmap)Picturebox.Image;
Graphics.FromImage(imgbit);
picturebox.invalidate();
// This is not redrawing the graphic it just shows a blank form
}
答案 0 :(得分:0)
使用DrawToBitmap方法。
Bitmap bmp = new Bitmap(pb.Width, pb.Height);
pb.DrawToBitmap(bmp, pb.ClientRectangle);
bmp.Save({your path});
bmp.Dispose();
当您不想要持久绘图时,可以使用CreateGraphics
对象。如果这是一个按钮点击事件,它将在表面上绘制,但一旦系统想要重绘它就不会停留。
using (Graphics g = pb.CreateGraphics)
{
g.FillRectangle(Brushes.Blue, new Rectangle(20, 20, 20, 20));
g.DrawRectangle(Pens.Red, new Rectangle(20, 20, 20, 20));
}