.comment-link {margin-left:.6em;}

kHSw

Wednesday, March 08, 2006

Create an icon using VB.NET

You just have finished your application, the only thing you need is an icon. You don't have icons on your machine and you don't have the graphical skills to create one. But... you have an image on your PC or you found one on Google (watch out for copyrights!) that could be used, too bad it's not in .ico format.

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 Blogger Cute NK2, at 2:17 PM  

  • You're welcome. Thank you for your comment.

    By Blogger kHSw, at 10:53 PM  

  • It was very very very good. Thnks. - Mahesh R

    By Blogger Maggu, 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 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 Blogger l___f, 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 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 Ema, at 12:09 PM  

  • Works like a dream. You've saved my hair.

    By Blogger Marc, 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 Blogger the_niko, at 10:45 PM  

  • Thanks man

    By Anonymous coolcat, at 9:19 AM  

  • the resulting icon is distorted; there must be a better way.

    By Anonymous Anonymous, at 5:54 AM  

  • brilliant! Thank you!

    By Anonymous Anonymous, at 11:50 AM  

Post a Comment

Links to this post:

Create a Link

<< Home


 
Stefanie Worm is het liefste vrouwtje van de wereld.
Melina is de liefste schatsie van de wereld (Erik De Maeyer).