16 Color Bitmap 2 Icon-VBForums
Results 1 to 1 of 1

Thread: 16 Color Bitmap 2 Icon

Threaded View

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    564

    16 Color Bitmap 2 Icon

    Hi this is my little bit of code I made to convert a 16 color bitmap to a 16 color icon with your selected transparent color. Anyway hope it maybe of some use to someone.
    Comments suggestions welcome.

    vbnet Code:
    1. Private Sub BitmapToIcon16(ByVal BitmapFile As String, ByVal IconFile As String, ByVal TransColor As Color)
    2.         'This little sniplet of code will convert a 16 color bitmap to a icon file.
    3.  
    4.         'Check that bitmap file is here.
    5.         If Not System.IO.File.Exists(BitmapFile) Then
    6.             MessageBox.Show("Bitmap File Not Found", "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    7.         Else
    8.             'Load bitmap
    9.             Dim bmp As Bitmap = Bitmap.FromFile(BitmapFile)
    10.             'Set trans color.
    11.             bmp.MakeTransparent(TransColor)
    12.             'Convert bitmap to icon from bitmap handle
    13.             Dim ico As Icon = Icon.FromHandle(bmp.GetHicon())
    14.             'Create the file that we use for the icon.
    15.             Dim sw As System.IO.StreamWriter = System.IO.File.CreateText(IconFile)
    16.             'Save icon data to filename.
    17.             ico.Save(sw.BaseStream)
    18.             'Close file
    19.             sw.Close()
    20.             'Clear up
    21.             ico.Dispose()
    22.             bmp.Dispose()
    23.             sw.Dispose()
    24.         End If
    25.     End Sub

    Example

    vbnet Code:
    1. Private Sub cmdDoIt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDoIt.Click
    2.         'Make a 32x32 16 color icon from a 16 color bitmap with seleced tranparent color.
    3.         BitmapToIcon16("C:\out\rocket.bmp", "C:\out\rocket.ico", Color.Magenta)
    4.     End Sub
    Last edited by BenJones; Mar 22nd, 2012 at 08:26 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.