dcsimg
Results 1 to 3 of 3

Thread: Need help adding png to custom groupbox

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2016
    Location
    UK
    Posts
    42

    Need help adding png to custom groupbox

    I i modified someone elses code for a custom group box to add a second rounded box with text on the bottom. Now ive been trying to add a png image inside the bottom box but i cant get it to work.

    The original code is here https://code.msdn.microsoft.com/wind...Class-96b56b33

    This is what im after
    Attachment 149457

    heres the main sections of code ive changed trying to get it to work
    Code:
        Private TextBottom As String
        Private _StatusImage As Image
    
        Public Property Text_Bottom() As String
            Get
                Return TextBottom
            End Get
            Set(ByVal value As String)
                TextBottom = value
            End Set
        End Property
    
        <Category("Appearance"), Description("Gets or sets the Background image.")> <Browsable(True)>
        Public Property StatusImage() As Image
            Get
                Return _StatusImage
            End Get
            Set(ByVal value As Image)
                _StatusImage = value
                Me.Refresh()
            End Set
        End Property
    
        'Used to paint the control according to how the properties are set 
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            With e.Graphics
                .FillRectangle(_BackColorBrush, 0, 0, Me.Width, Me.Height)
                .SmoothingMode = SmoothingMode.AntiAlias
    
                Dim textWidth As Integer = CInt(.MeasureString(Me.Text, Me.Font).Width)
                Dim textHeight As Integer = CInt(.MeasureString(Me.Text, Me.Font).Height)
    
                Dim rec As New Rectangle(0, CInt(textHeight / 2), Me.Width - 1, Me.Height - 1 - CInt(textHeight))
    
                Using gfxPath As New GraphicsPath
                    If Me.GroupPanelShape = PanelType.Rounded Then
                        Dim rad As Integer = 14
                        gfxPath.AddArc(rec.Right - (rad), rec.Y, rad, rad, 270, 90)
                        gfxPath.AddArc(rec.Right - (rad), rec.Bottom - (rad), rad, rad, 0, 90)
                        gfxPath.AddArc(rec.X, rec.Bottom - (rad), rad, rad, 90, 90)
                        gfxPath.AddArc(rec.X, rec.Y, rad, rad, 180, 90)
                        gfxPath.CloseFigure()
                    Else
                        gfxPath.AddRectangle(rec)
                    End If
    
                    .FillPath(_PanelBrush, gfxPath)
    
                    If Me.BackgroundPanelImage IsNot Nothing Then
                        DrawBackImage(e.Graphics, gfxPath, CInt(textHeight / 2))
                    End If
    
                    If Me.DrawGroupBorder Then .DrawPath(_BorderPen, gfxPath)
                End Using
    
    
                If textWidth > 0 And textHeight > 0 Then
    
                    Dim trec As New Rectangle(CInt(Me.Width / 16), 0, Me.Width - 15 - CInt(Me.Width / 16), textHeight + 2)
    
                    Using gfxPath As New GraphicsPath
                        Dim rad As Integer = 6
                        gfxPath.AddArc(trec.Right - (rad), trec.Y, rad, rad, 270, 90)
                        gfxPath.AddArc(trec.Right - (rad), trec.Bottom - (rad), rad, rad, 0, 90)
                        gfxPath.AddArc(trec.X, trec.Bottom - (rad), rad, rad, 90, 90)
                        gfxPath.AddArc(trec.X, trec.Y, rad, rad, 180, 90)
                        gfxPath.CloseFigure()
                        .FillPath(_TextBackBrush, gfxPath)
                        .DrawPath(_TextBorderPen, gfxPath)
    
                    End Using
    
                    Using sf As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center, .Trimming = StringTrimming.EllipsisCharacter, .FormatFlags = StringFormatFlags.NoWrap}
                        .DrawString(Me.Text, Me.Font, _TextBrush, trec, sf)
                    End Using
    
    
                    ' <------------------------------------------my stuff
                    ' adds the bottom box
                    trec = New Rectangle((Me.Width / 16), Me.Height - (textHeight + 3), Me.Width - 15 - (Me.Width / 16), textHeight + 2)
    
                    Using gfxPath As New GraphicsPath
                        Dim rad As Integer = 6
                        gfxPath.AddArc(trec.Right - (rad), trec.Y, rad, rad, 270, 90)
                        gfxPath.AddArc(trec.Right - (rad), trec.Bottom - (rad), rad, rad, 0, 90)
                        gfxPath.AddArc(trec.X, trec.Bottom - (rad), rad, rad, 90, 90)
                        gfxPath.AddArc(trec.X, trec.Y, rad, rad, 180, 90)
                        gfxPath.CloseFigure()
                        .FillPath(_TextBackBrush, gfxPath)
                        .DrawPath(_TextBorderPen, gfxPath)
    
                    End Using
    
                    Using sf As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center, .Trimming = StringTrimming.EllipsisCharacter, .FormatFlags = StringFormatFlags.NoWrap}
                        .DrawString(Me.TextBottom, Me.Font, _TextBrush, trec, sf)
                    End Using
    
                End If
    
                ' trying to add the png
    
                If Me._StatusImage IsNot Nothing Then
                    Dim pngrec As New Rectangle(50, 50, 22, 22)
                    Using gfxPath As New GraphicsPath
    
                        gfxPath.AddRectangle(pngrec)
                        DrawSmallPng(e.Graphics, gfxPath, 16)
    
                    End Using
                End If
    
            End With
        End Sub
    
        Private Sub DrawSmallPng(ByVal g As Graphics, ByVal grxpath As GraphicsPath, ByVal topoffset As Integer)
    
            Using bm As New Bitmap(StatusImage.Width, StatusImage.Height)
                Using grx As Graphics = Graphics.FromImage(bm)
                    grx.DrawImage(Me.StatusImage, CInt(Me.Width / 2), CInt(Me.Height / 2), 16, 16)
                End Using
    
    
            End Using
    
        End Sub
    
        ' <------------------------------------------my stuff end
    Can anyone please advise me on whats going wrong.

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Need help adding png to custom groupbox

    Your code in DrawSmallPng creates a new, blank bitmap the same size as the image, then draws the status image on top of that blank bitmap. It doesn't draw to the screen.

    It helps to name your Graphics objects after what they are drawing to so you can keep track of them:
    Code:
    Private Sub DrawSmallPng(ByVal screenGraphics As Graphics) 
        screenGraphics.DrawImage(StatusImage, CInt(Me.Width / 2), CInt(Me.Height / 2), 16, 16)
    End Sub
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2016
    Location
    UK
    Posts
    42

    Re: Need help adding png to custom groupbox

    Thanks Sitten Spynne

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width