【问题标题】:ImageResizer disposing provided Image referenceImageResizer 处理提供的图像参考
【发布时间】:2012-07-28 21:42:00
【问题描述】:

我们正在使用来自 imageresizing.net 的图像调整器,并且看到了一些奇怪的行为。

当我们从流中读取图像然后调整图像大小时,我们无法再访问原始图像的属性。

以下代码将重现该问题。

 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 行之前,我们可以看到 uploadImage.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

图片是,但它似乎出现在大多数图片上。

编辑:

调整图片大小后尝试以下方法无济于事

httpPostedFileBaseImage.Seek(0, SeekOrigin.Begin);

编辑2:

这让我很困惑

文档似乎暗示“除非 disposeSource=true,否则不会处理它,还是我误读了这个?

【问题讨论】:

    标签: .net image-processing imageresizer


    【解决方案1】:

    不知道我是怎么错过的,但是 ImageBuilder 有一个参数告诉它不要处理源代码

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

    尽管这修复了它,但它很奇怪,因为文档说默认情况下它是错误的

    【讨论】:

    • 我不是在您原来的电子邮件中告诉过您吗?
    • xml 文档说“如果传递了源流、位图或图像实例,它将在使用后被释放。使用 disposeSource=False 禁用该行为。”
    • 我不确定是什么令人困惑。
    • 啊,您正在查看文档以了解不同的过载情况。相反的说法更有意义,我会改变它。
    • 我得到了同样的错误信息,甚至相关的堆栈跟踪也是一样的。我正在使用延迟初始化。我正在图片框中查看图像,到目前为止一切都很好。但是当我尝试在面板中查看绘制此图像时,我收到此错误消息。有没有可能与Lazy有关?
    【解决方案2】:

    您是否尝试将流位置重置为 0 ?如果您的图像大小调整库将其向前移动,则需要在再次使用之前将其倒回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 2017-10-29
      相关资源
      最近更新 更多