![]() |
Image to Icon & Icon to Image
(Graphics; VB.NET)
This tutorial is about conversion of an image into icon and of an icon into image. We can say, it is extension of the previous tutorial about image formats. This code is interesting because in this I have used some techniques and controls, that we have not discussed till now. So, let's not waste much time and go straight to the code... Snapshot
As you can see the interface is simple - load the source file, convert it and save it! Code Let us discuss the image to icon first followed by icon to image conversion code. Image to Icon - Load button - Private Sub btnImgLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ The first line sets the filter property of the FileOpenDialog control, to restrict the type of files that can be opened. This code also demonstrates the use of exception handling in .NET. See inside the Try block. We have set the Image property of the PictureBox control. FromFile method loads the specified file and returns the image object. Now, what if the file we are trying to load is of incorrect format or corrupt? The program will halt. No. It will not because we have handled this possible exception condition. If anything goes wrong with the code inside the Try block, the program control is transferred to the Catch block. In catch block, we have reported the user about the possible error and have gracefully escaped the possible crash! - Convert to Icon - Private Sub btnIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
_ Line 1: Get the reference to the image in the PictureBox (Source Image) into img (Bitmap Object)Line 2: Make the image transparent (the color of the first pixel of the image to be made transparent) Line 3: Set the transparented image as target PictureBox 's Image - Save - The real fun is in this code... Private Sub btnIcnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
_ Please note that in the above code, we have created the FileStream object (fs). This is done to create a new file to save our icon. Also note the IntPtr type. This is a very important type when we are dealing with integer pointers to resources or handles. In short, the objects of this type can hold the handles. We have created hIcon of type IntPtr so that it can hold the pointer to the icon object we want. But where is the icon? What we have is a bitmap. Here, GetHicon method comes to our rescue (However, you will not see it in the member list of bitmap in code window - intellisense list). The GetHicon method returns the handle to the icon representing the bitmap. We store this in hIcon. We can create an Icon object from this handle using the FromHandle method of the Icon class. See code. Once the icon is created, we can save it using Save method of icon. Icon to Image Load and convert buttons are programmed similarly, let us consider the Save button code ... Private Sub btnImgSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
_ Please feel free to contact me for any queries, feedback, suggestions, comments or discussion at rahul@coder000.com |