Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
In System.Drawing what do I need to put in Default.aspx to make the following work?

Default.aspx.cs
C#
using System.Drawing;
using System.Drawing.Drawing2D;
namespace GraphOrbits
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

                    Bitmap xPanel = new Bitmap(100, 200);
                    Graphics objGraphicPanel = Graphics.FromImage(xPanel);
                    Pen colorPen = new Pen(Color.Green,2);
                    objGraphicPanel.DrawLine(colorPen, 110, 190, 120, 190);
         }
    }
}


What I have tried:

I have tried "asp:chart" and some other javascript based graphics programs. I thought "System.Drawing" might be more straightforward.
Posted
Updated 2-Jun-16 18:43pm
v2
Comments
F-ES Sitecore 3-Jun-16 4:42am    
If you're wanting to draw on the client browser then you can't do that from your code-behind as it runs on the server. Look into using the browser's "canvas" instead, google for tutorials.

1 solution

The code you've shown simply does nothing, only wastes CPU time and creates unmanaged resource leak for the IDisposable objects, which you fail to Dispose. Essentially, you create the object Bitmap xPanel, draw on the bitmap, and return from the method. The reference to this object goes nowhere and get lost. As all unreachable objects, it will eventually be destroyed by the Garbage Collection mechanism, which won't safe the code from the memory leak. That's all.

The question makes no sense. You cannot ask "how to make the following work" if the "following" does nothing (or does nothing useful). Instead, you need to explain what should work, what would you like to achieve. I also would strongly advice, before judging if something is straightforward enough or not, try to understand how this "something" works.

What to do? Well, you can keep using System.Drawing, but you need to do something with the bitmaps obtained. (And don't forget IDisposable.Dispose(), which should not be called directly, but via the using statement; don't confuse it with using directive, read about this feature.) You can store the bitmap in a file and then use the file name in HTML anchor. Better yet, you can have a separate ASP.NET page with just the bitmap sent in HTTP response, with appropriate content type of it, such as "image/png" (see the standard on content types: ).

But all this activity would be quite questionable in terms of performance and, I would say, low-tech. Pixel graphics… Eh… It would be much better to produce charts in modern vector graphics. In Web applications, there are at least two modern high-quality and high-performance possibilities: HTML5 Canvas element or embedded SVG (especially SVG). Canvas will require the use of JavaScript, but SVG, it it is static and generated on the server side, can work without JavaScript. There are two benefits: graphic is vector, no pixellation, it's scalable; and performance is high an there is no waste of traffic. If you want to get impressed, see, for example, D3:
D3.js — Data-Driven Documents[^],
Gallery · d3/d3 Wiki · GitHub[^].

Perhaps you want to have something more specialized and more ready to use. Then you may want to use something like Microsoft Charts for ASP.NET: Chart Controls for Integrating Data[^].

—SA
 
Share this answer
 
v2
Comments
Member 11242200 3-Jun-16 0:45am    
I do not know how to put it into Default.aspx so I can correct the "Do Nothing" c# program!!
Sergey Alexandrovich Kryukov 3-Jun-16 1:02am    
Perhaps you've read my answer before it was complete. Please see again.
—SA
Member 11242200 3-Jun-16 1:17am    
I have used HTML5 canvas with javascript and asp:chart in the past but I want to learn "System.Drawing" that interfaces with C# and VB. How do I put the drawing into HTML?
Sergey Alexandrovich Kryukov 3-Jun-16 2:31am    
If you just want to learn System.Drawing, it would be the best to use System.Windows.Forms UI.
How to put the drawing in HTML? As always:
<img src="myFile"/>
—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