Monday, June 17, 2013

How Convert Bmp To Icon in VBNET?

How Convert Bmp To Icon in VBNET?


Convert Bitmap picture and any image to icon with this sample application. Converting image becomes very simple. If you need to save the icon file, you just need to program and modify the sample code.

I made the sample code using Microsoft Visual Basic 2010.




Imports System.Security.Permissions ' for SecurityPermissionAttribute

''' <summary>
''' Author : Check-Kay Wong
'''
''' Description: Simple application to Convert a BITMAP to ICON in VBNET
'''
''' Anwser a question from : http://www.dreamincode.net/forums/topic/323139-how-convert-bmp-to-icon/
''' reference to : http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.gethicon(v=vs.100).aspx
''' </summary>
''' <remarks></remarks>
Public Class Form1

    'Declaration
    <SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags:=SecurityPermissionFlag.UnmanagedCode)> _
    Public Function GetHicon() As IntPtr

    End Function
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim oOpenFileDialog As OpenFileDialog
        Dim oSaveFileDialog As SaveFileDialog
        Dim oBitmap As Bitmap
        Dim HIcon As IntPtr
        Dim newIcon As Icon
        Dim oFileStream As IO.FileStream

        oOpenFileDialog = New OpenFileDialog
        oOpenFileDialog.Filter = "Bitmap File| *.bmp"
        If oOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = oOpenFileDialog.FileName

            ' Create a Bitmap object from an image file.
            oBitmap = New Bitmap(oOpenFileDialog.FileName)

            ' Get an Hicon for myBitmap.
            HIcon = oBitmap.GetHicon()

            ' Create a new icon from the handle.
            newIcon = System.Drawing.Icon.FromHandle(HIcon)

            ' Set the form Icon attribute to the new icon.
            Me.Icon = newIcon

            'Save the bitmap to new file
            oSaveFileDialog = New SaveFileDialog
            oSaveFileDialog.DefaultExt = ".ico"
            'oSaveFileDialog.AddExtension = True
            If oSaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                oFileStream = New IO.FileStream(oSaveFileDialog.FileName, IO.FileMode.CreateNew)
                newIcon.Save(oFileStream)
                oFileStream.Close()
            End If



        End If
    End Sub
End Class




Download the sample code here : SampleBitmaptoIcon.zip


I use Microsoft Visual Studio 2010 to do all my projects. I suggest you to buy and get the best IDE in the world and the newest one: Microsoft Visual Studio 2012 on Amazon

Reference :

2 comments:

  1. dude, this can create only 125 X 125,
    while I want the size custom...(I know max size is 225 X 225)
    I will use numeric updown for this so... please tell me fast

    ReplyDelete
  2. Nice explain about convert Bmp To Icon in VBNET. Good videos.

    Convert VB6 to VB.NET

    ReplyDelete