1

i want to create an application that delete the duplicated photo in some folder so i need to convert an image to an ARGB array [one diemnsion] then compare them . What is the fastest way to convert an Image to ARGB array? or how to improve this code ?

 Image img;
 using (FileStream stream = new FileStream(imagefile, FileMode.Open))
            {
                img = Image.FromStream(stream);
            }

 using (Bitmap bmp = new Bitmap(img))
            {
                int[] argb = new int[bmp.Width * bmp.Height];
                for (int x = 0; x < bmp.Width; x++)
                {

                    for (int y = 0; y < bmp.Height; y++)
                    {

                        Color pxl = bmp.GetPixel(x, y);

                        argb[x * y] = pxl.ToArgb();



                    }
                }
              }
  • You'll want to be using LockBits() to lock a whole region in memory rather than GetPixel(), which is comparatively inefficient. – adv12 Jun 17 '14 at 18:22
  • how to do it could you give me an example please – user3749485 Jun 17 '14 at 18:22
  • Examples are readily available in the MSDN documentation for the Bitmap class. – adv12 Jun 17 '14 at 18:25
  • 1
    http://bit.ly/1qpiUzc – Meirion Hughes Jun 17 '14 at 18:25
  • @MeirionHughes, that's awesome. – adv12 Jun 17 '14 at 18:27
  • 1
    If you're looking to compare files for exact duplicates could just use a hashing function (see here: http://stackoverflow.com/questions/10520048/calculate-md5-checksum-for-a-file/10520086#10520086) – rsbarro Jun 17 '14 at 18:28
  • @rsbarro files may have different md5 but same ARGB color array ,isn't it? – user3749485 Jun 17 '14 at 18:35
  • @user3749485, theoretically, yes. But if you know enough about the process by which the duplicates were created to say, for instance, that they're just the product of a file copy operation, you can save yourself the work of looking into the image contents. – adv12 Jun 17 '14 at 18:39
  • @user3749485 yes, possibly I guess if there were two images that were exactly the same but had different file formats. If the files are of the same format, I don't think so but I can't say for 100% certain. – rsbarro Jun 17 '14 at 18:40
  • so i want my application work without dependance on the file format and for that i want to look at image content – user3749485 Jun 17 '14 at 18:42
  • @user3749485, be aware that many image formats are lossy, so for instance if you convert a PNG to a JPEG, the pixel data won't be exactly the same; the JPEG will be just an approximation of the PNG. The same goes for loading a JPEG and saving it as a different JPEG. Each time you save via a lossy algorithm, more information is lost and the less the exported image matches the original. – adv12 Jun 17 '14 at 18:46
  • @adv12 as i understand from you that i need way to compare the two array to likely equal not exactly eqaul ,, is that true ??! – user3749485 Jun 17 '14 at 18:51
  • @user3749485, If you're going to be comparing images in a lossy file format to their originals, then yes, you'll need some way to find images that are "like" each other (same dimensions, pixel values "nearly equal", etc.) But I don't think the comparison logic will be trivial. – adv12 Jun 17 '14 at 18:55
  • @Adv12 so do you have a way to implement it or any idea to do it ?! oh and thanks for your suggestion about LockBit it work as perfect – user3749485 Jun 17 '14 at 19:32
  • Nope, I don't have any idea how to do it. Good luck. – adv12 Jun 17 '14 at 19:35

1 Answers1

0

After convert Image to Bitmap, translucensy have value 255 for all pixels...