Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a PictureBox inside a Form, I want to set unit of PictureBox is Pixel, but I dont see any property in Properties window of PictureBox, please help me do this, because of drawing graphic on PiictureBox. Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Aug-12 21:54pm    
Why, ever?
--SA

1 solution

You should not draw any graphics using PictireBox control. For this purpose, it's much better to use a custom control, derived from System.Windows.Forms.Control. It so happened that the name "picture box" mislead many beginners who try to abuse this control. It is possible to draw on this control and even implement some interactive or animated functionality… it makes no sense at all, because this control does not help drawing at all; just the opposite, it adds some hassled and eats additional resources and development time, reduce performance. It was designed for a simple purpose: display a static image, for people who would not want to know anything about graphics rendering.

As you really need to draw something, you just trying to use totally wrong control not designed the this purpose.

I'll explain you how to draw using GDI. Please see my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

See also these answers on rendering and related detail:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

—SA
 
Share this answer
 
Comments
Kenneth Haugland 19-Aug-12 21:38pm    
Good answer +5 (PS Whats a PictireBox?)
Sergey Alexandrovich Kryukov 19-Aug-12 22:26pm    
Thank you, Kenneth. Everyone can read the help page on the control, right?
--SA
Kenneth Haugland 19-Aug-12 22:50pm    
I though so too, but some of the Q's are sometimes... Well lets not go there ;)
Andrewpeter 20-Aug-12 0:29am    
Thanks SA, in your opinion what control I should use to draw graphics on? Panel control?
Sergey Alexandrovich Kryukov 20-Aug-12 0:57am    
Panel will do, but the best is just Control. Panel has no functionality except drawing background, border and ability to have child controls and, say, scrolling of them. If you need to have children on your control, use Panel, if not -- Control is the best. As you draw something on it in, anyway, you can always draw background, if you need it, and some border, if you need it. Also, don't forget that if you graphic is somewhat (or very) dynamic, you will need to avoid flicker, which is done via double buffering (there is both such property and the style via SetStyle).

Also, if you sub-class control, you can override OnPaint instead of handling the even Paint, and the code with overriding is a tiny bit simpler and perhaps even more effective (don't call base.OnPaint, by the way), so it's a little but some gain.

If that's all, please accept my answer formally (green button). If not, your follow-up questions are welcome.
--SA

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