Click here to Skip to main content
15,917,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i m trying to develope a application like MS Paint i am drawing every drwaing on a panel and now i m trying to implement pick color function i m writing the following code but it not working properly
bitmap bmp=new bitmap(panel.width,panel.height);
color c=bmp.Getpixel(x,y);//pixels whose color i want to get
lable.baclcolor=c.ToArgb();
it is not working properly
Posted

Of course that would not work dude!

the problem is : Your bitmap is empty!!!

solution : fill it.

So, change the code this way :

Bitmap MyBitmap = new Bitmap(Panel.Width, Panel.Height);
Panel.DrawToBitmap(MyBitmap, new Rectangle(0, 0, Panel.Width, Panel.Height));
Label.Text = (MyBitmap.GetPixel(X, Y)).ToString(); //Your X and Y.

have fun.


---------------------------
Regards

H.Maadani
 
Share this answer
 
v2
Please stop posting fake answers, and edit your post to include your new detail !!!! The panel will just draw itself. If you're drawing a bitmap onto the panel in your own code, you HAVE the bitmap to call GetPixel on, why are you making this so difficult, in that case ?
 
Share this answer
 
Why
wrote:
lable.baclcolor=c.ToArgb();

?
 
Share this answer
 
i did not get you what do u want to ask ?
 
Share this answer
 
OK, got it now.

I donno what you wanna do exactly with that two last lines in the code (regions), but my best guess is you are trying to keep track drawn rectangle cause they disappear when a new one is created, is it true?

well, if it is, thats a bad idea. lets get to buissiness, less talking.

I've uploaded the code for you :

http://rapidshare.com/files/332318003/PaintLikeApp.rar[^]

in this sample, you can load a pic ( or dont ) and draw some blue rectangle on it ( by draging left mouse button ), and see the color of selected pixel by right mouse button.

enjoy.

--------------------

Regards

H.Maadani
 
Share this answer
 
v2
thanks a lot for the solution i will try it and get back to you if any problem will be there because i have seperate functions for each drawing so i have to draw each drawing in bitmap now as previously i did it in panel directly
 
Share this answer
 
Hi Mr.Maadani,
Thanks a lot for the concern, I tried the solution you told me but it is not working properly as it is returning "white" no matter what color i am trying it on i think it is returning background color of panel but i want the color of the drawing drawn on that pannel suppose i draw a rectangle on that pannel filled with blue color if i click anywhere on that rectangle it should return blue color even though the color of panel is white just like Color Picker of MS Paint
this is my code to draw a rectangle---

//x,y->starting point of rectangle.
//nex,ney-->ending point of rectangle.

void Rectangle(float x, float y, float nex, float ney)
{
Graphics g2 = panel1.CreateGraphics();
Pen mP = new Pen(label18.BackColor, 2);
float Width;
float Height;
float startx, starty, endx, endy;
if (x < nex)
{
startx = x;
endx = nex;

}
else
{
startx = nex;
endx = x;

}
if (y < ney)
{
starty = y;
endy = ney;

}
else
{
starty = ney;
endy = y;

}
Width = endx - startx;
Height = endy - starty;
Rectangle r = new Rectangle((int)startx, (int)starty, (int)Width, (int)Height);
g2.DrawRectangle(mP, r);
Region re = new Region(r);
regionlist.Add(re);
}
please check it out
 
Share this answer
 
v2
Mr. Christian,
i m new to all these thingh that is why so many problems are coming and if u think that i m posting fake question then let me tell u i have many other things to do instead of posting fake questions and waste my time as well as others time so if you think that my posts are fake then do nto reply please
 
Share this answer
 
hi Mr.Maadani,
those last two lines were to add each drawing in a list so that i can reffere them later if i want to fill any drawing,anyways thanks a lot for your support but last night i got what was the problem and solved it and now it is working fine as you told that my bitmap is empty so i corected and it is working fine now thanks a lot.
 
Share this answer
 

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