Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone,

I'm adding an image to a tab control tabpage, which I have managed to to fine (this image will later be a close button). My question is how can I put some padding onto this image so that it looks better on the actual tab? My code is below:

VB
Private Sub tabControl_drawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
        Try
            Dim img As Image = New Bitmap("C:\close.png")
            Dim r As Rectangle = e.Bounds
            r = Me.TabControl.GetTabRect(e.Index)
            r.Offset(2, 2)
            Dim TitleBrush As Brush = New SolidBrush(Color.Black)
            Dim f As Font = Me.Font
            Dim Title As String = Me.TabControl.TabPages(e.Index).Text
            e.Graphics.DrawString(Title, f, TitleBrush, New PointF(r.X, r.Y))
            e.Graphics.DrawImage(img, New Point(r.X + (Me.TabControl.GetTabRect(e.Index).Width - _imageLocation.X), _imageLocation.Y))
            img..Padding = New System.Drawing.Point(10, 5)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
Posted
Comments
Sergey Alexandrovich Kryukov 20-Sep-12 12:49pm    
First, VB.NET is not a scripting language. There is not such thing as VB.NET script.
And even though I can guess this is System.Windows.Forms.TabControl, you should always specify exact type. There are more than one type named "TabControl".
--SA

There is no such thing as "padding" for System.Drawing.Graphics, so just add some shift to the coordinates of the image you already use. For consistency, this shift could be a member of the form using the control of a custom control class, perhaps. Yes, as simple as that.

—SA
 
Share this answer
 
Despite what Sergey says, it is possible. I'm creating a .gif and wanted all my images padded to maintain a constant width and height. Here I look through my array of images to go into the .gif and find the maximum height and width.

I then set my canvas to be the maximum height and width and colour all it's pixels white (there may well be a more efficient way of doing this rather than going pixel by pixel). If you want to get the pixel colour, you just use the GetPixel option. These are only available with bitmaps - not images.

I then get my x and y offsets so I can position the image in the centre of the canvas and then save this to my aryImages(i)._Image_Resized variable or you can save straight to file.

This may be a couple of years too late to help you but hopefully will help other people with the same issue.

VB
Dim MaxWidth As Integer = 0
Dim MaxHeight As Integer = 0

For i = 1 To aryImageCount
    If aryImages(i)._Image.Width > MaxWidth Then MaxWidth = aryImages(i)._Image.Width
    If aryImages(i)._Image.Height > MaxHeight Then MaxHeight = aryImages(i)._Image.Height
Next

Dim canvasWidth As Integer = MaxWidth
Dim canvasHeight As Integer = MaxHeight
Dim x_offset As Integer
Dim y_offset As Integer

For i = 1 To aryImageCount

    Dim Canvas As Bitmap = New Bitmap(canvasWidth, canvasHeight)
    Dim gfx As Graphics = Graphics.FromImage(Canvas)

    'Process the images pixels
    For y As Integer = 0 To canvasHeight - 1
        For x As Integer = 0 To canvasWidth - 1
            'Set this pixel's color
            Canvas.SetPixel(x, y, Color.White)
        Next
    Next

    x_offset = (MaxWidth - aryImages(i)._Image.Width) / 2
    y_offset = (MaxHeight - aryImages(i)._Image.Height) / 2

    gfx.DrawImage(aryImages(i)._Image, x_offset, y_offset, aryImages(i)._Image.Width, aryImages(i)._Image.Height)



    aryImages(i)._Image_Resized = New Bitmap(Canvas)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900