ImageResizer配置提供了Image引用

时间:2012-07-31 06:58:21

标签: .net image-processing imageresizer

我们正在使用来自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行之前,我们可以看到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,否则不会被处理,或者我误读了这个?

2 个答案:

答案 0 :(得分:5)

不知道我是如何错过它的,但ImageBuilder有一个参数告诉它不要处理源

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

即使这样修复它,但它的奇怪之处在于文档默认为假

答案 1 :(得分:0)

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