Come back to home page

Image to Icon & Icon to Image (Graphics; VB.NET)Download

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) _
Handles btnImgLoad.Click
    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 a correct filename" & vbNewLine _
        & "If filename is correct the image file might be corrupt...", "File cannot be loaded", _
        MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

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 -

Private Sub btnIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ 
Handles btnIcon.Click
    Dim img As Bitmap = pctSImg.Image
    img.MakeTransparent(img.GetPixel(0, 0))
    pctDIcn.Image = img
End Sub

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) _
Handles btnIcnSave.Click
    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)
End Sub

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) _
Handles btnImgSave.Click
    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)
End Sub

Simple enough!! You can contact me in case of any doubt...

Please feel free to contact me for any queries, feedback, suggestions, comments or discussion at rahul@coder000.com