|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionAs time goes on and applications evolve, the end user expects a richer and more vibrant user interface. Not only is it visually appealing, a nice user interface also serves as a function. Better design and use of icons to represent tasks will not only make your application stand out but will also give the end user a better experience working with your applications. This is the reason why I decided to publish this article. Now, let's jump to the technical aspects... We've all seen those nice toolbar icons Microsoft (c) uses in their products. They include shadows, 24 bit true colors, transparencies etc. These images are actually 24 bit images that contain an alpha channel which makes them 32 bit format. This format not only allows transparent pixels, but semi-transparent pixels as well. This allows images to blend in with their background nicely without the "jaggedness" you sometimes see with regular 24 bit transparent images. If you have ever tried using such icons or images in your applications, you have quickly found out that it's somewhat more of a challenge than you expected, and in fact there are some obstacles to overcome. Not to mention a bug that exists in the framework that will prevent you from displaying such images properly. In this article, I will try to explain what is needed to overcome these obstacles and also how to use my The ProblemThe bug I mentioned above, I believe, exists in the Now, if you try using these added images for your controls, you will find that since the alpha channel has become corrupt, the images contain solid black for the areas where alpha blending was to take place. In cases where you add PNG files with alpha channel, you will find that alpha blending does occur but contains a subtle color error in the areas where alpha blending was to take place. You can see a good example of this in the included image above by looking at the zoomed IE and MSN icon on the left. This might not be an eye catching thing for these 32x32 pixel icons but when you have 16x16 icons, the color error is very apparent. In conclusion, the The FixI have come up with a simple class with some static methods to perform the adding of 32 bit images to your Before using the codeFirst and foremost, your C# application and control properties must be setup correctly before the images are displayed properly. Let's go over these first before moving onto the usage of the class. You will see below that the //
// Enabling Visual Styles
//
static void Main()
{
// These two calls MUST be made before the
// .Run method to enable 32bit icons to be used.
Application.EnableVisualStyles();
Application.DoEvents();
// The color depth of the ImageList MUST be set
// to 32bit before images can be added to the control.
myImagelist.ColorDepth = ColorDepth.Depth32Bit;
Application.Run(new Form1());
}
Public Methods of the ImageList Classpublic sealed class Imagelist
{
public static void AddFromImage( Image sourceImage,
ImageList destinationImagelist ){..}
public static void AddFromImage( Icon sourceIcon,
ImageList destinationImagelist ){..}
public static void AddFromFile( string fileName,
ImageList destinationImagelist ){..}
public static void AddIconOfFile( string fileName, IconSize iconSize,
bool selectedState, bool openState,
bool linkOverlay, ImageList destinationImagelist ){..}
public static void AddIconOfFile( string fileName,
ImageList destinationImagelist ){..}
public static void AddIconOfFileExt( string fileExtension,
IconSize iconSize, bool selectedState, bool openState,
bool linkOverlay , ImageList destinationImagelist ){..}
public static void AddIconOfFileExt( string fileExtension,
ImageList destinationImagelist ){..}
public static void AddEmbeddedResource( string resourceName,
ImageList destinationImagelist){..}
}
Now that those calls and properties have been made, we can move on to the usage of the static class methods.... Adding Image or Icon objects//
// This example adds a 32bit alphablended image
// to myImagelist using the AddFromImage() method.
// This overloaded method accepts either
// an Image object or Icon object as its source.
//
Narbware.Imagelist.AddFromImage( Image.FromFile("image.png"),
myImagelist );
Bitmap myBitmap = new Bitmap( "image.png" );
Narbware.Imagelist.AddFromImage( myBitmap, myImagelist );
Icon myIcon = new Icon("icon.ico");
Narbware.Imagelist.AddFromImage( myIcon, myImagelist );
Adding images from embedded resourcesThe importance of this method is that you can use it to pre load images to the //
// This example adds a 32bit alphablended image from
// an embedded resource to myImagelist
// using the AddEmbeddedResource() method.
//
Narbware.Imagelist.AddEmbeddedResource( "myApplicationName.image.png",
myImagelist ); // where image.png resides in the projects root
Adding images from external filesThis method saves some time when wanting to add images from files that reside outside of your project's directory. Since you don't have to create an //
// This example adds a 32bit alphablended image from an external
// file to myImagelist using the AddFromFile() method.
//
Narbware.Imagelist.AddFromFile( "c:\\blah\image.png", myImagelist );
Narbware.Imagelist.AddFromFile( "c:\\icons\icon.ico", myImagelist );
Extracting and adding file or folder iconsThis method extracts file or folder icons and adds them to //
// This example extracts and adds 32bit alphablended icons
// from external files and folders
// to myImagelist using the AddIconOfFile() method.
//
// This short overload extracts and adds
// a largs icon (32x32) to and imagelist
Narbware.Imagelist.AddIconOfFile( "c:\\file.mpg",
myImagelist )
// adds the files icons, in this case the systems mpg icon
// This longer method gives you more
// options on the type of icon to extract
Narbware.Imagelist.AddIconOfFile( "c:\\file.mpg",
Narbware.IconSize.Small, false, false,
false, myImagelist );
// This adds a folder icon. It can also extract
// special folder icons such as My Music, My Pictures, etc...
Narbware.Imagelist.AddIconOfFile( "c:\mySpecialFolder",
Narbware.IconSize.Large, false, false,
false, myImagelist );
Extracting and adding file type icons (extensions)This method is similar to the above //
// This example extracts and adds 32bit alphablended file
// extension icons to myImagelist using the AddIconOfFileExt() method.
//
// adds a large sized icon associated with jpeg files.
Narbware.Imagelist.AddIconOfFileExt( "jpg", myImagelist );
Narbware.Imagelist.AddIconOfFileExt( "zip", Narbware.IconSize.Small,
false, false, false, myImagelist );
// adds a small sized zip file icon to the imagelist.
Points of InterestBelow is the private static void Add( Bitmap bm, ImageList il )
{
IntPtr hBitmap, ppvBits;
BITMAPINFO bmi = new BITMAPINFO();
if ( bm.Size != il.ImageSize )
{
// resize the image to dimensions of imagelist before adding
bm = new Bitmap( bm, il.ImageSize.Width, il.ImageSize.Height );
}
bmi.biSize = 40; // Needed for RtlMoveMemory()
bmi.biBitCount = 32; // Number of bits
bmi.biPlanes = 1; // Number of planes
bmi.biWidth = bm.Width; // Width of our new bitmap
bmi.biHeight = bm.Height; // Height of our new bitmap
bm.RotateFlip( RotateFlipType.RotateNoneFlipY );
// Required due to the way bitmap is copied and read
hBitmap = CreateDIBSection( new IntPtr(0), bmi, 0,
out ppvBits, new IntPtr(0), 0 );
//create our new bitmap
BitmapData bitmapData = bm.LockBits( new Rectangle( 0, 0,
bm.Width, bm.Height ), ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb );
RtlMoveMemory( ppvBits, bitmapData.Scan0,
bm.Height * bitmapData.Stride );
// copies the bitmap
bm.UnlockBits( bitmapData );
ImageList_Add( il.Handle, hBitmap, new IntPtr(0) );
// adds the new bitmap to the imagelist control
}
ConclusionBy using the | ||||||||||||||||||||