簡體   English   中英

如何使用invalidate方法強制重新繪制圖形

[英]How to force graphic to be redrawn with the invalidate method

我正在努力使表單在屏幕上消失時不能使用invalidate方法重繪該圖形,而該圖形沒有重繪,我試圖從該圖形中創建一個圖像,以使其使用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

        }

使用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));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM