We are using the image resizer from imageresizing.net and are seeing some weird behaviour.
When we read an image in from a stream and then resize the image we can no longer access the properties of the original image.
The following code will reproduce the issue.
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);
}
}
}
Before the ImageBuilder line we are able to see the uploadedImage.Width fine but afterwards it throws an exception:
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:
Is there something that we are doing wrong here or could this possibly be a bug in the image resizer?
Note: the problem was originally from an asp.net mvc app that has images uploaded which is why the variable is called httpPostedFileBaseImage and we are using Image.FromStream instead of perhaps Image.FromFile
The image is
but it appears to happen on most images.
EDIT:
Tried the following after the image resizing to no avail
httpPostedFileBaseImage.Seek(0, SeekOrigin.Begin);
EDIT2:
This is what confused me

The documentation seems to suggest that "it will not be disposed unless disposeSource=true, or am I misreading this?
Don’t know how I missed it but there is an parameter to the ImageBuilder that tells it not to dispose the source
Even though this fixes it, its strange as the documentation says its false by default