C# 在MVC中使用System.Windows.Media.Imaging会导致;“操作已成功完成”;错误

C# 在MVC中使用System.Windows.Media.Imaging会导致;“操作已成功完成”;错误,c#,asp.net,asp.net-mvc,wpf,gdi,C#,Asp.net,Asp.net Mvc,Wpf,Gdi,我们正在使用WPF库来调整上传到MVC操作的图像的大小,我们会定期收到一个错误,上面写着“操作成功完成”。错误发生在下面代码中的“new DrawingVisual()”行上 public static BitmapFrame ResizeWithExcess(this BitmapFrame photo, int width, int height) { double dpiX = photo.DpiX == 0 ? 96 : photo.DpiX; double dpiY =

我们正在使用WPF库来调整上传到MVC操作的图像的大小,我们会定期收到一个错误,上面写着“操作成功完成”。错误发生在下面代码中的“new DrawingVisual()”行上

public static BitmapFrame ResizeWithExcess(this BitmapFrame photo, int width, int height)
{
    double dpiX = photo.DpiX == 0 ? 96 : photo.DpiX;
    double dpiY = photo.DpiY == 0 ? 96 : photo.DpiY;

    var targetVisual = new DrawingVisual();
    using (var targetContext = targetVisual.RenderOpen())
    {
        targetContext.DrawRectangle(Brushes.White, null, new Rect(0, 0, width * 96 / dpiX, height * 96 / dpiY));
        var x = ((width - (photo.Width * dpiX / 96)) / 2) * 96 / dpiX;
        var y = ((height - (photo.Height * dpiY / 96)) / 2) * 96 / dpiY;
        var w = photo.Width;
        var h = photo.Height;
        targetContext.DrawImage(photo, new Rect(x, y, w, h));
        targetContext.Close();
    }

    var target = new RenderTargetBitmap(width, height, dpiX, dpiY, PixelFormats.Default);
    target.Render(targetVisual);

    var targetFrame = BitmapFrame.Create(target);
    if (targetVisual.Dispatcher != null) targetVisual.Dispatcher.InvokeShutdown();
        return targetFrame;
} 
根据TaskManager,w3wp进程的句柄数大约为35k。线程数约为270,“GDI”为0。我尝试过在BitmapFrame.Create(target)之后强制dispatcher启动关机,还添加了GC代码,但这些似乎都没有效果。目前唯一的解决方案是回收应用程序池

我在谷歌上做了一些搜索,我看到的主要是WPF应用程序,它们是桌面应用程序或WPF XAML控件。请注意,如果您想查看调用此方法的任何代码,我可以提供它。我基本上是使用多个任务一次创建多个调整大小的版本,并使用“等待”来观察它们是否完成。我也尝试过处理这项任务,但似乎什么都没做

编辑:我们还看到,在“操作成功完成”错误流出现之前,有时会出现一个错误,即“没有足够的存储空间来处理此命令”