Hi Friends, in this article I will explain about how to
convert bitmap to .ico file using VB.NET/C# or How to save Bitmap as icon.
I already explained some articles on Widows Forms.
Lets I explain about how to convert bitmap image to icon.
Take one new project
File---New—Project---give name
Take one button, OpenFileDialog and SaveFileDialog
Create one function BitmapToIcon() as
below.
Private Sub BitmapToIcon(ByVal
BitmapFile As String,
ByVal IconFile As
String, ByVal
TransColor As Color)
If Not System.IO.File.Exists(BitmapFile) Then
MessageBox.Show("Bitmap File Not Found", "File Not Found", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
Else
'Load
bitmap
Dim
bmp As Bitmap = Bitmap.FromFile(BitmapFile)
bmp.MakeTransparent(TransColor)
'Convert
a bitmap file to icon(.ico) file using from handle
Dim
ico As Icon = Icon.FromHandle(bmp.GetHicon())
'Create
the file that we use for the icon.
Dim
sw As System.IO.StreamWriter =
System.IO.File.CreateText(IconFile)
'Save
icon data where you want with your filename.
ico.Save(sw.BaseStream)
sw.Close()
ico.Dispose()
bmp.Dispose()
sw.Dispose()
End If
End Sub
|
Double click on the button and write the following code.
Try
'Make a
32x32 16 color icon from a 16 color bitmap with seleced tranparent color.
MsgBox("open
bitmap (image file)")
Dim
dialog As New
System.Windows.Forms.OpenFileDialog
With dialog
.InitialDirectory = "D:\"
.DefaultExt = "bmp"
.Filter = "Bitmap Image(*.bmp)|*.bmp|All files
(*.*)|*.*"
.Title = "Select an Bitmap Image"
.FilterIndex = 1
End
With
If
dialog.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then
Return
End
If
Dim
fname As String
fname = dialog.FileName
MsgBox("Save
the bitmap image to .ico")
SaveFileDialog1.ShowDialog()
BitmapToIcon(fname,
SaveFileDialog1.FileName() & ".ico",
Color.Green)
Catch
ex As Exception
MsgBox("Bitmap
File is not converted to .ico")
End Try
MsgBox(".Ico file is created.Check
once")
|
The complete code in VB.NET
Imports System.Drawing
Public Class Form1
Private Sub Button1_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
'Make a
32x32 16 color icon from a 16 color bitmap with seleced tranparent color.
MsgBox("open
bitmap (image file)")
Dim
dialog As New
System.Windows.Forms.OpenFileDialog
With
dialog
.InitialDirectory = "D:\"
.DefaultExt = "bmp"
.Filter = "Bitmap Image(*.bmp)|*.bmp|All files
(*.*)|*.*"
.Title = "Select an Bitmap Image"
.FilterIndex = 1
End
With
If
dialog.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then
Return
End
If
Dim
fname As String
fname = dialog.FileName
MsgBox("Save
the bitmap image to .ico")
SaveFileDialog1.ShowDialog()
BitmapToIcon(fname,
SaveFileDialog1.FileName() & ".ico",
Color.Green)
Catch
ex As Exception
MsgBox("Bitmap
File is not converted to .ico")
End Try
MsgBox(".Ico
file is created.Check once")
End Sub
Private Sub BitmapToIcon(ByVal
BitmapFile As String,
ByVal IconFile As
String, ByVal
TransColor As Color)
If Not System.IO.File.Exists(BitmapFile) Then
MessageBox.Show("Bitmap File Not Found", "File Not Found", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
Else
'Load
bitmap
Dim
bmp As Bitmap = Bitmap.FromFile(BitmapFile)
bmp.MakeTransparent(TransColor)
'Convert
a bitmap file to icon(.ico) file using from handle
Dim
ico As Icon = Icon.FromHandle(bmp.GetHicon())
'Create
the file that we use for the icon.
Dim
sw As System.IO.StreamWriter =
System.IO.File.CreateText(IconFile)
'Save
icon data where you want with your filename.
ico.Save(sw.BaseStream)
sw.Close()
ico.Dispose()
bmp.Dispose()
sw.Dispose()
End If
End Sub
End Class
|
In C#:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
Public Class Form1
{
private
void Button1_Click(System.Object sender, System.EventArgs e)
{
try
{
//Make a 32x32 16 color icon
from a 16 color bitmap with seleced
tranparent color.
Interaction.MsgBox("open bitmap (image file)");
System.Windows.Forms.OpenFileDialog
dialog = new
System.Windows.Forms.OpenFileDialog();
var _with1 = dialog;
_with1.InitialDirectory = "D:\\";
_with1.DefaultExt = "bmp";
_with1.Filter = "Bitmap Image(*.bmp)|*.bmp|All files
(*.*)|*.*";
_with1.Title = "Select an Bitmap Image";
_with1.FilterIndex = 1;
if
(dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) {
return;
}
string
fname = null;
fname = dialog.FileName;
Interaction.MsgBox("Save the bitmap image to .ico");
SaveFileDialog1.ShowDialog();
BitmapToIcon(fname,
SaveFileDialog1.FileName() + ".ico",
Color.Green);
} catch
(Exception ex) {
Interaction.MsgBox("Bitmap File is not converted to .ico");
}
Interaction.MsgBox(".Ico file is created.Check once");
}
private
void BitmapToIcon(string BitmapFile, string IconFile, Color TransColor)
{
if
(!System.IO.File.Exists(BitmapFile)) {
MessageBox.Show("Bitmap File Not Found", "File Not Found", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
} else
{
//Load bitmap
Bitmap bmp =
Bitmap.FromFile(BitmapFile);
bmp.MakeTransparent(TransColor);
//Convert a bitmap file to icon(.ico) file using
from handle
Icon ico =
Icon.FromHandle(bmp.GetHicon());
//Create the file that we
use for the icon.
System.IO.StreamWriter sw =
System.IO.File.CreateText(IconFile);
//Save icon data where you
want with your filename.
ico.Save(sw.BaseStream);
sw.Close();
ico.Dispose();
bmp.Dispose();
sw.Dispose();
}
}
}
|
Save the above program and Run. It will ask bitmap image and
saving location after that it automatically It converts bitmap image to icon
file(.ico)
If you like my blog Aspdotnet-Roja then why are you waiting like my blog through facebook page Aspdotnet-Roja
No comments:
Post a Comment