得到.png文件。要显示为表单标题栏图标embeddded图标资源 [英] Got .PNG file. Want embeddded icon resource displayed as icon on form title bar

查看:129
本文介绍了得到.png文件。要显示为表单标题栏图标embeddded图标资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个面试问题。由于Visual Studio 2008中,并保存为.png文件的图标,他们所需要的图像作为嵌入资源,并用作窗体的标题栏中的图标。

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.

我在寻找什么将是模型回答这个问题,这两个(工作!)code和任何Visual Studio的技巧。 (标准答案是一个应该让我的工作,如果我下次见面它周围。)

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."

我presuming的.png文件实际上需要的是一个.ICO,但我看不出如何进行转换。这是presumption正确还是有一个不同的问题?

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?

推荐答案

火了VS,启动新的Windows应用程序。打开属性表,添加.png文件作为资源(在这个例子中:glider.png)。从在此,您可以访问的资源作为一个位图文件作为WindowsFormsApplication10.Properties.Resources.glider

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

$ C $下使用它作为一个应用程序图标:

Code for using it as an application icon:

 public Form1()
        {
            InitializeComponent();
            Bitmap bmp = WindowsFormsApplication10.Properties.Resources.glider;
            this.Icon = Icon.FromHandle(bmp.GetHicon());
        }

这篇关于得到.png文件。要显示为表单标题栏图标embeddded图标资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆