This discussion is about conversion of an image into icon and of an icon into image. Apart from the code and technique of conversion, you will find some bits on FileOpenDialog, FileSaveDialog, Exception Handling, MakeTransparent method, FileStream, IntPtr (Integer Pointer), GetHicon & FromHandle methods as well.
Image to Icon
The code in the click event handler of Load button is:
foD.Filter = "Image Files (*.bmp; *.jpg; *.gif; *.tiff; *.png) | *.bmp;*.jpg;*.gif;*.tiff;*.png"
foD.ShowDialog()
Try
pctSImg.Image = pctSImg.Image.FromFile(foD.FileName)
Catch ex As Exception
MessageBox.Show("Please check that you have entered correct filename. If filename is correct the image file might be corrupt...", "File cannot be loaded", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Please note that foD is FileOpenDialog already added to the project, we have also used FileSaveDialog (fsD) in the code. 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
Dim img As Bitmap = pctSImg.Image img.MakeTransparent(img.GetPixel(0, 0)) pctDIcn.Image = img
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 stuff is here:
fsD.Filter = "Icon (*.ico)|*.ico"
fsD.ShowDialog()
Dim img As Bitmap = pctDIcn.Image
Dim fs As New IO.FileStream(fsD.FileName, IO.FileMode.Create)
Dim hIcon As IntPtr = img.GetHicon()
Dim icn As Icon = Icon.FromHandle(hIcon)
icn.Save(fs)
fs.Flush()
icn.Dispose() : fs.Close() : fs = Nothing
MessageBox.Show("Icon saved successfully", "Congratulations", MessageBoxButtons.OK, MessageBoxIcon.Information)
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.
fsD.Filter = "Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|GIF (*.gif)|*.gif"
fsD.ShowDialog()
Dim extn As String = fsD.FileName.Substring(fsD.FileName.IndexOf("."))
Dim img As Bitmap = pctDImg.Image
Dim format As Imaging.ImageFormat
Select Case extn
Case "*.bmp"
format = Imaging.ImageFormat.Bmp
Case "*.jpg"
format = Imaging.ImageFormat.Jpeg
Case "*.gif"
format = Imaging.ImageFormat.Gif
Case Else
format = Imaging.ImageFormat.Bmp
End Select
img.Save(fsD.FileName, format)
MessageBox.Show("Image saved successfully", "Congratulations", MessageBoxButtons.OK, MessageBoxIcon.Information)



Comments