Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to convert an image to icon in ASP.NET

Author: Rudy Pinson

In this article, I will show you how you can convert an image to icon in ASP.NET code.

 

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.  

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to use AJAX CalendarExtender in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net