Question

Suppose programmatically I was comparing between two images. After extracting the difference between two images suppose I store the difference in another Bitmap variable called bmp3

I got a code from another site which show how to merge the difference with first image.

Suppose that I have two Bitmap variables called bmp1 & bmp2. I extract programmatically the difference between two variable called bmp1 & bmp2 and store it in bmp3 variable.

Now I want to merge the difference with bmp1 variable just at the same position. So I got code from a site and which works fine but I have confusion about few line of that code.

Here is the code:

Bitmap bComb = new Bitmap(bmp3.Width, bmp3.Height);

using (Graphics g = Graphics.FromImage(bComb))
{
    g.DrawImage(this.pictureBox1.Image, 0, 0, bComb.Width, bComb.Height);
    g.DrawImage(bmp3, 0, 0, bComb.Width, bComb.Height);
}

this.pictureBox4.Image = bComb;

the meaning of this line is

Bitmap bComb = new Bitmap(bmp3.Width, bmp3.Height);

bcom is new variable which will have same size of bmp3....... am I right ?

The meaning of this line g.DrawImage(this.pictureBox1.Image, 0, 0, bComb.Width, bComb.Height); ?

We are writing picbox1 content to bcom variable from 0,0 coordinate....am I right ?

Again we are writing bmp3 content to bcom variable from 0,0 coordinate again....am I right ?

This last line is making confusion for me. why we write bmp3 variable content to bcom variable from 0,0 coordinate again....am I right ?

If we write both from 0,0 coordinate again to bcom variable then pic should over lap on each other but output is coming right. how it is being possible.

I need your help to understand those couple of line. So please discuss those line in detail and why always 0,0 coordinate is used. Please help me to understand the code. Thanks

Was it helpful?

Solution

1. bComb is new variable which will have same size of bmp3 ....... am i right ?

yes , It's a bitmap with size of bmp3 which we'll use to draw some other stuff on it but now It contains only empty pixels , nothing more !

2.

using (Graphics g = Graphics.FromImage(bComb))

this line gives us the ability to draw whatever we need on bComb it can be a bitmap or a rectangle or simply anything !

3.

g.DrawImage(this.pictureBox1.Image, 0, 0, bComb.Width, bComb.Height);

here it draws the this.pictureBox1.Image to the bitmap (bComb) from point (0,0) to (bComb.Width,bComb.Height) which will contain the whole bitmap indeed

4.

g.DrawImage(bmp3, 0, 0, bComb.Width, bComb.Height);

bmp3 is being drawn to bComb here again from point (0,0) to (bComb.Width,bComb.Height)

the bitmaps are being drawn on each other from the same point and with the same size , the point is if they contain transparent pixels the result will be a combination of those bitmaps , otherwise you will just see the bmp3 which was drawn later and no clue of the first image , and we use (0,0) because it's the beginning point of our bitmap , we could use any other point to draw our stuff but in this way we leave some pixels behind so why would we ? :)

  • Update : It's totally possible to draw images over each other and you're right if they have transparent pixels the result will be like marge , but as you mentioned if the last bitmap does not contain any transparent pixels so the only picture we're gonna see is the last one and we won't be able to see the previous bitmaps because those pixels are over-written by the last bitmap feel free to to use some png semi-transparent images and drawing them on top of each other and see the result , and I'm afraid , I don't know any built-in gdi+ function for image comparison but you can do it easily by GetPixel() function in gdi+ and comparing each pixel from one bitmap to another or you may use unsafe code and access memory directly it totally depends on you but If you want a clean and easy way go with GetPixel() ! you may also take a look at Intel's open-cv library for image processing , hope it helps

OTHER TIPS

Bitmap bComb = new Bitmap(bmp3.Width, bmp3.Height); Here we create an empty image (bComb) which has the same size as bm3.

using (Graphics g = Graphics.FromImage(bComb)) This creates a new graphic, which will be using the empty image bComb. Anything drawn with graphics, will be applied onto bComb directly.

g.DrawImage(this.pictureBox1.Image, 0, 0, bComb.Width, bComb.Height); This draws the image off the pictureBox onto bComb, and stretches it to match the image sizes.

g.DrawImage(bmp3, 0, 0, bComb.Width, bComb.Height); This draws bm3 onto bComb, on top of the picturebox image we drew before. What this looks like, depends on whether this image has transparency or not. Assuming it comes our looking correct, this probably means bm3 has an alpha value (transparency) so it will act as a sort of filter over the previous image, resulting is what appears to be a merger of the two.

EDIT: Also, some libraries you may want to look into: Accord.Net (which is an extension of Aforge.Net. The combination of these two covers almost any image filter,comparison or effect you would need. Another good one is EMGU CV which is a C# wrapper of the OpenCV library mentioned in the other answer.

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