none
PICTUREBOX ROUND CORNER RRS feed

  • Question

  • HI

    I used this class
    To ROUND CORNER OF PICTUREBOX
    But the problem appears poorly when you place a background image

    Public Class RoundPictureBox
        Inherits PictureBox
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    
            Dim rect As Rectangle = MyBase.ClientRectangle
            Dim g As Graphics = e.Graphics
            Dim br As New SolidBrush(MyBase.BackColor)
            g.FillRectangle(br, rect)
            MyBase.OnPaint(e)
    
            Dim CtrlPen, LightPen, DarkPen As Pen
            CtrlPen = SystemPens.ControlDark
    
       
                LightPen = SystemPens.ControlLightLight
                DarkPen = SystemPens.ControlDarkDark
    
            Dim width As Int32 = 12
            Dim height As Int32 = 12
            Dim x As Int32 = 0
            Dim y As Int32 = 0
            Dim startAngle As Int32 = 270
            Dim sweepAngle As Int32 = -90
            g.DrawArc(LightPen, x, y, width, height - 1, startAngle, sweepAngle)
    
            x = rect.Width - width
            y = 0
            startAngle = 270
            sweepAngle = 90
            g.DrawArc(CtrlPen, x, y, width - 1, height, startAngle, sweepAngle)
    
            x = rect.Width - width
            y = rect.Height - height
            startAngle = 0
            sweepAngle = 90
            g.DrawArc(DarkPen, x, y, width - 1, height - 1, startAngle, sweepAngle)
    
            x = 0
            y = rect.Height - height
            startAngle = 90
            sweepAngle = 90
            g.DrawArc(CtrlPen, x, y, width, height - 1, startAngle, sweepAngle)
    
            g.DrawLine(LightPen, (width \ 2) - 1, 0, rect.Right - (width \ 2), 0)
            g.DrawLine(DarkPen, rect.Right - 1, (height \ 2), rect.Right - 1, rect.Bottom - (height \ 2) - 1)
            g.DrawLine(DarkPen, (width \ 2) - 1, rect.Bottom - 1, rect.Right - (width \ 2), rect.Bottom - 1)
            g.DrawLine(LightPen, 0, (height \ 2), 0, rect.Bottom - (height \ 2) - 1)
    
        End Sub
    
        Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
            MyBase.Invalidate(MyBase.ClientRectangle)
        End Sub
    End Class

    Saturday, September 22, 2018 6:20 AM

Answers

  • Hi

    Here is some code I had experimented with a long time ago. The code is a mixture from various snippets found on the Web (as far as I remember).

    This creates a new control called RoundedPB, which Inherits from a PictureBox and add some functionality for rounding corners, border color and border width.

    To add a new control in the Designer, you would first need to ReBuild the code to put the RoundedPB into the browsable control list in the ToolBox.

    I haven't played around much with this code, but from this example, seems it is mostly working.

    Option Strict On
    Option Explicit On
    Imports System.ComponentModel
    Imports System.Drawing.Drawing2D
    
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' RoundedPB1 added from ToolBox 
        ' in Designer
        With RoundedPB1
          .Edge = 100
          .penWidth = 5.0F
          .BorderColor = Color.Red
          .BackColor = Color.LightBlue
          .SizeMode = PictureBoxSizeMode.Zoom
    
          '  ' comment out if static
          '  ' PB wanted
          .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
    
          .Image = Image.FromFile("C:\Users\lesha\Desktop\Plans\Dice 2\62.png")
        End With
    
      End Sub
    End Class
    
    Public Class RoundedPB
      Inherits PictureBox
    
      Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)
        ExtendedDraw(e)
        DrawSingleBorder(e.Graphics)
      End Sub
    
      Private _penWidth As Single = 4.0F
      <Browsable(True)>
      Public Property penWidth() As Single
        Get
          Return _penWidth
        End Get
        Set(ByVal Value As Single)
          _penWidth = Value
          Invalidate()
        End Set
      End Property
    
      Private _borderColor As Color
      <Browsable(True)>
      Public Property BorderColor() As Color
        Get
          Return _borderColor
        End Get
        Set(ByVal Value As Color)
          _borderColor = Value
          Invalidate()
        End Set
      End Property
    
      Private _edge As Integer = 50
      <Browsable(True)>
      Public Property Edge() As Integer
        Get
          Return _edge
        End Get
        Set(ByVal Value As Integer)
          _edge = Value
          Invalidate()
        End Set
      End Property
    
      Private Function GetLeftUpper(ByVal e As Integer) As Rectangle
        Return New Rectangle(0, 0, e, e)
      End Function
    
      Private Function GetRightUpper(ByVal e As Integer) As Rectangle
        Return New Rectangle(Width - e, 0, e, e)
      End Function
    
      Private Function GetRightLower(ByVal e As Integer) As Rectangle
        Return New Rectangle(Width - e, Height - e, e, e)
      End Function
    
      Private Function GetLeftLower(ByVal e As Integer) As Rectangle
        Return New Rectangle(0, Height - e, e, e)
      End Function
    
      Private Sub ExtendedDraw(ByVal e As PaintEventArgs)
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
        Dim path As GraphicsPath = New GraphicsPath()
        path.StartFigure()
        path.StartFigure()
        path.AddArc(GetLeftUpper(Edge), 180, 90)
        path.AddLine(Edge, 0, Width - Edge, 0)
        path.AddArc(GetRightUpper(Edge), 270, 90)
        path.AddLine(Width, Edge, Width, Height - Edge)
        path.AddArc(GetRightLower(Edge), 0, 90)
        path.AddLine(Width - Edge, Height, Edge, Height)
        path.AddArc(GetLeftLower(Edge), 90, 90)
        path.AddLine(0, Height - Edge, 0, Edge)
        path.CloseFigure()
        Region = New Region(path)
      End Sub
    
      Private Sub DrawSingleBorder(ByVal graphics As Graphics)
        Dim pen As New Pen(BorderColor, penWidth)
        graphics.DrawArc(pen, New Rectangle(0, 0, Edge, Edge),
                         180, 90)
        graphics.DrawArc(pen, New Rectangle(Width - Edge - 1, -1,
                         Edge, Edge), 270, 90)
        graphics.DrawArc(pen, New Rectangle(Width - Edge - 1,
                         Height - Edge - 1, Edge, Edge), 0, 90)
        graphics.DrawArc(pen, New Rectangle(0, Height - Edge - 1,
                         Edge, Edge), 90, 90)
        graphics.DrawRectangle(pen, 0.0F, 0.0F, CType((Width - 1),
                               Single), CType((Height - 1), Single))
      End Sub
    End Class
    
    


    Regards Les, Livingston, Scotland

    • Marked as answer by ahmeddc Sunday, September 23, 2018 6:54 PM
    Saturday, September 22, 2018 3:16 PM