从资源文件中读取Cursor时,抛出ArgumentException

jan*_*anj 9 .net c# winforms

当我使用a 从资源文件MemoryStream加载a时Cursor,我会收到一个ArgumentException.这是我用来加载游标的代码:

Cursor myCursor
    = new Cursor(new MemoryStream(WaterforMGC.Properties.Resources.waterspray));
Cursor = myCursor;
Run Code Online (Sandbox Code Playgroud)

但是我得到了错误.我不知道出了什么问题,我甚Cursor = myCursor;至改变了 this.Cursor = myCursor;给了我同样的错误.我试过了,gameform.Cursor = myCursor;但根本没用.

System.ArgumentException: Image format is not valid. The image file may be corrupted.
Parameter name: stream ---> System.Runtime.InteropServices.COMException (0x800A01E1): Exception from HRESULT: 0x800A01E1 (CTL_E_INVALIDPICTURE)
   at System.Windows.Forms.UnsafeNativeMethods.IPersistStream.Load(IStream pstm)
   at System.Windows.Forms.Cursor.LoadPicture(IStream stream)
   --- End of inner exception stack trace ---
   at System.Windows.Forms.Cursor.LoadPicture(IStream stream)
   at WaterforMGC.gameform.Form1_Load(Object sender, EventArgs e) in C:\Users\Jan\Documents\Visual Studio 2008\Projects\WaterforMGC\WaterforMGC\Form1.cs:line 39
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

dle*_*lev 11

问题在异常的第一行拼写出来:

System.ArgumentException:图像格式无效.图像文件可能已损坏.

您确定要加载的图像处于未损坏状态,并且与游标的图像格式兼容吗?

Cursor类不支持动画光标(.ani文件)或具有黑色和白色以外颜色的光标.

你还有其他任何你加载光标图像的地方吗?你可能能够解决这个问题,以确定这里出了什么问题.

  • @janj:[每个MSDN](http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor%28v=VS.80%29.aspx),"Cursor类不支持动画光标(.ani文件)或光标,黑色和白色以外的颜色." 它们是动画还是彩色? (2认同)

mei*_*rsd 6

实际上,您可以将彩色光标加载到.Net中.你只需要使用win32就可以了.

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern IntPtr LoadImage(IntPtr hinst, string lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad);

//........

const int IMAGE_CURSOR = 2; 
const uint LR_LOADFROMFILE = 0x00000010;
IntPtr ipImage = LoadImage(IntPtr.Zero, 
    @"c:\mycolor.cur", 
    IMAGE_CURSOR, 
    0, 
    0, 
    LR_LOADFROMFILE);

Cursor testCursor = new Cursor(ipImage);

Cursor.Current = testCursor;
Run Code Online (Sandbox Code Playgroud)