如何使用invalidate方法强制重画图形 [英] How to force graphic to be redrawn with the invalidate method
问题描述
当表格移到屏幕上时,我一直在努力使用invalidate方法重绘图形,但图形并未重绘,我试图从图形中创建图像,以便使用invalidate方法重绘该图形,但是它不起作用,有人可以帮我编辑代码并以简单的方式完成代码,因为我仍然是初学者,谢谢,谢谢
I am struggling with getting the graphic to be redrawn with the invalidate method when the form goes the screen the graphic isn't redrawn i have tried to create an image from the graphics for it to been redrawn with the invalidate method but it doesn't work could somebody help edit the code for and do it in simple terms as i am still a beginner thanks would be much appreciated
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
}
推荐答案
使用当您不需要永久性图形时,可以使用CreateGraphics
对象.如果这是在按钮单击事件中,它将在表面上绘制,但是一旦系统要重新绘制,它就不会停留.
You can use the CreateGraphics
object when your not wanting a persistent drawing. If this was in a button click event it will draw on the surface, but it will not stay once the system wants to redraw.
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));
}
这篇关于如何使用invalidate方法强制重画图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!