0

フォームが画面に移動したときにグラフィックを無効化メソッドで再描画するのに苦労していますグラフィックが再描画されません無効化メソッドで再描画するためにグラフィックスから画像を作成しようとしましたが、そうではありません私はまだ初心者なので、誰かがコードを編集して簡単な言葉でそれを行うのを手伝ってくれませんか

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

        }
4

1 に答える 1

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));
}
于 2013-10-19T18:27:48.857 に答える