none
Create Central Gradient Brush RRS feed

  • Question

  • Hi,

    I have tried to create a central gradient brush by code on a label, but I get something different from which what I want.

    Here is the code I have written:

    Option Strict On
    
    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    
    Public Class Form1
    
        Private Sub Label1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
    
            Dim H As Integer = Label1.ClientRectangle.Height
            Dim W As Integer = Label1.ClientRectangle.Width
    
            Dim Pt(3) As Point
            Pt(0) = New Point(0, 0)
            Pt(1) = New Point(W, 0)
            Pt(2) = New Point(W, H)
            Pt(3) = New Point(0, H)
    
            Dim PGB As New PathGradientBrush(Pt)
            PGB.CenterColor = Color.White
            PGB.SurroundColors = New Color() {Color.Blue}
    
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
            e.Graphics.FillEllipse(PGB, 3, 3, W - 6, H - 6)
            e.Graphics.DrawEllipse(New Pen(Color.Black, 5), 3, 3, W - 6, H - 6)
    
        End Sub
    
    End Class
    

    What should I do?

    I know that I can use the Oval Shape control, but I need to draw on the label.

    Thanks to any ideas.

    Sunday, September 29, 2013 8:56 AM

Answers

  • This should fix it.

    Private Sub Label1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
    
            Dim H As Integer = Label1.ClientRectangle.Height
            Dim W As Integer = Label1.ClientRectangle.Width
    
            Dim gp As New GraphicsPath()
            gp.AddEllipse(Label1.ClientRectangle)
    
            Dim PGB As New PathGradientBrush(gp)
            PGB.CenterColor = Color.White
            PGB.SurroundColors = New Color() {Color.Blue}
    
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
            e.Graphics.FillEllipse(PGB, 3, 3, W - 6, H - 6)
            e.Graphics.DrawEllipse(New Pen(Color.Black, 5), 3, 3, W - 6, H - 6)
    
            PGB.Dispose()
            gp.Dispose()
    
    
        End Sub


    Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by BGQQ Sunday, September 29, 2013 10:07 AM
    Sunday, September 29, 2013 9:56 AM

All replies

  • This should fix it.

    Private Sub Label1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
    
            Dim H As Integer = Label1.ClientRectangle.Height
            Dim W As Integer = Label1.ClientRectangle.Width
    
            Dim gp As New GraphicsPath()
            gp.AddEllipse(Label1.ClientRectangle)
    
            Dim PGB As New PathGradientBrush(gp)
            PGB.CenterColor = Color.White
            PGB.SurroundColors = New Color() {Color.Blue}
    
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
            e.Graphics.FillEllipse(PGB, 3, 3, W - 6, H - 6)
            e.Graphics.DrawEllipse(New Pen(Color.Black, 5), 3, 3, W - 6, H - 6)
    
            PGB.Dispose()
            gp.Dispose()
    
    
        End Sub


    Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by BGQQ Sunday, September 29, 2013 10:07 AM
    Sunday, September 29, 2013 9:56 AM
  • It works, thank you!
    Sunday, September 29, 2013 10:07 AM