{"record":{"id":"970ead7779de76b7","repo":"dotnet/wpf","slug":"sr-cursor-invalidstream","errorCode":null,"errorMessage":"SR.Cursor_InvalidStream","messagePattern":"SR\\.Cursor_InvalidStream","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Cursor.cs","lineNumber":249,"sourceCode":"                        }\n\n                        // Write any remaining bytes\n                        fileStream.Write(cursorData, offset: 0, count: dataSize);\n                    }\n                }\n\n                // This method is called with File Write permission still asserted.\n                // However, this method just reads this file into an icon.\n                _cursorHandle = UnsafeNativeMethods.LoadImageCursor(IntPtr.Zero,\n                                                                    filePath,\n                                                                    NativeMethods.IMAGE_CURSOR,\n                                                                    0, 0,\n                                                                    NativeMethods.LR_DEFAULTCOLOR |\n                                                                    NativeMethods.LR_LOADFROMFILE |\n                                                                    (_scaleWithDpi? NativeMethods.LR_DEFAULTSIZE : 0x0000));\n                if (_cursorHandle == null || _cursorHandle.IsInvalid)\n                {\n                     throw new ArgumentException(SR.Cursor_InvalidStream);\n                }\n            }\n            finally\n            {\n                try\n                {\n                    File.Delete(filePath);\n                }\n                catch(System.IO.IOException)\n                {\n                    //  We may not be able to delete the file if it's being used by some other process (e.g. Anti-virus check).\n                    // There's nothing we can do in that case, so just eat the exception and leave the file behind\n                }\n            }\n        }\n        //**** end of DEAD CODE ****//\n\n        private void LoadFromStream(Stream cursorStream)","sourceCodeStart":231,"sourceCodeEnd":267,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Cursor.cs#L231-L267","documentation":"LegacyLoadFromStream writes the stream to a temp file and loads it via LoadImage; if the resulting _cursorHandle is null or invalid, it throws ArgumentException(SR.Cursor_InvalidStream). This is the compat (quirk-flagged) stream-loading path, and the error means the stream's bytes could not be turned into a valid native cursor handle.","triggerScenarios":"new Cursor(Stream) (via LoadFromStream → LegacyLoadFromStream, quirk flag enabled) with a stream whose content is not a valid .cur/.ani image or is empty.","commonSituations":"Embedding a .cur as an assembly resource and passing the wrong manifest resource stream (getting a different/empty resource); passing a stream positioned after the data (need Seek(0)); passing non-cursor image data.","solutions":["Seek the stream to position 0 (stream.Seek(0, SeekOrigin.Begin)) before constructing the Cursor.","Verify the stream actually contains .cur/.ani bytes (check length and first bytes) rather than another resource.","Confirm the embedded resource name and that it was copied into the assembly.","Catch ArgumentException around Cursor construction and fall back to a built-in cursor."],"exampleFix":"// before\nusing var s = assembly.GetManifestResourceStream(\"App.cursor.cur\");\ncursor = new Cursor(s);\n// after\nusing var s = assembly.GetManifestResourceStream(\"App.Resources.cursor.cur\");\ns.Seek(0, SeekOrigin.Begin);\ncursor = new Cursor(s);","handlingStrategy":"validation","validationCode":"using var s = assembly.GetManifestResourceStream(resourcePath);\nif (s == null || s.Length == 0) throw new InvalidOperationException($\"Resource missing or empty: {resourcePath}\");\ns.Seek(0, SeekOrigin.Begin);\nCursor cursor = new Cursor(s);","typeGuard":"bool IsValidCursorStream(Stream s) => s != null && s.CanSeek && s.Length > 0 && s.Position == 0;","tryCatchPattern":"try { cursor = new Cursor(stream); }\ncatch (ArgumentException ex)\n{ logger.LogWarning(\"Stream is not a valid cursor: {Msg}\", ex.Message); cursor = Cursors.Default; }","preventionTips":["Always Seek(0) a stream before handing it to new Cursor(Stream).","Verify embedded resource names with assembly.GetManifestResourceNames().","Check stream length > 0 before construction.","Keep a built-in cursor as fallback for resource-loading failures."],"tags":["wpf","cursor","stream","legacy"],"backgroundTag":"invalid-argument-format","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}