Save bitmap in ICO file, with transparency

I am trying to save a bitmap as an ico file, but the result file is a PNG image with the extension changed to "ico".

Why does the ImageFormat class save my bitmap in PNG format if I choose the icon format? and then how to save the ico file?

PS: I need to keep ICO with transparency

This is how I call proc:

Save_Icon(Resize_Image(Bitmap.FromFile(PictureBox_Regedit.Tag), 24, 24), "Regedit.ico") 

And this is res code

 Private Sub Save_Icon(ByVal Source As Bitmap, ByVal Filename As String) Try If Not Directory.Exists(AppDir) Then Directory.CreateDirectory(AppDir) If Not Directory.Exists(AppIcons) Then Directory.CreateDirectory(AppIcons) Source.MakeTransparent() Source.Save(Path.Combine(AppIcons, Filename), ImageFormat.Icon) Catch ex As Exception Throw New Exception(ex.Message) End Try End Sub Private Function Resize_Image(ByVal img As Image, ByVal Width As Int32, ByVal Height As Int32) As Bitmap Dim Bitmap_Source As New Bitmap(img) Dim Bitmap_Dest As New Bitmap(CInt(Width), CInt(Height)) Dim Graphic As Graphics = Graphics.FromImage(Bitmap_Dest) Graphic.DrawImage(Bitmap_Source, 0, 0, Bitmap_Dest.Width + 1, Bitmap_Dest.Height + 1) Return Bitmap_Dest End Function 
0
source share
2 answers

β€œSave as” in editing applications always means a conversion that you are not doing here.

You can, for example, use the Bitmap.GetHicon Method to convert the source code to the icon format and use the System.Drawing Icon Class from there. Remember to destroy Hicon after this.

The problem is that if I remember correctly that the .NET Framework leaves you. I can’t remember the transparency functions, and a few years ago I switched to the FreeImage library and do everything with it.

+1
source

As the documentation says:

When you call MakeTransparent, the bitmap will be converted to Format32bppArgb, since this format supports the alpha channel.

Icons do not support alpha channels. Transparency transparency is achieved differently. The Save method does everything possible to keep the structure you create intact, hence the PNG result.

+2
source

All Articles