Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Saving a bitmap as an icon.

Posted on 2005-02-09
9
Medium Priority
?
9,585 Views
Last Modified: 2011-08-18
Hello I was just wondering how I would save a bitmap file as an icon file.  I know how to convert the bitmap to an icon, but I do not know how to save it to disk once it is converted.  Thanks for the help.

Bitmap bmp = new Bitmap("c:\temp\test.bmp");
Icon icon = ImageToIcon(bmp);

// How do I save the icon to disk??

// Convert an image to an Icon
System.Drawing.Icon ImageToIcon(System.Drawing.Image image)
{
    return System.Drawing.Icon.FromHandle(((Bitmap) image).GetHicon());
}

- Joe
0
Comment
Question by:Realmrat
  • 5
  • 2
  • 2
9 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 13261865
bmp.Save(@"c:\temp\test.ico", ImageFormat.Icon);
0
 
LVL 3

Author Comment

by:Realmrat
ID: 13261908
Thank you Alex ... that appears to work -  though if I try and use the newly saved icon in a project, the program crashes.  Also if you try and edit it within visual studio I get what looks like only a hex representation of the object, instead of being able to edit the Icon (change colors of each pixel).

I am creating the bitmap dynamically, so I suppose that could be it.  I will create a test bitmap with photoshop and see if I get the same error.


- Joe
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13261923
Try this:

Bitmap bmp = new Bitmap("c:\temp\test.bmp");
Icon icon = ImageToIcon(bmp);
Bitmap bmp1 = Bitmap.FromHicon(icon.Handle);
bmp1.Save(@"c:\temp\test.ico", ImageFormat.Icon);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Author Comment

by:Realmrat
ID: 13261944
I regret to inform that this did not work.

In visual studio I tried to change the form icon by clicking on the properties of the form, and browsing to the newly created icon.  I received the error:

The argument 'picture' must be a picture that can be used as a Icon.

Any ideas?

- Joe
0
 
LVL 3

Author Comment

by:Realmrat
ID: 13261958
Your second suggestion provided the same results.

- Joe
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13263094
What is the color-depth for the BMP that you are trying to convert?

Bob
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 13263187
using System.IO;

public static void BmpToIcon(string sourcePath, string targetPath)
{

   // Retrieve the bitmap from the file.
   Bitmap bmp = Image.FromFile(filePath);

   // Convert the bitmap to an icon.
   Icon ico = Icon.FromHandle(bmp.GetHicon);
   
   // Create a file stream to save the icon stream.
   Stream st = new FileStream(targetPath, FileMode.Create);

   // Create a stream writer to physical write the data to the disk.
   BinaryWriter wr = new BinaryWriter(st);

   // Write the binary icon data to the file stream.
   ico.Save(st);

   // Close the file to write the stream to the disk.
   wr.Close();

}

Bob
0
 
LVL 3

Author Comment

by:Realmrat
ID: 13263832
Your solution worked perfectly thanks Bob.

- Joe
0
 
LVL 3

Author Comment

by:Realmrat
ID: 13263856
Guess I should say I had to change a line or two to get it to compile - but minor changes.  Here's the cut & paste.


public static void BmpToIcon(string sourcePath, string targetPath)
{

            // Retrieve the bitmap from the file.
            Bitmap bmp = (Bitmap)Image.FromFile(sourcePath);

            // Convert the bitmap to an icon.
            Icon ico = Icon.FromHandle(bmp.GetHicon());
   
            // Create a file stream to save the icon stream.
            Stream st = new FileStream(targetPath, FileMode.Create);

            // Create a stream writer to physical write the data to the disk.
            BinaryWriter wr = new BinaryWriter(st);

            // Write the binary icon data to the file stream.
            ico.Save(st);

            // Close the file to write the stream to the disk.
            wr.Close();
}
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question