Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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);
            g.Dispose();
 
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.
Posted 23-Jan-12 6:23am
Edited 23-Jan-12 6:26am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx[^].
 
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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].
 
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[^].
 
—SA
  Permalink  
Comments
Radu_Socaciu at 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 .
SAKryukov at 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.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 375
1 Sergey Alexandrovich Kryukov 370
2 BillWoodruff 314
3 George Jonsson 274
4 OriginalGriff 257
0 OriginalGriff 5,030
1 CPallini 4,115
2 Sergey Alexandrovich Kryukov 3,554
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 23 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100