当读取从资源光标文件将引发ArgumentException光标、文件、资源、ArgumentException

2023-09-04 00:39:03 作者:等我熬过所有苦难

当我使用的MemoryStream 加载光标从一个资源文件,我收到 ArgumentException的。这里是code我使用加载光标:

 光标myCursor
    =新的光标(新的MemoryStream(WaterforMGC.Properties.Resources.w​​aterspra​​y));
光标= myCursor;
 

但我得到的错误。我不知道什么是错的,我甚至修改光标= 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(的IStream PSTM)
   在System.Windows.Forms.Cursor.LoadPicture(的IStream流)
   ---内部异常堆栈跟踪的结尾---
   在System.Windows.Forms.Cursor.LoadPicture(的IStream流)
   在WaterforMGC.gameform.Form1_Load(对象发件人,EventArgs e)在C:\用户\月\文档\ Visual Studio 2008的\项目\ WaterforMGC \ WaterforMGC \ Form1.cs中:行39
   在System.Windows.Forms.Form.OnLoad(EventArgs的五)
   在System.Windows.Forms.Control.CreateControl(布尔fIgnoreVisible)
   在System.Windows.Forms.Control.CreateControl()
   在System.Windows.Forms.Control.WmShowWindow(消息&M)
   在System.Windows.Forms.Control.WndProc(消息&M)
   在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&M)
   在System.Windows.Forms.NativeWindow.Callback(IntPtr的的HWND,味精的Int32,IntPtr的WPARAM,IntPtr的LPARAM)
怎样读取ISO格式的文件

解决方案

问题是规定了对异常的第一行:

  

System.ArgumentException:图像格式是无效。图像文件   可能会被破坏。

您确定您正在加载的图像是一个廉洁的状态,并且是compatible与光标的图像格式?

  

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

其他游标

你有任何其他地方,你加载一个光标图像和它的作品?你也许可以工作过的,要确定发生了什么事情错在这里。

When I use a MemoryStream to load a Cursor from a resources file I receive an ArgumentException. Here is the code I use to load the cursor:

Cursor myCursor
    = new Cursor(new MemoryStream(WaterforMGC.Properties.Resources.waterspray));
Cursor = myCursor;

But I get the error. I have no idea what is wrong, I even changed Cursor = myCursor; to this.Cursor = myCursor; which gave me the same error. I tried gameform.Cursor = myCursor; but that didn't work at all.

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)

解决方案

The problem is spelled out on the very first line of the exception:

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

Are you sure the image you're loading is in an uncorrupted state, and is 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.

Do you have any other places where you load a cursor image and it works? You might be able to work off of that to determine what's going wrong here.