Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Can Help me with graphics in c#. It isn't for me, it is for my friend. In fact it is homework

ex example
when I click to button draw rectangle then when I click another button draw ellipse etc


h7h7 !
Updated 8-May-12 13:31pm
Sergey Alexandrovich Kryukov 8-May-12 16:43pm    
What are you interested in? WPF? Forms? Anything else? You need to tag it, not just "C#" as you did.
Sergey Alexandrovich Kryukov 8-May-12 16:44pm    
Look, I recently advised you to do more complete specifications when asking questions. Why would you ignore it this time again?
Sandeep Mewara 9-May-12 1:43am    
For whomsoever it is, you guys should try first and post specific issues.


Of couse I help you to do your homework but as i said "I Help You" not doing your homework.

If you mean in WinForm:

1. Create 2 Methods in code behind
private void DrawRectangle()
   // some code 1
private void DrawEllipse()
   //some code 2

Don't you know what method is? Methods (C# Programming Guide)

2. In Click event of Buttons:
private void button1_Click(object sender, System.EventArgs e)
private void button2_Click(object sender, System.EventArgs e)

Don't you know about Click event? Control.Click Event

3. And what code should you substitue for //some code1 and //some code2 ?

Look at here: Drawing with Graphics in WinForms using C#

Sorry about forgetting something. (Thanks to SA) You also need a Control to keep the drawn shapes. For example a PictureBox.
Of course after reading the last link you could find it out.
Share this answer
Sergey Alexandrovich Kryukov 8-May-12 17:16pm    
This is only about Forms. I would prefer to wait until OP specify what UI library to use -- please see my comment to the question.
Where are you going to get the instance of Graphics? Create it? It would work, but not good. The images will disappear in each invalidation (shadows by other window and shown again, for example), because you are not placing rendering in response to WM_PAINT, which is done via the event Paint or overridden OnPaint.
Also, there is such thing as fighting flicker.
So, this is not really a solution. Did you try it by yourself? Try it to see that the graphics get disappeared -- there was a number of questions related to this problem.
(I did not vote, but it does not solve the problem.)
Shahin Khorshidnia 8-May-12 17:31pm    
Hello SA. I've read your comment (Of course I saw it after sending this answer). yes OP must specify the UI library, I told that if he/she meant WinForm. And also "yes" I've forgotten something. But I hope OP can find it out after reading the last link. ( Thanks for not voting:D ) I'm going to improve the solution.
Sergey Alexandrovich Kryukov 8-May-12 23:41pm    
Again, this is not a correct answer. Actually, PictureBox is evil for such things. It does not help anything, only add hassles...
Sergey Alexandrovich Kryukov 8-May-12 23:56pm    
Anyway, I lost my patience, added my answer, more for you then for OP. Please see -- good to understand... Well, I don't think you should have started to answer...
All right, you do not confess what do you want. :-) I'm adding this answer only to fix the answer by Shahin.

Please see my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

This should explain you why not PictureBox:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

Share this answer
Shahin Khorshidnia 9-May-12 11:08am    
Thank you SA
Sergey Alexandrovich Kryukov 9-May-12 11:09am    
You are welcome.

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