tags:

views:

2360

answers:

4

This was an interview question. Given Visual Studio 2008 and an icon saved as a .PNG file, they required the image as an embedded resource and to be used as the icon within the title bar of a form.

I'm looking for what would have been the model answer to this question, Both (working!) code and any Visual Studio tricks. (Model answer is one that should get me the job if I meet it next time around.)

Specifically I don't know how to load the image once it is an embedded resource nor how to get it as the icon for the title bar.

As a part solution, ignoring the embedded bit, I copied the resource to the ouput directory and tried the following:-

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Icon = new Icon("Resources\\IconImage.png");
    }
}

This failed with the error "Argument 'picture' must be a picture that can be used as a Icon."

I presuming that the .PNG file actually needed to be a .ICO, but I couldn't see how to make the conversion. Is this presumption correct or is there a different issue?

A: 

Here are some good resources on the subject:

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/4a10d440-707f-48d7-865b-1d8804faf649/

http://www.dreamincode.net/code/snippet1684.htm

Patrik
Nice routine for converting an image to an icon. In my case image had right dimensions for an icon so maybe a simplification is possible?Also I don't have a way of getting at the embedded image.
David Max
+4  A: 

Fire up VS, start new Windows Application. Open the properties sheet, add the .png file as a resource (in this example: glider.png ). From hereon, you can access the resource as a Bitmap file as WindowsFormsApplication10.Properties.Resources.glider

Code for using it as an application icon:

 public Form1()
        {
            InitializeComponent();
            Bitmap bmp = WindowsFormsApplication10.Properties.Resources.glider;
            this.Icon = Icon.FromHandle(bmp.GetHicon());
        }
Silver Dragon
+7  A: 

The model answer for that question would be:

System.Console.WriteLine("Are you serious?");
System.Console.WriteLine("I think I will try my chances with another employer.");

That is the sort of thing you could solve in front of your computer in a few minutes using resources like Google and Stack Overflow, solving a task like this gives little insight into if you are a good developer or not.

Icon.FromHandle will cause problems with a PNG, because PNGs have more than one bit of transparency. This type of issue can be solved with a library like this one.

Chances are they didn't know how to do it and they were trying to squeeze the answer out of potential employees. Furthermore, setting the icon of the form from a PNG is an unessecary performance hit, it should have been an ICO in the first place.

Jonathan C Dickinson
+2  A: 

Go here: http://www.getpaint.net/ (free)

and here: http://www.evanolds.com/pdnicocur.html (free)

Install paint.net. Put the ico plugin (second link) into the Paint.NET\FileTypes folder. Start up paint .net. Open your .png and save it as an ico.

Free and easy.

Kelly