top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to convert an image to icon in ASP.NET?

0 votes
412 views
How to convert an image to icon in ASP.NET?
posted Feb 26, 2016 by Sathaybama

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Sometimes we need to use icons (.ico files) in our ASP.NET application but usually we get images (.png, .jpg, .gif, .bmp files) and we may not be able to use these images in our application. We can convert these images to icons using ASP.NET code. The code provided below will convert image files (.png, .jpg, .gif, .bmp etc) to icons for using in ASP.NET application.

ASP.NET provides classes including Image, Bitmap, Icon and Stream to achieve our purpose. System.Drawing namespace has Image, Bitmap and Icon classes and System.IO namespace has the Stream class. The code below is simple and self explanatory but I will explain to make it more easy.

C#

public void ConvertImageToIcon()
{
    string filePathImage = "D:\\Test.png";
    string filePathIcon = "D:\\Test.ico";
    Image imageFile = Image.FromFile(filePathImage, true);
    using (Bitmap bitmapFile = new Bitmap(imageFile))
    {
        bitmapFile.SetResolution(72, 72);
        IntPtr intptr = bitmapFile.GetHicon();

        using (Icon iconFile = Icon.FromHandle(intptr))
       {
            using (Stream stream = File.Create(filePathIcon))
            {
                iconFile.Save(stream);
            }
        }
    }
}

VB.NET

Public Sub ConvertImageToIcon()
    Dim filePathImage As String = "D:\Test.png"
    Dim filePathIcon As String = "D:\Test.ico"
    Dim imageFile As Image = Image.FromFile(filePathImage, True)
    Using bitmapFile As New Bitmap(imageFile)
        bitmapFile.SetResolution(72, 72)
        Dim intptr As IntPtr = bitmapFile.GetHicon()

        Using iconFile As Icon = Icon.FromHandle(intptr)
            Using stream As Stream = File.Create(filePathIcon)
                iconFile.Save(stream)
            End Using
        End Using
    End Using
End Sub

The first step is to get the image file path in a string variable. The image file can be in any format including .png, .jpg, .gif, .bmp extension files. You can also set the file path for icon file to in a string variable to save it to specific location. I have created an object of Image class and called the FromFile() method to create an image from specified file. Then I have created an object of Bitmap class from image file. IntPtr is a platform specific type that is used to represent a handle or pointer. GetHicon() method returns a handle to an icon which is represented using IntPtr. Then I have created an object of Icon class and called the FromHandle() method by passing IntPtr handle. I have created an object of Stream class and called the Create() method of File class to create a new icon file for the specified file path. At the end, I have called the Save() method of Icon class by providing object of Stream class as parameter.

answer Feb 26, 2016 by Shivaranjini
...