最終更新:2020-04-21 (火) 14:16:42 (1459d)  

System.Drawing
Top / System.Drawing

.NET Framework/クラスライブラリ

クラス

  • System.Drawing.Pen?
  • System.Drawing.Brush
  • System.Drawing.LinearGradientBrush?
  • System.Drawing.PathGradientBrush?
  • System.Drawing.TextureBrush?
  • System.Drawing.GraphicsPath?

何かを描画したいとき

  • OnPaint?PaintEventArgs eのGraphicsに描画してやると良い。
  • でないと再描画のタイミングで消えてしまう。
Private Sub PictureBox1_Paint(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.PaintEventArgs) _
    Handles PictureBox1.Paint
    '画像を表示する
    If Not (bmp Is Nothing) Then
        e.Graphics.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height)
    End If
End Sub
 
  • ImageGraphicsで描画処理を行い、最終的にGraphics.DrawImage?で描画
Dim img As New Bitmap(200, 100)
'imgのGraphicsオブジェクトを取得
Dim g As Graphics = Graphics.FromImage(img)

'Graphicsを使って白に塗りつぶす
g.FillRectangle(Brushes.White, g.VisibleClipBounds)
'直接Imageのピクセルを指定して描画
img.SetPixel(rnd.Next(img.Width), rnd.Next(img.Height), col)

'作成した画像を表示する
Dim pg As Graphics = PictureBox1.CreateGraphics()
pg.DrawImage(img, g.VisibleClipBounds)
 

Graphicsを用いた描画

  • Control.CreateGraphics をつかう
    g=PictureBox1.CreateGraphics()
    'ここで描画処理
    g.Clear(Color.White)
    g.Dispose
  • Bitmapオブジェクトから
    'bmpに描画してからImageにセット
    bmp=new Bitmap(w,h)
    g=Graphics.FromImage(bmp)
    'ここで描画処理
    g.Clear(Color.White)
    g.Dispose
    PictureBox1.Image=bmp
  • Imageプロパティから
    'Imageに直接描画
    PictureBox.Image=new Bitmap(w,h)
    g=Graphics.FromImage(PicruteBox1.Image)
    'ここで描画処理
    g.Clear(Color.White)
    g.Dispose
    PicruteBox1.Invalidate()'再描画してやる