2011-07-12 48 views
9

當我使用MemoryStream從資源文件加載Cursor時,我收到一個ArgumentException。這裏是我用來加載光標的代碼:當從資源文件讀取遊標時拋出ArgumentException

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

但是我得到錯誤。我不知道什麼是錯的,我甚至將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) 
+0

你的圖片格式是什麼? –

+0

異常消息有讀取......顯然你沒有這樣做,因爲你改變了行後的東西,導致異常修復錯誤... –

+0

@丹:我懷疑他知道什麼他正在尋找那個巨大的例外文本牆。我削減了它,希望它會有所幫助。 – user7116

回答

11

的問題闡述了對異常的第一行:

System.ArgumentException:圖片格式不正確。圖像文件 可能已損壞。

你確定你加載圖像處於未損壞狀態,是compatible with the image format for cursors?

光標類不支持動畫光標(.ANI文件)顏色或遊標除了黑色和白色

你有沒有其他地方加載光標圖像,它的工作原理?你可能能夠解決這個問題,以確定這裏出了什麼問題。

+0

是的,我確定。我爲我的光標使用了一個.cur文件。我不知道什麼是錯的...而且我從來沒有在C#中加載光標# – janj

+2

@janj:[per MSDN](http://msdn.microsoft.com/zh-cn/library/system.windows.forms。 cursor%28v = VS.80%29.aspx),「Cursor類不支持動畫光標(.ani文件)或具有非黑白色彩的光標。」他們是動畫還是彩色? – user7116

+0

@sletterlettervariables它是彩色的。所以這意味着我不能使用它... – janj

3

由於某些原因,遊標類對讀取的內容太挑剔。您可以使用Windows API自己創建句柄,然後將其傳遞給遊標類。

C#:

//(in a class) 
public static Cursor ActuallyLoadCursor(String path) { 
    return new Cursor(LoadCursorFromFile(path)) 
} 
[System.Runtime.InteropServices.DllImport("user32.dll")] 
private static extern IntPtr LoadCursorFromFile(string fileName); 

VB.Net:

'(in a class)' 
Public Shared Function ActuallyLoadCursor(path As String) As Cursor 
    Return New Cursor(LoadCursorFromFile(path)) 
End Function 
<System.Runtime.InteropServices.DllImport("user32.dll")> 
Private Shared Function LoadCursorFromFile(fileName As String) As IntPtr 
End Function 
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; 
+1

如何從內存流中做到這一點?流來自assembly.GetManifestResourceStream(... – Pedro77

0

因爲你有你的光標作爲該項目的資源,你可以這樣做:

[DllImport("User32.dll", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)] 
private static extern IntPtr LoadCursorFromFile(String str); 

public static Cursor LoadCursorFromResource(Icon icono) // Assuming that the resource is an Icon, but also could be a Image or a Bitmap 
{ 
    // Saving cursor icon in temp file, necessary for loading through Win API 
    string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".cur"; 
    using (var fileStream = File.Open(fileName, FileMode.Create)) 
    { 
     icono.Save(fileStream); 
    } 

    // Loading cursor from temp file, using Win API 
    Cursor result = new Cursor(LoadCursorFromFile(fileName)); 

    // Deleting temp file 
    File.Delete(fileName); 

    return result; 
} 

然後,讓光標,你只是做:

Cursor myCursor = LoadCursorFromResource(WaterforMGC.Properties.Resources.waterspray); 

閱讀儘管MSDN中爲Cursor類列出了一些限制,但使用Win API通過指針指向文件的遊標允​​許您處理動畫或顏色遊標。

我的回答是基於this another SO answer(並愉快地在.NET 4.0上測試過)。