Dan*_*ell 5 .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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在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:
Run Code Online (Sandbox Code Playgroud)
我们在这里做错了什么,或者这可能是图像缩放器中的错误?
注意:问题最初来自上传图像的asp.net mvc应用程序,这就是为什么变量名为httpPostedFileBaseImage,我们使用的是Image.FromStream而不是Image.FromFile
图像是
但它似乎发生在大多数图像上.
编辑:
在图像调整大小后无用,尝试以下操作
httpPostedFileBaseImage.Seek(0, SeekOrigin.Begin);
Run Code Online (Sandbox Code Playgroud)
EDIT2:
这让我很困惑

文档似乎暗示"除非disposeSource = true,否则它不会被处理,或者我误读了这个?
不知道我是怎么错过它的,但是ImageBuilder有一个参数告诉它不要处理源代码
var resizedImage = ImageBuilder.Current.Build(uploadedImage,new ResizeSettings("width=110;height=83"),false);
Run Code Online (Sandbox Code Playgroud)
即使这样修复它,它的奇怪之处在于默认情况下文档说它是假的
| 归档时间: |
|
| 查看次数: |
689 次 |
| 最近记录: |