2012-07-31 58 views
4

我們使用imageresizing.net中的圖像調整大小,並看到一些奇怪的行爲。ImageResizer disposing provided圖像參考

當我們從流中讀取圖像然後調整圖像大小時,我們無法再訪問原始圖像的屬性。

以下代碼將重現該問題。

static void Main(string[] args) 
     { 
      using(var httpPostedFileBaseImage = new FileStream(@"C:\test.jpg",FileMode.Open, FileAccess.Read, FileShare.Read)) 
      { 
       using(var uploadedImage = Image.FromStream(httpPostedFileBaseImage)) 
       { 

        Console.WriteLine(uploadedImage.Width); 
        var resizedImage = ImageBuilder.Current.Build(uploadedImage, 
                    new ResizeSettings("width=110;height=83")); 

        Console.WriteLine(uploadedImage.Width); 
       } 
      } 
     } 

的ImageBuilder線,我們能夠看到uploadedImage.Width不錯,但後來它拋出一個異常之前:

System.ArgumentException was unhandled 
    HResult=-2147024809 
    Message=Parameter is not valid. 
    Source=System.Drawing 
    StackTrace: 
     at System.Drawing.Image.get_Width() 
     at ConsoleApplication6.Program.Main(String[] args) in C:\Users\Daniel\Desktop\ConsoleApplication6\ConsoleApplication6\Program.cs:line 25 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 

有我們在這裏做錯了什麼或可能這可能是一個圖像調整器中的錯誤?

注:這個問題最初是從已上傳圖片的asp.net MVC應用程序這就是爲什麼變量稱爲httpPostedFileBaseImage和我們使用Image.FromStream代替或許Image.FromFile

圖像是enter image description here但它似乎發生在大多數圖像上。

編輯:

嘗試下面的圖像大小調整無果後

httpPostedFileBaseImage.Seek(0, SeekOrigin.Begin); 

EDIT2:

這是困惑我 enter image description here

的文件似乎表明, 「除非disposeSource = true,否則不會處理,還是我誤讀了?

回答

5

不知道我怎麼錯過了,但有一個參數的ImageBuilder,告訴它不要丟棄源

var resizedImage = ImageBuilder.Current.Build(uploadedImage,new ResizeSettings("width=110;height=83"),false); 

即使這個修復它,它的文檔由怪說,它的假默認

+0

我沒有告訴你在你原來的電子郵件嗎? – 2012-08-01 14:38:56

+0

xml文檔說:「如果傳遞源流,位圖或圖像實例,它將在使用後處置。使用disposeSource = False禁用該行爲。」 – 2012-08-01 14:42:58

+0

我不確定什麼是混淆。 – 2012-08-01 14:43:09

0

您是否嘗試將流位置重置爲0?如果圖像調整大小庫將其向前移動,則需要在重新使用它之前將其倒帶。

+0

試過,仍然得到相同的錯誤 – 2012-07-31 23:48:53