From 46df3cb5cb7cff93dd5978f53a4c96a98f7e275c Mon Sep 17 00:00:00 2001 From: Apoorv Darshan Date: Tue, 17 Feb 2026 12:55:41 +0530 Subject: [PATCH] Avoid calling GetTempFileName in BitmapDownload Replace the Win32 GetTempFileName P/Invoke with Path.GetTempFileName, whose .NET 8+ implementation avoids the 65k file limit by generating random filenames internally. Fixes #259 --- .../System/Windows/Media/Imaging/BitmapDownload.cs | 12 ++++-------- .../Shared/MS/Win32/UnsafeNativeMethodsOther.cs | 14 -------------- 2 files changed, 4 insertions(+), 22 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDownload.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDownload.cs index c9bdddf6134..29455ece6d9 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDownload.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDownload.cs @@ -9,7 +9,6 @@ using MS.Internal; using System.Net; using System.Net.Cache; -using System.Text; using MS.Win32; using Microsoft.Win32.SafeHandles; @@ -111,16 +110,13 @@ Stream stream entry.inputUri = uri; entry.inputStream = stream; - string cacheFolder = MS.Win32.WinInet.InternetCacheFolder.LocalPath; bool passed = false; - // Get the file path - StringBuilder tmpFileName = new StringBuilder(NativeMethods.MAX_PATH); - MS.Win32.UnsafeNativeMethods.GetTempFileName(cacheFolder, "WPF", 0, tmpFileName); - try { - string pathToUse = tmpFileName.ToString(); + // Use Path.GetTempFileName whose .NET 8+ implementation avoids the + // Win32 GetTempFileName 65k file limit by generating random filenames. + string pathToUse = Path.GetTempFileName(); SafeFileHandle fileHandle = MS.Win32.UnsafeNativeMethods.CreateFile( pathToUse, dwDesiredAccess: NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, @@ -135,7 +131,7 @@ Stream stream { throw new Win32Exception(); } - + entry.outputStream = new FileStream(fileHandle, FileAccess.ReadWrite); entry.streamPath = pathToUse; passed = true; diff --git a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsOther.cs b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsOther.cs index d2182c16b2b..5c8cb734e81 100644 --- a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsOther.cs +++ b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsOther.cs @@ -14,20 +14,6 @@ namespace MS.Win32 { internal partial class UnsafeNativeMethods { - [DllImport(ExternDll.Kernel32, CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "GetTempFileName")] - internal static extern uint _GetTempFileName(string tmpPath, string prefix, uint uniqueIdOrZero, StringBuilder tmpFileName); - - internal static uint GetTempFileName(string tmpPath, string prefix, uint uniqueIdOrZero, StringBuilder tmpFileName) - { - uint result = _GetTempFileName(tmpPath, prefix, uniqueIdOrZero, tmpFileName); - if (result == 0) - { - throw new Win32Exception(); - } - - return result; - } - [DllImport(ExternDll.Shell32, CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)] internal static extern int ExtractIconEx( string szExeFileName,