JaCraig / Structure.Sketching

Image processing library for use in .Net applications that supports .Net Core.
Apache License 2.0
84 stars 5 forks source link

AccessViolationException thrown when trying to create Image from Stream #30

Closed gpasnik closed 7 years ago

gpasnik commented 7 years ago

Hi, I have an issue when I try to create Image object from Stream. Here is the code:

public class FileValidator : IFileValidator
{
    public bool IsImage(File file)
    {
        try
        {
            //using (var stream = new MemoryStream(file.Bytes))
            //{
            //    var image = Image.FromStream(stream);
            //    return image.Width > 0 && image.Height > 0;
            //}
            using (var stream = new MemoryStream(file.Bytes))
            {
                var image = new Image(stream);
                return image.Width > 0 && image.Height > 0;
            }
        }
        catch (Exception exception)
        {
            return false;
        }
    }
}

File is the class that contains image's data

public class File
{
    public string Name { get; protected set; }
    public string ContentType { get; protected set; }
    public byte[] Bytes { get; protected set; }
    public long SizeBytes => Bytes.Length;
}

And this is the exception that I get when I create Image object "var image = new Image(stream);"

An exception of type 'System.AccessViolationException' occurred in Structure.Sketching.dll but was not handled in user code

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I also attached the image I used, when this exception occured. 22291968

Can you take a look on this issue? I've checked the same code with different library: "CoreCompat.System.Drawing" and there it was fine. (only thing I had to change was to replace line

var image = new Image(stream);

with

var image = Image.FromStream(stream);
JaCraig commented 7 years ago

Sorry on the delay for replying on this. New kid born on the day you posted the bug. Running on no sleep at the moment. That said I've run into a similar issue and think I know where the bug is for this one. Couple of questions though, what OS/system are you running the code on? The code base is having a number of issues on Linux at the moment and I've yet to try in anything like say Unity. So any info you can give on the system to test with would be a great help.

JaCraig commented 7 years ago

Believe I found the issue. The PNG in question was a 48 bit image, 16bit per channel, no alpha. The PNG True Color reader was assuming 32bit by default when reading the scan lines. Changed it to take in to account the bytes per channel and bytes per pixel when reading it. So that should fix that issue.

gpasnik commented 7 years ago

To the question about OS, I'm running on both - Windows 10 x64 and Linux Mint 18. Thanks for efforts! I'll try it after update.