Click here to Skip to main content
15,886,104 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Help!
The subtraction of two images by pixel gives me an error:
C#
int h, w;
h = pictureEdit1.Image.Height;
w = pictureEdit1.Image.Width;
Bitmap b = new Bitmap(h,w );


Bitmap p1 = (Bitmap )pictureEdit1.Image;
Bitmap p2 = (Bitmap)pictureEdit2.Image;
try
{
    for (int x = 0; x <= pictureEdit1.Image.Height; x++)
    {
        for (int i = 0; i <= pictureEdit1.Image.Width; i++)
        {

            if (p1.GetPixel(i, x) != p2.GetPixel(i,x))
            {
                 pictureEdit1.Image.Height.ToString());
                b.SetPixel(x, i, p2.GetPixel(x, i));

            }
            //else
            //{
            //    b.SetPixel(x, i, Color.Red);
            //}


        }
    }

}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
pictureEdit3.Image = b;

Exception Error
Parameter must be positive and < Height.
Parameter name: y
Posted
Updated 15-Feb-11 10:15am
v3
Comments
DaveAuld 15-Feb-11 16:18pm    
When using X,Y coords for points, why use x and i as you integers. use x and y and make things self documenting!
Manfred Rudolf Bihy 17-Feb-11 4:40am    
I updated my answer to fix that your resulting image was rotated.

It seems you swapped up width and height there. You take picture1's height for the x axis and picture1's width for the y axis. It should be the other way. That was the most obvious thing there. There may or may not be other issues in your code - hard to say.

[Edit]
~~~~~~~~

Okay, even on top, you are creating a new Bitmap with the width-height swapped, so perhaps you intended it that way? The code is rather confusing.
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 15-Feb-11 16:24pm    
Actually he just got the naming wrong. Look again and see my answer :)
Nish Nishant 15-Feb-11 16:26pm    
Possible, the code is written in a semi-obfuscated manner. :-)
I think it is your loops that are tripping you up.

Look at the outer loop, you have used <= and .Height +1

Look at the inner loop, you have used <= and .Width

They should both be treated the same.

I would think you would want to use < and .Height or .Width for both the loops.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 15-Feb-11 16:22pm    
Spotted the same as I did! 5+ :)
Man, you've got some strange stuff going on there.

Firstly, by convention use a variable 'x' to go across and 'y' to go up. Not 'i'.

The error looks like it comes about because you're going out of range on the height, looping here:

C#
for (int x = 0; x <= pictureEdit1.Image.Height+1; x++)


surely should be:

for (int y = 0; y < pictureEdit1.Image.Height; y++)


Still not sure what you want to do mind.

Also, the bitmap 'b' - width always comes before height - you're constucting it on its side...
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 15-Feb-11 16:23pm    
Well spotted! 5+
JF2015 17-Feb-11 4:46am    
Very good answer. 5+
It's in the condition of your for loops. See here:

C#
int h, w;
h = pictureEdit1.Image.Height;
w = pictureEdit1.Image.Width;
Bitmap b = new Bitmap(h,w );

Bitmap p1 = (Bitmap )pictureEdit1.Image;
Bitmap p2 = (Bitmap)pictureEdit2.Image;
try
{
    // Naming the loop variable for the vertical axis as X is completely idiotic
    // Besides that counting starts from zero so less than is the correct
    // comparison operator
    for (int x = 0; x < pictureEdit1.Image.Height; x++)
    {
        // Same argumentation as for the vertical axis. My comments also apply to the
        // horizontal axis (which should be named X)
        for (int i = 0; i < pictureEdit1.Image.Width; i++)
        {

            if (p1.GetPixel(i, x) != p2.GetPixel(i,x))
            {
                pictureEdit1.Image.Height.ToString());
                // The following line is your faulty rotating code
                // b.SetPixel(x, i, p2.GetPixel(x, i));
                // The next line fixes the rotating of the created image
                b.SetPixel(i, x, p2.GetPixel(i, x));
            }
            //else
            //{
            // Same error here
            //    b.SetPixel(x, i, Color.Red);
            // Should be :
            //    b.SetPixel(i, x, Color.Red);
            //}
        }
    }

}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
pictureEdit3.Image = b;


Best Regards,
Manfred
 
Share this answer
 
v2
Comments
Nish Nishant 15-Feb-11 16:27pm    
Well-detected, voted 5.
Espen Harlinn 16-Feb-11 15:17pm    
Good work, my 5
JF2015 17-Feb-11 4:43am    
Good answer. 5+
fjdiewornncalwe 17-Feb-11 8:34am    
Great catch... +5
Hi,

If the two images are not the same size then your code will generate an exception

if (p1.GetPixel(i, x) != p2.GetPixel(i,x))

if i is bigger than the width of the second image than it breaks.

you should break out when then width is bigger then the second image width

Valery.
 
Share this answer
 
Comments
Mostafa Elsadany 15-Feb-11 16:40pm    
two image same size
Valery Possoz 15-Feb-11 16:46pm    
Then look at Manfred's answer (#4):

for (int x = 0; x < pictureEdit1.Image.Height; x++)
{
for (int i = 0; i < pictureEdit1.Image.Width; i++)
{

That will solve your issue.
Mostafa Elsadany 15-Feb-11 17:14pm    
thanks mann thanks for every

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900