C# 得到 .PNG 文件。希望嵌入的图标资源在表单标题栏上显示为图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/154119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:55:46  来源:igfitidea点击:

Got .PNG file. Want embeddded icon resource displayed as icon on form title bar

提问by David Max

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.

这是一道面试题。鉴于 Visual Studio 2008 和保存为 .PNG 文件的图标,他们需要将图像作为嵌入资源并用作表单标题栏中的图标。

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

我正在寻找这个问题的模型答案,包括(工作!)代码和任何 Visual Studio 技巧。(如果我下次遇到它,模范答案应该能让我得到这份工作。)

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?

我假设 .PNG 文件实际上需要是 .ICO,但我看不到如何进行转换。这个假设是正确的还是有不同的问题?

采纳答案by Silver Dragon

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

启动 VS,启动新的 Windows 应用程序。打开属性表,将 .png 文件添加为资源(在本例中为: glider.png )。从这里开始,您可以将资源作为位图文件访问为 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());
        }

回答by Patrik Svensson

A good resource on the subject in C# 2.0 Convert Bitmap to Icon.

C# 2.0 Convert Bitmap to Icon 中关于该主题的一个很好的资源。

回答by Jonathan C Dickinson

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.

这是您可以在几分钟内使用 Google 和 Stack Overflow 等资源在计算机前解决的那种事情,解决这样的任务几乎无法了解您是否是一名优秀的开发人员。

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.

Icon.FromHandle 会导致 PNG 出现问题,因为 PNG 具有不止一点的透明度。这种类型的问题可以通过像这样的库来解决。

Chances are theydidn'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.

很有可能他们不知道该怎么做,他们正试图从潜在员工中挤出答案。此外,从 PNG 设置表单的图标是一种不必要的性能打击,它首先应该是一个 ICO。

回答by Kelly

Go here:

到这里:

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

http://www.getpaint.net/(免费)

And here:

和这里:

Paint.NET ico Plugin(free)

Paint.NET ico 插件(免费)

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.

安装 Paint.NET。将 ico 插件(第二个链接)放入 Paint.NET\FileTypes 文件夹。启动 Paint.NET。打开您的 .png 并将其另存为 .ico。

Free and easy.

自由而轻松。

回答by dizzy.stackoverflow

This worked for my purposes since all of my resources were PNG files:

这对我有用,因为我的所有资源都是 PNG 文件:

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

// From http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.gethicon.aspx
private Icon bitmapToIcon(Bitmap myBitmap)
{
    // Get an Hicon for myBitmap.
    IntPtr Hicon = myBitmap.GetHicon();

    // Create a new icon from the handle.
    Icon newIcon = Icon.FromHandle(Hicon);

    return newIcon;
}