我正在使用VBUC将VB6应用程序迁移到C#,但出现了以下错误:
无法将类型“System.Drawing.Image”转换为“System.Drawing.Icon”,我的代码如下:
this.Icon = (Icon) ImageList1.Images[0];
this.Text = "Edit Existing Level";
哪种最快的内存方式可以解决这个问题?
我正在使用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;
}
}
}