home

View All Blogs

Blog Search


Most Popular Blogs
Visual Basic or C#?
    12340 Views
Cool Visual Basic Tips: Graphics and Icons
    8015 Views
The Big Pink Button
    2533 Views
Software as a Service - SaaS
    460 Views
Building Customer Loyalty
    248 Views

Most Recent Blogs
Building Customer Loyalty
    4/14/2010 2:29:40 PM
How IT Can Build Partnerships with the Business Side of the Company
    4/14/2010 7:16:29 AM
Protecting Your Customer Base by Building Better Relationships
    4/7/2010 1:07:46 PM
Software as a Service - SaaS
    11/18/2008 8:15:28 AM
The Big Pink Button
    4/13/2007 4:59:36 PM

 


James' BLOG

Cool Visual Basic Tips: Graphics and Icons

By: James Foxall   12/15/2006 3:31:28 PM        8015 Views


Hi all,

At Tigerpaw, we're constantly forced to push a language well beyond what many programmers do. We've got about 12,000 end-users of our current release, and we closely mimic Microsoft's interfaces whenever possible. This means we spend a lot of time on details that otherwise get overlooked in many consulting or in-house projects. We license lots of 3rd party components (Infragistics, ComponentOne, Dundas, DotNetBar, SyncFusion, Data Dynamics, and many more). We've beaten our heads against the wall many times, and I'm sure we'll continue to do so for the next two years as we work on our first .NET-based release. Through all this confusion we often run across little solutions and ideas that may seem trivial, but that often solve a very real problem in an elegant or simple manner. Often times, the mechanisms employed aren't obvious or well-documented. Someday I plan on writing an advanced book on .NET, but my plate is full. In the mean-time, I thought I'd post some snippets on my blog in hopes they they prove helpful to others fighting similar battles.

Keep up the good fight! :)

Please note that not everything I post will be overly enlightening or particularly clever, but hopefully you'll find something of value. The really cool stuff I have to post will take some time to document and put in a usuable format. Remember, even if you don't need these specific procedures, take a hard look at the code and understand what's being done; you never know where you'll pick up a snippet, technique, or approach that you'll find useful in your own development work.

Transparent Background for Controls

I know it seems silly, but the first time I needed to make the background of a label control transparent, I was completely at a loss. The solution? Set the BackColor property to Web.Transparent. Viola!

Loading ImageList Controls using a Resource File

2. I really really like the way .NET handles resource files. In the past, our various toolbars (some 100+) all had their own embedded images, and making UI changes mid-stream was a tedious process. Now with resource files, it's easy to create a central repository of images to be used on various controls (PS: If you're not using PNG files, you should. Icon files aren't fully supported like PGN files. They should be, but they're not). Unfortunately, some controls, most notably the ListView and TreeView controls, don't make use of resource files and instead still want to use an ImageList control. This means you must duplicate your images in various ImageList controls as well as the resource files in which they belong. Or do you? When I discovered this, I absolutely refused to keep duplicate images and I set to work to find a solution. I ended up coming up with a rather elegant solution (though it probably can be improved upon). Here's the gist of it:

1. Create a new class called ImageListPacker. Here's the code:

Public Shared Sub AddImage(ByRef imageControl As _
   Windows.Forms.ImageList, ByVal imageName As String)

   imageControl.Images.Add(imageName, _
         CType(My.Resources.Resource1.ResourceManager.GetObject(imageName), _
         Image))
End Sub

Note that you'll have to substitute Resource1 with the name of your resource file. This procedure will load the image with the specified name into the specified ImageList control.

2. Next, in the Form_Load event of the form with an ImageList, make a call to the AddImage method for each image you needed added to the ImageList, like this:

ImageListPacker.AddImage(imagelistcontrol, imagename)

That's it! Now you can still have one central repository for your images, but you can use them with controls that require interaction with an ImageList control.

Converting Icons to Bitmaps and Bitmaps to Icons

Every now and then I find myself needing to convert icons to bitmaps or vice versa. This is usually because a 3rd party control wants a specific file type that differs from other components I'm using. The following two procedures should do the conversions (assuming I didn't botch something when taking the code from my project). NOTE: both procedures assume you've added Imports System.Drawing to the class.

Public Function BitmapToIcon(ByVal value As Bitmap) As Icon
   Dim objBitmap As Bitmap = New Bitmap(value)

   ' Use the hidden member GetHIcon() to get a handle to the icon.
   Dim HIcon As IntPtr = objBitmap.GetHicon()

   Return Icon.FromHandle(HIcon)

End Function

Public Function IconToBitmap(ByVal value As Icon, ByVal transparentColor As Color) As Bitmap

   Dim objGraphics As Graphics
   Dim objBitmap As Bitmap

   ' Create a new bitmap the same size as the icon.
   objBitmap = New Bitmap(value.Width, value.Height, _
         Imaging.PixelFormat.Format32bppPArgb)
   objGraphics = Graphics.FromImage(objBitmap)
   ' Fill the icon with the color we'll make transparent.
   objGraphics.FillRectangle(New SolidBrush(transparentColor), _
         New Rectangle(0, 0, objBitmap.Width, objBitmap.Height))
   ' Draw the icon into the bitmap object.
   objGraphics.DrawIcon(value, 0, 0)
   ' Always clean up after ourselves!
   objGraphics.Dispose()
   ' Make the specified color transparent in the icon.
   objBitmap.MakeTransparent(transparentColor)

   Return objBitmap

End Function

I've got some other great code for drawing visual theme elements, but I've got to get back to work. :)

james

 


Comments:

1/29/2008 1:56:56 PM     By: Brisky

I had C# installed on my old laptop that went out on me, I just another lAPTOP as a Christmas present, but the program will not run because it says " According to our records, this copy of Visual Studio.net trial 2003 English has already been activated on the maximun number of computers" How can I get pass this problem?


1/29/2008 4:09:42 PM     By: james

I would consider downloading the Express edition of Visual Studio 2008. The 2003 version is quite old at this time, and you'll get more value from the 2008 version.


2/18/2009 11:38:48 AM     By: Pamela Hall

Can't find the icon file log.ico which Visual Basic 2005 book says can be found on your site.

_pamela@bellsouth.net


3/16/2009 11:16:19 AM     By: James

On my books page you can find sample files for all books. Please go to www.jamesfoxall.com/books.aspx.

James


12/13/2009 12:21:23 AM     By: buy strattera

this is a cool news. Thank you.


5/5/2010 9:41:32 AM     By: Martin Kiely

Thank you for clarity in your VB 2008 book. I am an educator and someone new to programming and VB to boot, so please accept my thanks.

When I bought the book 'in-store' as it were, I have a coupon in the frontispiece to entitle me to 45 day viewing via Safari. There seems no place to enter this on Safari's site - they seem to want a subscription set up. Am I missing something obvious? Even a search on Safari under "Coupon" (per book) or "45-Day access" (per the book, yields a "0 results found".

My French wife laughed when she saw a typo in your "Transparent Background for Controls" section above. "Viola" should read "Voila!"

Many thanks once again.

Martin

martin.peptechnologies@googlemail.com


5/5/2010 1:26:25 PM     By: james

Hi Martin,

I'm glad you enjoyed the book and I appreciate your comments! I have nothing to do with Safari and haven't used the service myself, so I'm afraid I don't have any info for you. I would just contact Safari through the web site and ask. Please tell your wife that shows what a good four years of French classes can do for you in high school... lol

james




Post Comments:  
Full Name   

Comments   

Contact James | About James