Create an icon using VB.NET
But since you're a .NET-coder, you can create an application to convert an image to an icon in less then one minute!
Have a look at this demo application.
- Create a new Windows Application
- Set the value of the AllowDrop property of the form to True
- Add this code to the DragEnter event of the form
Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
- Add code to the DragDrop event of the form
Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
Dim arr As Array = DirectCast(e.Data.GetData(DataFormats.FileDrop), Array)
For i As Integer = 0 To arr.Length - 1
CreateIcon(Convert.ToString(arr.GetValue(i)))
Next
End Sub
- Create the CreateIcon procedure
Private Sub CreateIcon(ByVal bitmapName As String)
Try
Dim fi As New System.IO.FileInfo(bitmapName)
Dim bmp As New Bitmap(fi.FullName)
Dim sw As System.IO.StreamWriter = System.IO.File.CreateText(fi.FullName.Replace(fi.Extension, ".ico"))
Icon.FromHandle(bmp.GetHicon).Save(sw.BaseStream)
sw.Close()
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex)
End Try
End Sub
That's all... Just drop an image on your form and the icon will be created.
12 Comments:
Thank you for the tip...I'm trying to enhance it to work instantly at runtime without the dragdrop thing...but you gave me something to begin with...
By Cute NK2, at 2:17 PM
You're welcome. Thank you for your comment.
By kHSw, at 10:53 PM
It was very very very good. Thnks. - Mahesh R
By Mahesh Radhakrishnan, at 2:49 PM
Cool! Especially since you cant seem to get a "free" working icon creator online. Now i can make one :-)
By Anonymous, at 12:14 PM
Great code but....
It converts the bmp into an icon in the dir of the original bmp, but the file appears corrupt when the process is compleated....
Any ideas what I can do to fix this?
By Unknown, at 4:40 PM
Thanks, but I get a "Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated." warning, any suggestions?
By Anonymous, at 6:14 PM
Good job!! it function very good!
Only the quality seems not very good but it function!
It transform any image in icon also with transparent.
ema from italy
By Anonymous, at 12:09 PM
Works like a dream. You've saved my hair.
By Unknown, at 10:23 AM
Thanks, but I get a "Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated." warning, any suggestions?
By Anonymous, at 6:14 PM
yeh im getting that too any ideas people>>
By the_niko, at 10:45 PM
Thanks man
By coolcat, at 9:19 AM
the resulting icon is distorted; there must be a better way.
By Anonymous, at 5:54 AM
brilliant! Thank you!
By Anonymous, at 11:50 AM
Post a Comment
<< Home