I want to get the difference between two images
I use this source code:
public Bitmap GetDifference(Bitmap bm1, Bitmap bm2)
{
if (bm1.Size != bm2.Size)
throw new ArgumentException("exception");
var resultImage = new Bitmap(bm1.Width, bm1.Height);
using (Graphics g = Graphics.FromImage(resultImage))
g.Clear(Color.Transparent);
for (int w = 0; w < bm1.Width; w++)
{
for (int h = 0; h < bm1.Height; h++)
{
var bm2Color = bm2.GetPixel(w, h);
if (IsColorsDifferent(bm1.GetPixel(w, h), bm2Color))
{
resultImage.SetPixel(w, h, bm2Color);
}
}
}
return resultImage;
}
bool IsColorsDifferent(Color c1, Color c2)
{
return c1 != c2;
}
This source works good for me, but I have following issues:
For exaple if I choose image with a resolution 480×320 my resultImage has the same resolution (and this is corect, because I set in when created new Bitmap), but my Images have transparent color and I want to get result images with a solid color only.
Let us say – if the solid result images (solid color pixel) in 480×320 have 100×100 field I should to get just this solid Bitmap with a resolution 100×100.
To put it simply I need to get only rectangle that bordered with a solid color and cutt of alpha chanel.
Thanks!
Please see the class that I wrote below:
I think, this class can be modified to optimize, but now it works good. If you need return image with original size and without alpha channel – this is one of many solutions how to do it.