SaaS CMS has officially launched! Learn more now.

'Parameter is not valid' exception when using PNG stream from OpenRead()

Vote:
 

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);
var imageData = content as IContentImage;

try
{     using (var stream = imageData.BinaryData.OpenRead())     {         var image = System.Drawing.Image.FromStream(stream, false); // exception is thrown         ... do stuff with image properties         image.Dispose();     } } catch (Exception ex) {     Log.Error(ex.Message, ex); }


If I try to load the exact same image using native .NET code it also works fine:

try
{
    using (FileStream stream = System.IO.File.Open("C:\\Users\\Me\\Downloads\\error-image.png"FileMode.Open))
    {
        var image = System.Drawing.Image.FromStream(stream, false);
        image.Dispose();
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

What could be causing this?
#197575
Edited, Oct 08, 2018 14:11
Vote:
 

Can you try with 

System.Drawing.Image.FromStream(stream, false, false);

this ignore the format of the stream 

#197577
Oct 08, 2018 14:57
Vote:
 

Setting validateImageData boolean FALSE has the same effect: large PNG files throw an error, whereas small PNG files and JPG files work fine.

#197578
Oct 08, 2018 15:10
Vote:
 

what about trying out with `System.Drawing.ImageConverter` ?

#197579
Oct 08, 2018 15:22
Vote:
 

Thanks Valdis Iljuconoks; what would I have to convert into what in that case?

#197612
Oct 09, 2018 8:11
Vote:
 

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..

#197632
Oct 09, 2018 9:42
Vote:
 

using a new MemoryStream did the trick!

#197654
Oct 09, 2018 15:12
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.