Grafikobjekte mit GDI+

e.Graphics, FromHwnd, FromImage



Die verschiedenen Grafikobjekte von GDI+

Es gibt viele Wege um auf einem Formular eine Grafik zu zeichnen. Folgend stellen wir 4 verschiedene Arten mit unterschiedlichen Eigenschaften vor.

'Graphics Object with e Parameter
Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
  e.Graphics.FillRectangle(New
  SolidBrush(Color.Red), 80, 40, 20, 10)
End Sub
 
 
'Graphics Object from Windows Handle
Private Sub PictureBox2_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles
  PictureBox2.Paint
  Dim grfx As Graphics
  grfx = Graphics.FromHwnd(PictureBox2.Handle)
  grfx.FillRectangle(New SolidBrush(Color.Green), 80, 40, 20, 10)
End Sub
 
 
'Graphics Object from Image
Private Sub PictureBox3_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles
  PictureBox3.Paint
  Dim Grfx As Graphics
  Grfx = Graphics.FromImage(PictureBox3.Image)
  Grfx.FillRectangle(New SolidBrush(Color.Blue), 80, 40, 20, 10)
End Sub
 
 
'Graphics Object from Specified Handle to a Device
Private Sub PictureBox4_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles
  PictureBox4.Paint
  Dim Hdc As IntPtr = e.Graphics.GetHdc() New graphics
  Dim Grfx As Graphics = Graphics.FromHdc(Hdc)
  Grfx.FillRectangle(New SolidBrush(Color.Black), 80, 40, 20, 10)
 
  'Release
  e.Graphics.ReleaseHdc(Hdc)
End Sub