无法将类型 'System.Drawing.Image' 转换为 'System.Drawing.Icon'



我使用VBUC将一个VB6应用程序迁移到C#,但我得到了这个错误:

无法将类型"System.Drawing.Image"转换为"System.Drawing.Icon"我的代码是:

this.Icon = (Icon) ImageList1.Images[0];
this.Text = "Edit Existing Level";

内存中解决此问题的最快方法是哪种?

我写了一个扩展方法,将图像转换为位图,然后转换为图标:

public static class MyExtensions
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);
public static System.Drawing.Icon ToIcon(this System.Drawing.Image instance)
{
using (System.Drawing.Bitmap bm = (System.Drawing.Bitmap)instance)
{
System.Drawing.Icon copy = null;

// Retrieve an HICON, which we are responsible for freeing.
IntPtr hIcon = bm.GetHicon();
try
{
// Create an original from the Bitmap (the HICON of which must be manually freed).
System.Drawing.Icon original = System.Drawing.Icon.FromHandle(hIcon);
// Create a copy, which owns its HICON and hence will dispose on its own.
copy = new System.Drawing.Icon(original, original.Size);
}
finally
{
// Free the original Icon handle (as its finalizer will not). 
DestroyIcon(hIcon);
}
// Return the copy, which has a self-managing lifetime.
return copy;
}
}
}

最新更新