Click here to Skip to main content
15,846,038 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello . I have a rectangle like this one in my my pictureBox
| |
| |
| |
| |

I used this code to generate this rectangle:
Graphics g = pictureBox1.CreateGraphics();
Pen selPen = new Pen(Color.Blue);
g.DrawRectangle(selPen, 50, 50, 10, 50);

My goal is to color just 50% of this rectangle and make it look like this:

| |
| |

So 50% will be colored in the default color and the other part will be colored in an other color.

I really don`t know how can I do this and I need your help.
Thank you for your replay in advance.
Updated 3-Sep-22 0:23am

Abusing PictureBox is a usual mistake. Of course you can draw on an instance of a System.Windows.Forms.PictureBox, but there are no situations when it can be useful. It can be useful to draw in an instance of System.Drawing.Image or an instance of any control class, usually derived from System.Windows.Forms.Control.

As to the PictureBox, it is designed to make it very simple to present pictures, one whole static picture at a time. An attempt to do anything dynamic, interactive or animated is just waste of extra resources and development time. It does not present any additional feature, only the hassles. Even your simple example would be an abuse. You can do it, but you never need it.

Not to worry, I'll explain what to do instead.

To draw on the instance of Image, obtain an instance of System.Drawing.Graphics using the static factory method System.Drawing.Graphics.FromImage(Image) and draw using Graphics, please see[^].

In contrast, you should never create an instance of Graphics if you need to render graphics on screen, using any Control. Instead, you should obtain one from the event arguments parameter passed to the overridden method OnPaint or your event handler of the event Paint, please see:[^].

Please find further detail on rendering on-screen graphics in my past solutions:
How do I clear a panel from old drawing[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

Share this answer
Radu_Socaciu 23-Jan-12 13:11pm    
Thank you for this , very helpfull . I need to study thoose examples , but on a first look that is perfect for my problem .
Sergey Alexandrovich Kryukov 23-Jan-12 13:39pm    
You are welcome.
This is certainly a solution, and this matter is easy enough when you know how it works.

Good luck, call again.
Hi, im trying to draw a parallelogram, i have class of rectangle and i want that the parallelogram will use the function from rectangle to the parallelogram.
than you .
Share this answer
Kats2512 3-Sep-22 9:06am    
No one on this platform will be doing your homework for you!

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