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

前端 未结 5 1170
予麋鹿
予麋鹿 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: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;
    }
    

提交回复
热议问题