Im trying to make something that when im drawing something in picturebox2 at the same time it will d

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
using System;<br/>
using System.Collections.Generic;<br/>
using System.ComponentModel;<br/>
using System.Data;<br/>
using System.Drawing;<br/>
using System.Linq;<br/>
using System.Text;<br/>
using System.Windows.Forms;
namespace WindowsFormsApplication1<br/>
{<br/>
public partial class ImagesPixelsColorsComparison : Form<br/>
{<br/>
int x;<br/>
int y;<br/>
public static Rectangle Rect { get; set; }<br/>
Bitmap FirstImage = null;<br/>
Bitmap SecondImage = null;<br/>
bool StartPaint;<br/>
bool _dontDrawRect = false;<br/>
public ImagesPixelsColorsComparison()<br/>
{<br/>
InitializeComponent();<br/>
textBox6.Text = this.Size.ToString();<br/>
StartPaint = false;<br/>
FirstImage = Properties.Resources.RadarImageClose;<br/>
SecondImage = Properties.Resources.RadarImageActive;<br/>
pictureBox1.Size = FirstImage.Size;<br/>
pictureBox2.Size = SecondImage.Size;<br/>
this.pictureBox1.Image = this.FirstImage;<br/>
this.pictureBox2.Image = this.SecondImage;<br/>
<br/>
<br/>
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)<br/>
{<br/>
if (e.Button == MouseButtons.Left && this._dontDrawRect == false)<br/>
{<br/>
StartPaint = true;<br/>
Rect=new Rectangle(e.X, e.Y, 0, 0);<br/>
textBox4.Text = "Top " + Rect.Top + " Left " + Rect.Left;<br/>
pictureBox1.Invalidate();<br/>
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)<br/>
{<br/>
if (e.Button == MouseButtons.Left && this._dontDrawRect == false)<br/>
{<br/>
Rect = new Rectangle(Rect.X, Rect.Y, e.X - Rect.X, e.Y - Rect.Y);<br/>
pictureBox1.Invalidate();<br/>
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)<br/>
{<br/>
if (e.Button == System.Windows.Forms.MouseButtons.Left)<br/>
{<br/>
textBox5.Text = "Bottom " + Rect.Bottom + " Right " + Rect.Right;<br/>
//check, if picture-variables are set<br/>
if (FirstImage != null && SecondImage != null)<br/>
{<br/>
//check, if selected the correct rectangle for caomparing,<br/>
//if so, set further rectangle-selecting to false<br/>
if (this._dontDrawRect == false)<br/>
{<br/>
if (MessageBox.Show("Use this Rectangle for comparing?", "Question",<br/>
MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==<br/>
System.Windows.Forms.DialogResult.Yes)<br/>
{<br/>
this._dontDrawRect = true;<br/>
}<br/>
}
//if correct rectangle selected, do the job<br/>
if (_dontDrawRect)<br/>
{<br/>
if (CompareImageRegions())<br/>
MessageBox.Show("identical");<br/>
else<br/>
MessageBox.Show("different");<br/>
}<br/>
}<br/>
}<br/>
else if (e.Button == System.Windows.Forms.MouseButtons.Right)<br/>
this._dontDrawRect = false;
}
private bool CompareImageRegions()<br/>
{<br/>
textBox3.Text = Rect.ToString();<br/>
bool different = false;
//if pictures are not of same size, return that they are different<br/>
if (FirstImage.Width != SecondImage.Width || FirstImage.Height != SecondImage.Height)<br/>
return false;
//Iterate over the Rect<br/>
for ( x = Rect.X; x < Rect.X + Rect.Width; ++x)<br/>
{<br/>
for ( y = Rect.Y; y < Rect.Y + Rect.Height; ++y)<br/>
{<br/>
//check, if x and y are inside the picture<br/>
if (x >= 0 && x < FirstImage.Width && y >= 0 && y < FirstImage.Height &&<br/>
x < SecondImage.Width && y < SecondImage.Height)<br/>
{<br/>
Color c = FirstImage.GetPixel(x, y);<br/>
textBox1.Text = c.ToString();<br/>
Color c2 = SecondImage.GetPixel(x, y);<br/>
textBox2.Text = c2.ToString();<br/>
if (CheckColorValues(c,c2) == false)<br/>
{<br/>
different = true;<br/>
break;<br/>
}<br/>
}<br/>
else<br/>
{<br/>
different = true;<br/>
break;<br/>
}<br/>
}
if (different)<br/>
break;<br/>
}<br/>
return !different;
}


private static bool CheckColorValues(Color c, Color c2)<br/>
{<br/>
if (c.A.Equals(c2.A)<br/>
&& c.R.Equals(c2.R)<br/>
&& c.G.Equals(c2.G)<br/>
&& c.B.Equals(c2.B))<br/>
{<br/>
return true;<br/>
}<br/>
else<br/>
{<br/>
return false;<br/>
}<br/>
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)<br/>
{<br/>
using (Pen pen = new Pen(Color.Red, 2))<br/>
{<br/>
if (StartPaint == true)<br/>
{<br/>
e.Graphics.DrawRectangle(pen,Rect);<br/>
pictureBox2.Paint += new PaintEventHandler(pictureBox2_Paint);<br/>
// 26,240,347,35<br/>
}<br/>
}
}
private void ImagesPixelsColorsComparison_FormClosing(object sender, FormClosingEventArgs e)<br/>
{<br/>
//dispose<br/>
if (FirstImage != null)<br/>
FirstImage.Dispose();<br/>
if (SecondImage != null)<br/>
SecondImage.Dispose();<br/>
}
private void ImagesPixelsColorsComparison_Resize(object sender, EventArgs e)<br/>
{<br/>
textBox6.Text = this.Size.ToString();<br/>
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)<br/>
{<br/>
using (Pen pen = new Pen(Color.Red, 2))<br/>
{<br/>
e.Graphics.DrawRectangle(pen, Rect);<br/>
}<br/>
}<br/>
}<br/>
}

So i thought inside the pictruebox1 paint event ot register the picturebox2 paint event: pictureBox2.Paint += new PaintEventHandler(pictureBox2_Paint);<br/>
So when its painting in picturebox1 it will active the paint event on picturebox2.
And then in the end of the code i created the picturebox2 paint vent:

private void pictureBox2_Paint(object sender, PaintEventArgs e)<br/>
{<br/>
using (Pen pen = new Pen(Color.Red, 2))<br/>
{<br/>
e.Graphics.DrawRectangle(pen, Rect);<br/>
}<br/>
}
But i used a breakpoint on where i register the picturebox2 paint event and it stoped there.
But when i put breakpoint on the picturebox2 paint event i created its never get there never stop.
What did i do wrong?

Thanks. <hr class="sig danieli

View the full article
 
Back
Top