0

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.

4

4 回答 4

0

这就是我能够解决答案的方式。

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();
}

我知道 resize 函数而不是 ph_dct_imagehash 函数存在一些问题。

于 2014-02-04T05:46:00.357 回答
0

问题可以在对 的调用中找到ph_dct_imagehash。这会调用非托管代码,显然托管和非托管领域之间的互操作存在错误。

问题中的代码与问题无关。解决方案是找到并解决您使用ph_dct_imagehash.

我们无法提供有关如何执行此操作的任何详细信息,因为您没有提供有关ph_dct_imagehash.

于 2014-02-03T11:55:52.877 回答
0

您不需要 aFileStream打开图像您可以使用Bitmap myBmp = Bitmap.FromFile("path");

尝试删除文件流。

于 2014-01-27T05:40:15.270 回答
0

在做几乎完全相同的事情时遇到了类似的错误——从现有位图创建新位图。不同之处在于我们的方法是作物,而不是您的安全 gdip 行,我们的异常在与您相同的类中的不同 .net 方法中:在我们的例子中是 System.Drawing.SafeNativeMethods.Gdip.GdipDrawImageRectI 。

我们做的事情和你几乎一样(复制位图,尽管我们裁剪而不是调整它的大小)。此外,我们需要内存中的位图,因此我们无法处理最终图像,否则它与原始示例的代码相同。

我们的解决方案不同。即使我们的错误发生在我们的裁剪阶段复制后,我们还是使用了一个第三方库来提供被裁剪的位图。

为了测试这是 Windows 问题还是库问题,编写了一种新方法将原始数据(3 通道 BGR 字节数据)转换为 BMP。这修复了错误。

奇怪的是,我们现有的代码在其他地方使用库的位图转换器没有问题......这里的区别在于,作为单个处理运行的一部分,相同的图像数据被转换为位图两次。如果将其转换一次并缓存/重用句柄,而不是在线创建两次......这也解决了问题。

如果您使用的是 3rd 方代码,需要注意的是,即使堆栈跟踪显示它是 .net,它也可能是罪魁祸首。

于 2017-02-11T05:03:44.940 回答