Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert bitmap to icon

Tags:

I am trying to convert bitmap into icon. But there is some error as the resultant file is just blank.

private void btnCnvrtSave_Click(object sender, EventArgs e) {     Bitmap bmp = new Bitmap(sourceFile);  //sourceFile = openfiledialog.FileName;     IntPtr Hicon = bmp.GetHicon();     Icon myIcon = Icon.FromHandle(Hicon);      SaveFileDialog sfd = new SaveFileDialog();     sfd.Title = "Save Icon";     sfd.Filter = "Icon|*.ico";     sfd.ShowDialog();      FileStream fileStream = new FileStream(sfd.FileName, FileMode.OpenOrCreate);     myIcon.Save(fileStream);     fileStream.Flush();     fileStream.Close();      MessageBox.Show("Image is converted successfully!");      //Process.Start(sfd.FileName); } 

I've tried a lot to figure-out the problem but couldn't. Please tell me where the problem is.

like image 352
Muhammad Ali Dildar Avatar asked Nov 17 '11 21:11

Muhammad Ali Dildar


People also ask

Can I use a PNG as an icon?

If you're loading the images from a resource file, then no, you can't use PNGs, you have to use ICOs. Fortunately, there are a number of tools that can convert PNGs into ICOs, including ImageMagick (great for automation), and MSPaint as a lowest common denominator.


1 Answers

Please, use DestroyIcon after GetHicon, to prevent memory leak

[DllImport("user32.dll", CharSet = CharSet.Auto)] extern static bool DestroyIcon(IntPtr handle); 

MSDN : https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.gethicon%28v=vs.110%29.aspx

like image 84
Nigrimmist Avatar answered Sep 19 '22 14:09

Nigrimmist