2012-07-31 46 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