创建的BitmapImage当ASP.NET抛出Win32Exception,找不到指定文件 [英] ASP.NET Throws Win32Exception when creating BitmapImage, cannot find file specified

查看:252
本文介绍了创建的BitmapImage当ASP.NET抛出Win32Exception,找不到指定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一本书,观看网站,需要相当大的图像(向上77+百万像素),并提供图像之前缩放下来。它基本上下面的算法:

I have been developing a book viewing website that takes rather large images (upwards of 77+ megapixels) and scales them down before serving the image. It basically does the following algorithm:


  1. 如果请求的书已经被缓存,服务缓存图像。

  2. 如果这本书还没有被缓存,这本书加入到我们的书缓存线程池。

  3. 这本书缓存线程池增加了每本书的页面,我们的页面缓存的线程池。

  4. 页面缓存线程池通过一遍又一遍减半图像,直到我们缓存图像2倍,4倍,8倍,16倍,32倍缩小版本缓存图像(位于我们的局域网上的服务器上),以及64倍

下面是code二等分,单独的图像。注意使用的BitmapImage的:

Here is the code that halves an individual image. Note the use of BitmapImage:

Private Shared Function HalveImage(ByVal baseImagePath As String, ByVal baseWidth As Integer, ByVal baseHeight As Integer, ByVal newImagePath As String) As BitmapImage
    Dim bi As New BitmapImage

    With bi
        .BeginInit()
        .UriSource = New Uri(baseImagePath)
        .DecodePixelWidth = baseWidth / 2 'only seting one DecodePixelXXX property preserves the aspect ratio
        .EndInit()
    End With

    Dim enc As New System.Windows.Media.Imaging.JpegBitmapEncoder
    With enc
        .QualityLevel = 50
        .Frames.Add(BitmapFrame.Create(bi))
    End With

    Using stream As New IO.FileStream(newImagePath, IO.FileMode.Create)
        enc.Save(stream)
    End Using

    Return bi
End Function

这code是我开发的机器上工作正常,但是当我安装了它到服务器该服务器会突然停下来缓存图像,缓存后,数百名。该网站的其余部分继续正常工作。我们发现,页面缓存code会在每次尝试创建的BitmapImage对象时最终抛出此异常:

This code was working fine on my dev machine, but when I installed it onto a server that server would suddenly stop caching images, after caching hundreds. The rest of the website continued to work fine. We found that the page caching code would eventually throw this exception every time it tried to create the BitmapImage object:

System.ComponentModel.Win32Exception: The system cannot find the file specified
   at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
   at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
   at MS.Win32.MessageOnlyHwndWrapper..ctor()
   at System.Windows.Threading.Dispatcher..ctor()
   at System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
   at System.Windows.Freezable..ctor()
   at System.Windows.Media.Imaging.BitmapSource..ctor(Boolean useVirtuals)
   at System.Windows.Media.Imaging.BitmapImage..ctor()
   at RemoteFSTester.PageCachingThreadPool.WICResizer.CachePage(String id, Int32 item, Int32 index) in C:\Users\[...]\PageCachingThreadPool.vb:line 127
   at RemoteFSTester.PageCachingThreadPool.CachePage(String id, Int32 item, Int32 index) in C:\Users\[...]\PageCachingThreadPool.vb:line 103
   at RemoteFSTester.PageCachingThreadPool.WorkerMethod(PriorityThreadPoolDelegateArgs args) in C:\Users\[...]\PageCachingThreadPool.vb:line 91
   at MDSA.Util.PriorityThreadpool.PriorityThreadPoolBase.Thread_DoWork(PriorityThreadPoolDelegateArgs args) in C:\Users\[...]\PriorityThreadPoolBase.vb:line 199

虽然异常说找不到指定文件',我可以直接去到基本映像所在的位置,并打开该文件自己。

Even though the exception says 'cannot find the file specified', I can go straight to where the base image is located and open the file myself.

注:只是为了澄清,该房屋的code和缓存的图片是不是房子的基础映像的服务器的服务器。在code服务器抓起通过URI像'从基础映像服务器\\服务器名\\路径\\ filename.jpg的文件。

在运行一些测试后,例外只是想通过的BitmapImage对象打开我们的IIS服务器上的图像时发生。在BitmapImage的例外情况是否通过我设置UriSource文件路径或作出FileStream对象和BitmapImage的的StreamSource的属性设置为它。如果文件是通过FileStream对象打开它们不会发生,也不如果文件是通过一个控制台应用程序打开它们发生这样做。使用的BitmapImage对象还控制台应用程序运行没有问题。

After running some tests, the exceptions only occur when trying to open images on our IIS server via the BitmapImage object. The exception in BitmapImage happens whether I set the file path via UriSource or make a FileStream object and set the BitmapImage's StreamSource property to it. They don't occur if the files are opened via a FileStream object, nor do they occur if the files are opened via a console application. A console application using the BitmapImage object also runs without problem.

所以,到最后问我的问题,为什么服务器有麻烦通过ASP.NET缓存这些图像,而我的开发机器有没有问题?

So, to finally ask my question, why does the server have trouble caching these images via ASP.NET while my dev machine has no problem?

推荐答案

我找到了解决问题就在这里:
<一href=\"http://connect.microsoft.com/VisualStudio/feedback/details/620588/system-componentmodel-win32exception-0x80004005-not-enough-storage-is-available-to-process-this-command\"相对=nofollow> Microsoft连接反馈

I found the solution to the problem here: Microsoft Connect Feedback

长话短说,我不得不添加以下code其中,将在每一个线程过程结束运行:

Long story short, I had to add the following code where it would run at the end of every thread process:

Dim myDispatcher As Dispatcher = Dispatcher.CurrentDispatcher
myDispatcher.BeginInvokeShutdown(DispatcherPriority.Normal)
Dispatcher.Run()

这篇关于创建的BitmapImage当ASP.NET抛出Win32Exception,找不到指定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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