WhenreadingCursorfromresourcefile,ArgumentExceptionthrowWhenIuseMemoryStreamtoloadCursorfromresourcefile,IreceiveArgumentException.这是我用来加载游标的代码:CursormyCursor=newCursor(newMemoryStream(WaterforMGC.Properties.Resources.waterspra??y));游标=我的游标;但我得到了错误。我得到了Cursor=myCursor;有什么问题,我什至更改了Cursor=myCursor;this.Cursor=myCursor;这给了我同样的错误。我试过gameform.Cursor=myCursor;但这根本不起作用。System.ArgumentException:图像格式无效。图像文件可能已损坏。参数名称:流--->System.Runtime.InteropServices.COMException(0x800A01E1):来自HRESULT的异常:0x800A01E1(CTL_E_INVALIDPICTURE)在System.Windows.Forms.UnsafeNativeMethods.IPersistStream.Load(IStreampstm)在System.Windows.Forms。Cursor.LoadPicture(IStream)---内部异常堆栈跟踪结束---在System.Windows.Forms.Cursor.LoadPicture(IStream)在WaterforMGC.gameform.Form1_Load(Objectsender,EventArgse)在C:UsersJanDocumentsVisualStudio2008ProjectsWaterforMGCWaterforMGCForm1.cs:System.Windows.Forms.Form.OnLoad(EventArgse)System.Windows.Forms.Control.CreateControl(BooleanfIgnoreVisible)System.Windows.Forms中的第39行。System.Windows.Forms.Control.WmShowWindow(Message&m)在System.Windows.Forms.Control.WndProc(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)在System.Windows.Forms.NativeWindow.Callback(IntPtrhWnd,Int32msg,IntPtrwparam,IntPtrlparam)问题出在异常的第一行说明:System.ArgumentException:InvalidimageformatTheimagefilemaybecorrupt.您确定您正在加载的图像处于未损坏状态并且与光标的图像格式兼容吗?Cursor类不支持动画光标(.ani文件)或非黑白颜色的光标。您还有其他加载光标图像的地方吗?您也许可以解决此问题以确定这里出了什么问题。您实际上可以将彩色光标加载到.Net中。你只需要使用win32。[DllImport("user32.dll",SetLastError=true,CharSet=CharSet.Auto)]internalstaticexternIntPtrLoadImage(IntPtrhinst,stringlpszName,uintuType,intcxDesired,intcyDesired,uintfuLoad);//........constintIMAGE_CURSOR=2;构造LR_LOADFROMFILE=0x00000010;IntPtripImage=LoadImage(IntPtr.Zero,@"c:mycolor.cur",IMAGE_CURSOR,0,0,LR_LOADFROMFILE);游标testCursor=newCursor(ipImage);Cursor.Current=测试光标;出于某种原因,游标类对其读取的内容非常挑剔。您可以使用WindowsAPI自己创建句柄,然后将其传递给游标类。C#://(在一个类中)publicstaticCursorActuallyLoadCursor(Stringpath){returnnewCursor(LoadCursorFromFile(path))}[System.Runtime.InteropServices.DllImport("user32.dll")]privatestaticexternIntPtrLoadCursorFromFile(字符串文件名);VB.Net:'(inaclass)'PublicSharedFunctionActuallyLoadCursor(pathAsString)AsCursorReturnNewCursor(LoadCursorFromFile(path))EndFunctionPrivateSharedFunctionLoadCursorFromFile(fileNameAsString)AsIntPtrEndFunction因为您将光标作为项目目标的资源,所以可以这样做:publicstaticCursorLoadCursorFromResource(Iconicono)//假设资源是图标,但也可以是图像或位图{//在临时文件中保存光标图标,这是通过WinAPI加载所必需的stringfileName=System.IO.Path.GetTempPath()+Guid.NewGuid().ToString()+".cur";使用(varfileStream=File.Open(fileName,FileMode.Create)){icono.Save(fileStream);}//从临时文件加载游标,使用WinAPICursorresult=newCursor(LoadCursorFromFile(fileName));//删除临时文件File.Delete(fileName);返回结果;然后,要获取光标,您只需:CursormyCursor=LoadCursorFromResource(WaterforMGC.Properties.Resources.waterspra??y);从文件中读取光标允许您处理动画或彩色光标,尽管MSDN中列出了Cursor类的限制我的答案基于另一个SO答案(并在.NET4.0上愉快地测试)。以上是C#学习教程:从资源文件中读取Cursor时,抛出ArgumentException。代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
