Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
AI OnAI Off
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Can you try with
System.Drawing.Image.FromStream(stream, false, false);
this ignore the format of the stream
Setting validateImageData boolean FALSE has the same effect: large PNG files throw an error, whereas small PNG files and JPG files work fine.
Thanks Valdis Iljuconoks; what would I have to convert into what in that case?
if I skip my laziness and think once more, there could be 2 options to solve this:
1) using image converter:
using (var stream = img.BinaryData.OpenRead()) { var ic = new System.Drawing.ImageConverter(); var img = (System.Drawing.Image)ic.ConvertFrom(stream.ReadAllBytes()); }
2) or try to load up binary data into memory stream first:
using (var stream = img.BinaryData.OpenRead()) { using(var memStream = new MemoryStream()) { stream.CopyTo(memStream); memStream.Position = 0; var image = System.Drawing.Image.FromStream(memStream, false); image.Dispose(); } }
however, just to note that I was able to load up 1Mb png image using your code fragment above..
When I try to use a Stream created using [media].BinaryData.OpenRead(), I get the following exception trying to load that stream into an Image object:
Parameter is not valid.
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at System.Drawing.Image.FromStream(Stream stream)
at ###.Controllers.HomePageController.LoadImages() in ###\HomePageController.cs:line 48
This only occurs for PNG files that are bigger than usual (in this case a file of 4.815 Kb), JPG files work fine.
Here is the (simplified) sample code:
var content = ServiceLocator.Current.GetInstance().Get(imageContentReference);
If I try to load the exact same image using native .NET code it also works fine: