mousedown event
-
hi all i did this a while ago but lost everything on my comp and now cant remember how I did it. I remember it was fairly simple but I cant for the life of me remember what or how I did it. Basically I want to get the colour of the pixel under my mouse pointer. I've loaded the image into the picturebox (picLayout) and have the mouseevent being handled I just can't remember how to get the colour of the pixel. any pointers well appreciated TIA tim
-
hi all i did this a while ago but lost everything on my comp and now cant remember how I did it. I remember it was fairly simple but I cant for the life of me remember what or how I did it. Basically I want to get the colour of the pixel under my mouse pointer. I've loaded the image into the picturebox (picLayout) and have the mouseevent being handled I just can't remember how to get the colour of the pixel. any pointers well appreciated TIA tim
-
Hello, I think it is a good way to use the Bitmap.GetPixel function.
using(Bitmap bm = new Bitmap(pictureBox2.BackgroundImage))
{
actColor = bm.GetPixel(e.X,e.Y);
}Hope it helps!
All the best, Martin
hi martin thanks for that but I'm getting an error (im probably being a bit thick here but.....) i have this when the image loads: this.picLayout.MouseDown += new MouseEventHandler(this.HandleMouseDown); and then:
public void HandleMouseDown(object sender, MouseEventArgs e) { using(Bitmap bm = new Bitmap(picLayout.BackgroundImage)) { label2.Text = bm.GetPixel(e.X,e.Y).ToString(); } }
and it just crashes it, any thoughts (apologies ofr my dimwittedness - brain just isn't functioning atm) also i was thinking about having this as a mouseover event, if its creating bitmaps on every mousemove event wont it just explode ? I wish I could remember what I did before, it was fairly simple and something along the line imgName.R.ToString(); ta -
hi martin thanks for that but I'm getting an error (im probably being a bit thick here but.....) i have this when the image loads: this.picLayout.MouseDown += new MouseEventHandler(this.HandleMouseDown); and then:
public void HandleMouseDown(object sender, MouseEventArgs e) { using(Bitmap bm = new Bitmap(picLayout.BackgroundImage)) { label2.Text = bm.GetPixel(e.X,e.Y).ToString(); } }
and it just crashes it, any thoughts (apologies ofr my dimwittedness - brain just isn't functioning atm) also i was thinking about having this as a mouseover event, if its creating bitmaps on every mousemove event wont it just explode ? I wish I could remember what I did before, it was fairly simple and something along the line imgName.R.ToString(); ta -
going_mental wrote:
but I'm getting an error
Standard Question No.?: What does the error say?
going_mental wrote:
and it just crashes it
runtime or designtime?
All the best, Martin
hmmm, told you my brain wasn't working.... i should have used picLayout.Image rather than picLayout.BackgroundImage. it was throwing an unhandled exception (which confused me as tried to catch it but it was happening before it got to my try/catch) over the lack of background image in the picturebox. thanks, all working now, but it would be nice to be able to do this as a mouseover event rather than just mousedown. I just tried it as mouseover event and the cpu load kinda went a bit mad. Any thoughts ? when i did it before it was fairly simple and something along the line imgName.R.ToString(); tim
-
hmmm, told you my brain wasn't working.... i should have used picLayout.Image rather than picLayout.BackgroundImage. it was throwing an unhandled exception (which confused me as tried to catch it but it was happening before it got to my try/catch) over the lack of background image in the picturebox. thanks, all working now, but it would be nice to be able to do this as a mouseover event rather than just mousedown. I just tried it as mouseover event and the cpu load kinda went a bit mad. Any thoughts ? when i did it before it was fairly simple and something along the line imgName.R.ToString(); tim
-
hi martin thanks for that but I'm getting an error (im probably being a bit thick here but.....) i have this when the image loads: this.picLayout.MouseDown += new MouseEventHandler(this.HandleMouseDown); and then:
public void HandleMouseDown(object sender, MouseEventArgs e) { using(Bitmap bm = new Bitmap(picLayout.BackgroundImage)) { label2.Text = bm.GetPixel(e.X,e.Y).ToString(); } }
and it just crashes it, any thoughts (apologies ofr my dimwittedness - brain just isn't functioning atm) also i was thinking about having this as a mouseover event, if its creating bitmaps on every mousemove event wont it just explode ? I wish I could remember what I did before, it was fairly simple and something along the line imgName.R.ToString(); taHi, why would you need a new Bitmap in your MouseDown handler ? This is what I would try:
public void HandleMouseDown(object sender, MouseEventArgs e) {
Bitmap bm = picLayout.Image as Bitmap;
string s= bm==null ? "Not a bitmap" : bm.GetPixel(e.X,e.Y).ToString();
label2.Text = s;
}And since this is a cheap operation now, you could try it in MouseHover handler also. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
hi martin just to pick your brains a little further.....it would be nice to be able to do this as a mouseover event rather than just mousedown. I just tried it as mouseover event and the cpu load kinda went a bit mad. when i did it before it was fairly simple and something along the lines of imgName.R.ToString(); or something like that. is that something you've heard of ? I can access the RGB values for the background but obviously thats not much use (as its just grey) but it wont let do the same for the Image. Any thoughts ?
-
Hi, why would you need a new Bitmap in your MouseDown handler ? This is what I would try:
public void HandleMouseDown(object sender, MouseEventArgs e) {
Bitmap bm = picLayout.Image as Bitmap;
string s= bm==null ? "Not a bitmap" : bm.GetPixel(e.X,e.Y).ToString();
label2.Text = s;
}And since this is a cheap operation now, you could try it in MouseHover handler also. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi, why would you need a new Bitmap in your MouseDown handler ? This is what I would try:
public void HandleMouseDown(object sender, MouseEventArgs e) {
Bitmap bm = picLayout.Image as Bitmap;
string s= bm==null ? "Not a bitmap" : bm.GetPixel(e.X,e.Y).ToString();
label2.Text = s;
}And since this is a cheap operation now, you could try it in MouseHover handler also. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
excellent, that does the trick nicely. thanks to you both