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

前端 未结 5 1148
予麋鹿
予麋鹿 2021-01-03 18:25

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

相关标签:
5条回答
  • 2021-01-03 18:32

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

    0 讨论(0)
  • 2021-01-03 18:33

    Go here:

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

    And here:

    Paint.NET ico Plugin (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.

    0 讨论(0)
  • 2021-01-03 18:38

    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.

    0 讨论(0)
  • 2021-01-03 18:41

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

    [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;
    }
    
    0 讨论(0)
  • 2021-01-03 18:49

    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());
            }
    
    0 讨论(0)
提交回复
热议问题