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