Why am I getting an error "Attempted to read or write protected memory." after resizing image in C#?

StackOverflow https://stackoverflow.com/questions/21373527

  •  03-10-2022
  •  | 
  •  

Question

I am using PHASH for computing the hash values for a large database of images. These images are of high resolution and hence I need to resize them for fast computing of the hash.

However, when I resize the image, the PHASH program throws an error. If I don't resize, the PHASH program works fine.

My resize code is as below.

public void ResizeImage(string ImagePath, int width, int height, string newPath)
    {
        Bitmap b = new Bitmap(width, height);
        using (Graphics g = Graphics.FromImage((Image)b))
        {
            FileStream fs = new FileStream(ImagePath, FileMode.Open);
            Image img = Image.FromStream(fs);
            g.DrawImage(img, 0, 0, width, height);
            fs.Close();
        }
        b.Save(newPath);
        b.Dispose();
    }

The error which I receive is "Attempted to read or write protected memory".

The phash code is as below:

 ph_dct_imagehash(imagePath, ref hash);

The above function calls the C++ program and returns me the hash value for that image. It works fine when the image is not resized programmatically. If I resize the image using MS Paint then also it works fine.

Was it helpful?

Solution 3

This is how I was able to resolve the answer.

public void ResizeImage(string ImagePath, int width, int height, string newPath)
{
    Bitmap b = new Bitmap(width, height);
    using (Graphics g = Graphics.FromImage((Image)b))
    {
        FileStream fs = new FileStream(ImagePath, FileMode.Open);
        Image img = Image.FromStream(fs);
        g.CompositingQuality = CompositingQuality.HighSpeed;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.CompositingMode = CompositingMode.SourceCopy;
        g.DrawImage(img, 0, 0, width, height);
        fs.Close();
    }
    b.Save(newPath);
    b.Dispose();
}

I knew there was some problem with the resize function and not the ph_dct_imagehash function.

OTHER TIPS

You don't need a FileStream to open an Image You could just use Bitmap myBmp = Bitmap.FromFile("path");

Try removing the filestream.

The problem can be found in the call to ph_dct_imagehash. This calls unmanaged code and clearly there is an error in your interop between the managed and unmanaged realms.

The code in the question has no relevance to the problem. The solution will be to find and fix the problem with your use of ph_dct_imagehash.

We cannot offer any details on how to do that since you provided no details on ph_dct_imagehash.

Got similar error doing almost exactly the same thing- creating a new bitmap from an existing one. Differences was that ours was a crop and instead of your safenative gdip line, our exception was in a different .net method in same class as yours: System.Drawing.SafeNativeMethods.Gdip.GdipDrawImageRectRectI in our case.

We were doing pretty much the same thing as you (copying a bitmap, although we cropped rather than resized it). Also we needed our bitmap in memory so we couldn't dispose of the final image, but otherwise it was same code as your original example.

Our solution was different. Even though our error was occuring post-copy in our crop stage, we used a 3rd party library that supplied the bitmap that was cropped.

To test if this was a windows issue or an issue with the library, wrote a new method to convert the original data (3 channel BGR byte data) into a BMP. This fixed the error.

Oddly, we had existing code that used the library's bitmap converter elsewhere w/no problems... The difference here was that the same image data was converted to a bitmap twice as part of a single processing run. If converted it once and cached/reused the handle rather than creating it twice in-line... this also fixed the problem.

Something to be aware of if you're using 3rd party code, it might be the culprit even if the stack trace shows it's .net.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top