수색…


그래픽 객체 만들기

그래픽 객체를 만드는 세 가지 방법이 있습니다.

  1. 페인트 이벤트에서

컨트롤을 다시 그릴 때마다 (크기 조정, 새로 고침 ...)이 이벤트가 호출됩니다. 컨트롤에서 일관되게 컨트롤을 그리려면이 방법을 사용하십시오.

   'this will work on any object's paint event, not just the form
   Private Sub Form1_Paint(sender as Object, e as PaintEventArgs) Handles Me.Paint
     Dim gra as Graphics
     gra = e.Graphics
   End Sub
  1. 그래픽 만들기

이 방법은 컨트롤에 한 번만 그래픽을 만들려는 경우 또는 컨트롤에서 다시 그리기를 원하지 않는 경우에 주로 사용됩니다

   Dim btn as New Button
   Dim g As Graphics = btn.CreateGraphics
  1. 기존 그래픽에서

기존 그래픽을 그리고 변경하려는 경우이 방법을 사용하십시오.

   'The existing image can be from a filename, stream or Drawing.Graphic
   Dim image = New Bitmap("C:\TempBit.bmp")
   Dim gr As Graphics = Graphics.FromImage(image)

도형 그리기

펜 객체를 정의해야하는 도형 그리기를 시작하려면 Pen 두 개의 매개 변수가 허용됩니다.

  1. 펜 색상 또는 브러시
  2. 펜 너비

펜 객체는 그리려는 객체의 윤곽선 을 만드는 데 사용됩니다.

펜 정의 후 특정 펜 등록 정보를 설정할 수 있습니다

   Dim pens As New Pen(Color.Purple)
   pens.DashStyle = DashStyle.Dash 'pen will draw with a dashed line
   pens.EndCap = LineCap.ArrowAnchor 'the line will end in an arrow
   pens.StartCap = LineCap.Round 'The line draw will start rounded
   '*Notice* - the Start and End Caps will not show if you draw a closed shape

그런 다음 만든 그래픽 개체를 사용하여 도형을 그립니다.

  Private Sub GraphicForm_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
    Dim pen As New Pen(Color.Blue, 15) 'Use a blue pen with a width of 15
    Dim point1 As New Point(5, 15) 'starting point of the line
    Dim point2 As New Point(30, 100) 'ending point of the line
    e.Graphics.DrawLine(pen, point1, point2)

    e.Graphics.DrawRectangle(pen, 60, 90, 200, 300) 'draw an outline of the rectangle

기본적으로 펜의 너비는 1입니다.

    Dim pen2 as New Pen(Color.Orange) 'Use an orange pen with width of 1
    Dim origRect As New Rectangle(90, 30, 50, 60) 'Define bounds of arc
    e.Graphics.DrawArc(pen2, origRect, 20, 180) 'Draw arc in the rectangle bounds

End Sub

채우기 모양

Graphics.FillShapes는 모양을 그린 다음 지정된 색상으로 채 웁니다. 채우기 셰이프에서 사용할 수 있습니다.

  1. Brush 도구 - 단색으로 도형을 채 웁니다.

    Dim rect As New Rectangle(50, 50, 50, 50)
    e.Graphics.FillRectangle(Brushes.Green, rect) 'draws a rectangle that is filled with green
    
    e.Graphics.FillPie(Brushes.Silver, rect, 0, 180) 'draws a half circle that is filled with silver
    
  2. HatchBrush 도구 - 패턴으로 도형을 채 웁니다.

Dim hBrush As New HatchBrush(HatchStyle.ZigZag, Color.SkyBlue, Color.Gray)
'creates a HatchBrush Tool with a background color of blue, foreground color of gray, 
'and will fill with a zigzag pattern
Dim rectan As New Rectangle(100, 100, 100, 100)
e.Graphics.FillRectangle(hBrush, rectan)
  1. LinearGradientBrush - 그라디언트로 도형을 채 웁니다.

    Dim lBrush As New LinearGradientBrush(point1, point2, Color.MediumVioletRed, Color.PaleGreen)
     Dim rect As New Rectangle(50, 50, 200, 200)
     e.Graphics.FillRectangle(lBrush, rect)
    
  2. TextureBrush - 그림으로 도형을 채 웁니다.

리소스, 이미 정의 된 비트 맵 또는 파일 이름에서 그림을 선택할 수 있습니다.

   Dim textBrush As New TextureBrush(New Bitmap("C:\ColorPic.jpg"))
    Dim rect As New Rectangle(400, 400, 100, 100)
    e.Graphics.FillPie(textBrush, rect, 0, 360)

Hatch Brush ToolLinearGradientBrush 는 모두 다음 문을 가져옵니다. Imports System.Drawing.Drawing2D

본문

양식에 텍스트를 그리려면 DrawString 메서드를 사용합니다.

문자열을 그릴 때 위에 나열된 4 개의 브러시 중 하나를 사용할 수 있습니다

Dim lBrush As New LinearGradientBrush(point1, point2, Color.MediumVioletRed, Color.PaleGreen)
e.Graphics.DrawString("HELLO", New Font("Impact", 60, FontStyle.Bold), lBrush, New Point(40, 400))
'this will draw the word "Hello" at the given point, with a linearGradient Brush

텍스트의 폭이나 높이를 정의 할 수 없으므로 텍스트 Measure Text 사용하여 텍스트 크기를 확인하십시오

Dim lBrush As New LinearGradientBrush(point1, point2, Color.MediumVioletRed, Color.PaleGreen)
Dim TextSize =  e.Graphics.MeasureString("HELLO", New Font("Impact", 60, FontStyle.Bold), lBrush)
'Use the TextSize to determine where to place the string, or if the font needs to be smaller

예 : 양식 상단에 "Test"라는 단어를 그려야합니다. 폼의 너비는 120입니다. 폼 너비에 맞을 때까지이 루프를 사용하여 글꼴 크기를 줄입니다.

Dim FontSize as Integer = 80
Dim TextSize = e.graphics.measeString("Test", New Font("Impact",FontSize, FontStyle.Bold), new Brush(colors.Blue, 10)    
Do while TextSize.Width >120
FontSize = FontSize -1
TextSize = e.graphics.measeString("Test", New Font("Impact",FontSize, FontStyle.Bold), new Brush(colors.Blue, 10)  
Loop


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow