Multiple text labels in GDI+


Jun 8, 2004
Programming Experience
Hi all. Yay - first to post in graphics/GDI - unless someone posts before I finish this.....

I'm writing a program that I hope to replace the normal desktop with.
To do this I need to create icons with labels and I'm using an array of pictureboxes for the icons, but I can't make clear labels (there's picturebox on top of the form for the background pic (Can't remember why it's this way - I think I had trouble stretching the image on the form.... ) So I want to use GDI+ to do it. Trouble is I can't work out how to make more than one block of text appear..... Can anyone please explain if it's possible to do it, or if they have worked out a better way I'd love to hear it.

Instead of using pictureBoxs and labels, you could just draw both the icons and labels using GDI+ methods (Graphics.DrawImage and Graphics.DrawString). You just need to keep track of the positions of each. And I would suggest using double-buffering.
ahhh GDI

That sounds great, and alot simpler - trouble is I have no idea where to begin with GDI+ :(
If you could please post some example code it'd be great...


Here's some sample code showing off some of the things you can do with GDI+:
Private bmpIcon As Bitmap
Private bmpBackGnd As Bitmap
Private sf As New StringFormat()
Private recfText As RectangleF

Private Sub frmGDI_Load(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles MyBase.Load
    'these allow double buffering 
    'to stop flickering when resizing form
    Me.ResizeRedraw = True
    Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
    Me.SetStyle(ControlStyles.UserPaint, True)
    Me.SetStyle(ControlStyles.DoubleBuffer, True)
    'load an icon bitmap
    bmpIcon = New Bitmap("C:\IconPic.gif")
    'load the background bitmap
    bmpBackGnd = New Bitmap("C:\BackGnd.jpg")
    'setup the string format for drawing the text
    sf.Alignment = StringAlignment.Center
    sf.Trimming = StringTrimming.EllipsisCharacter
    'setup the rectangle to contain the text
    recfText = New RectangleF(10, 10 + bmpIcon.Height, bmpIcon.Width, Me.Font.Height * 2)
End Sub

Protected Overrides Sub OnPaint(ByVal e As _
    'draw icon bitmap at X=10, Y=10
    e.Graphics.DrawImage(bmpIcon, 10, 10)
    e.Graphics.DrawString("Long Icon text to show off the fancy " & _
                                  "trimming that can be done", _
            Me.Font, Brushes.White, recfText, sf)
End Sub

Protected Overrides Sub OnPaintBackground(ByVal pevent As _
    'draw the background, allowing the image to stretch with resize
    pevent.Graphics.DrawImage(bmpBackGnd, Me.ClientRectangle)
End Sub

The problem with doing it this way is capturing mouse clicks, though it can be done. Perhaps you could use a combination of pictureboxs and the DrawString method.
Thanks for your help, I've worked out a great system for the icons now. The only thing I'm needing to work on now is the highlighted icons (ie. mouse over). Instead of making a new bmp/png for everyicon when it's selected, I want to use the palette changing method. Trouble is, I can't make head nor tails of it. Feel like helping me out again? ;)

Thanks again,

I could give it a shot. Are you using pictureboxes or the DrawImage method to draw the bmp/png? How are you capturing the mouse events?

Maybe you could attach the project if you feel up to it.
If not just give an overview of how you decided to set this up.

I have a feeling it would be easier to use a matrix manipulation on the image. Or use the ImageAttributes parameter in one of the overloaded DrawImage methods along with the SetGamma method of the ImageAttribute (if you are using the DrawImage method).
Yeah, I am using the drawimage method. I created a class that holds all the info about an icon (image location, target, x and y pos's etc.) and then created an array class that keeps them all together, and just use for loops to draw each image. The mouseup even is used to get the x,y co-ords and check whether that's over one of the icons, then the last image area is invalidated and the form updated, so it redraws the last highlighted image. I am using two seperate images for each icon, and that seems like a total waste, so I went looking through the different drawing methods and saw palette and have been trying to work out how to use it, cos then I could just adjust the colours and alpha of the bitmap and redraw it then. What's that Matrix Manipulation thing you mentioned, sounds inteesting....
Here's a link to a good article that explains some typical image color adjustments using ColorMatrix and ImageAttributes classes: ColorMatrix Basics - Simple Image Color Adjustment

You can do some pretty cool things with the GDI+ classes fairly simply.

Can't wait for LongHorn and all the new graphics possibilities that will hopefully come with it. ;)
Hi there.

I will definitely look oldfashioned, bur I would prefer direct use of WinApi for this problem, specifically the region API plus perhaps the direct drawing by BitBlt and direct text output by DrawText. The reason is that the region API are well suited for detecting the mouse click, just call the PtnInRegion. Not a .NET solution exactly, but the overhead is extremely small.

VB C++ seo webdesign Php
.....and about anything else
Paszt said:
Here's a link to a good article that explains some typical image color adjustments using ColorMatrix and ImageAttributes classes: ColorMatrix Basics - Simple Image Color Adjustment

I checked that out.. it's a cool article. I just can't work out how the hell to use that damn thing. I played with it for about an hour and still can't manage to make it display the new coloured image :<
The code below increases the blue component of the image, kind of like the regular desktop does to the icon when selected:

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    Dim imgattr As New ImageAttributes()
    Dim rc As New Rectangle(0, 0, img.Width, img.Height)
    'create colormatrix
    Dim cm As New ColorMatrix(New Single()() _
                  {New Single() {1, 0, 0, 0, 0}, _
                   New Single() {0, 1, 0, 0, 0}, _
                   New Single() {0, 0, 1, 0, 0}, _
                   New Single() {0, 0, 0, 1, 0}, _
                   New Single() {0, 0, 1, 0, 0}})
    ' associate the ColorMatrix object with an ImageAttributes object
    ' draw the image applying the ColorMatrix
    e.Graphics.DrawImage(img, rc, 0, 0, img.Width, img.Height, _
                           GraphicsUnit.Pixel, imgattr)
End Sub