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?
Just right click on your project-> add-> resource. Add resource window would have popped up where in you can select icon as your preference and select import. Then it lets you to browse through your directory and select your icon.
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()); }
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 IconLib.
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 unnecessary performance hit, it should have been an ICO in the first place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With