Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there,

Why doesn't a Line get drawn on the screen with below code???

C#
//Declaration
public Graphics obj_Graphics;
private Bitmap obj_Bitmap;

//Coding
        obj_Bitmap = new Bitmap(1000, 1000);
        obj_Graphics = Graphics.FromImage(obj_Bitmap);
        obj_Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 5), 100, 500, 400, 500);


Thanks in advance...
Posted
Updated 1-May-11 21:00pm
v2
Comments
Sergey Alexandrovich Kryukov 2-May-11 3:26am    
ASP.NET?!
--SA

You are using some objects you declared yourself. These are in memory but not on screen. Have a look at the link for a simple example how to do it when working with forms:
http://www.geekpedia.com/tutorial50_Drawing-with-Csharp.html[^]

If you want to use the created content on your web page, have a look at this link:
http://msdn.microsoft.com/en-us/library/system.web.httpresponse.outputstream.aspx[^]

Good luck!
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 2-May-11 3:25am    
ASP.NET?!
--SA
E.F. Nijboer 2-May-11 4:31am    
The link to the example isn't quite correct on that but the reason the line isn't visible still is the same I guess. Drawing to a Graphics object won't automatically display on screen.
Sergey Alexandrovich Kryukov 2-May-11 4:38am    
Yes of course. It's about drawing in Paint or OnPaint and Invalidate. But it has nothing to do with ASP.NET, which makes the whole question incorrect. It's either ASP.NET or System.Drawing, not both. See our discussion with Griff.
--SA
E.F. Nijboer 2-May-11 6:04am    
The question isn't very clear as is already mentioned. It is of course possible to create images on the fly for use in a page by saving it to the response.outputStream.
Sergey Alexandrovich Kryukov 2-May-11 6:32am    
Or gosh, you're absolutely right! I somehow missed the fact the OP draws on Bitmap, probably because as I was mislead by the words "drawn on screen". And this is pretty usual technique, this is how complex mathematical expressions are rendered on the fly in the Web site.

Certainly, my 5. (I would mention it in the body of your answer.)
And thanks for finally explaining this to me.
--SA
Because you are not drawing on the screen, you are drawing on a bitmap:
obj_Graphics = Graphics.FromImage(obj_Bitmap);
If you want to draw it i=on teh screen, then handle the Paint event for your form, or a panel on your form and use the Graphics object you are handled in the EventArgs parameter:
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 5), 100, 500, 400, 500);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-May-11 3:26am    
ASP.NET?!
--SA
OriginalGriff 2-May-11 4:09am    
Hadn't noticed that - I assumed the OP was forms based from the "get drawn on the screen" part.
ASP.NET could be just buzzword frenzy - either that or we need more code.
Sergey Alexandrovich Kryukov 2-May-11 4:34am    
It it is just a buzzword, the correct answer should be "stop messing up unrelated things, first tell me what do you want". This is not just the formality (who knows?), there are real examples of messing up things. For example, recently one patient asked how to use Windows.Forms.Form in the ASP.NET Page (!!!). I had to explain that there is a client part and the server, and "Who will look at this form?", etc...
--SA
OriginalGriff 2-May-11 4:43am    
And I had one the other way this morning: http://www.codeproject.com/Answers/189811/how-to-use-javascript-in-Csharp-NET-window-applica.aspx
You do have to wonder what they teach them sometimes.
Sergey Alexandrovich Kryukov 2-May-11 5:20am    
Yes, funny (I voted 5 for that answer). Fantasy is unlimited...
--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