当读取从资源光标文件将引发ArgumentException [英] When reading a Cursor from a resources file an ArgumentException is thrown

查看:237
本文介绍了当读取从资源光标文件将引发ArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用的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)

解决方案

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

  

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

您确定您正在加载的图像是一个廉洁的状态,并且是<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor%28v=VS.80%29.aspx">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.

这篇关于当读取从资源光标文件将引发ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆