c# – 从资源文件读取Cursor时,会抛出一个ArgumentException异常

前端之家收集整理的这篇文章主要介绍了c# – 从资源文件读取Cursor时,会抛出一个ArgumentException异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我使用MemoryStream从资源文件加载游标时,我收到一个ArgumentException.这是我用来加载光标的代码
Cursor myCursor
    = new Cursor(new MemoryStream(WaterforMGC.Properties.Resources.waterspray));
Cursor = myCursor;

但是我收到错误.我不知道是什么错,我甚至改变了Cursor = myCursor; to 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)

解决方法

问题在例外的第一行中阐明:

System.ArgumentException: Image format is not valid. The image file
may be corrupted.

您确定要加载的图像处于未破坏的状态,为compatible with the image format for cursors?

The Cursor class does not support animated cursors (.ani files) or cursors with colors other than black and white.

你有任何其他地方加载光标图像,它的工作原理?你可能能够解决这个问题.

原文链接:https://www.f2er.com/csharp/96142.html

猜你在找的C#相关文章