user1560834
user1560834

Reputation: 151

I want to be able to draw lines in codebehind for webpage

I'm looking at being able to draw lines in code behind in C#, for a webpage created in Visual Studio. They will be just vertical lines but must be able to change size, depending on other variables.

I've got some code, as below:

using System.Drawing;
using System.Drawing.Imaging;

public partial class SpareFields : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Bitmap objBitmap;
        Graphics objGraphics;
        objBitmap = new Bitmap(400, 440);
        objGraphics = Graphics.FromImage(objBitmap);
        objGraphics.Clear(Color.White);
        Pen p = new Pen(Color.Yellow, 0);

        Rectangle rect = new Rectangle(600, 1500, 601, 2000);
        objGraphics.DrawEllipse(p, rect);
        objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
        //objBitmap.Save(Server.MapPath("x.jpg"), ImageFormat.Jpeg);

        objBitmap.Dispose();
        objGraphics.Dispose();
    }
}

And so on. This is just at the start of the Page_Load - there's a lot of other non-graphics code in there. Trouble is, the rest of the page is being ignored and it's just drawing the graphics.

Also, I know the above is for an Ellipse (basically a quick test on some pie chart drawing). It'd be easy to adapt for a line, or a really thin rectangle.

Any idea how to draw, whilst still having the rest of the original page available?

Upvotes: 1

Views: 1779

Answers (2)

Mesh
Mesh

Reputation: 6462

To just draw some lines I'd use SVG, there are very few reasons not to use it for dynamic images, the big huge advantage for svg is you can change the image on the page/client via JS.

But if you really want bitmaps, there are many articles around the web discussing Dynamic Image, a Google for 'msdn dynamic image asp.net' should give you many, relevant articles with source.

Upvotes: 1

Hans Kesting
Hans Kesting

Reputation: 39329

The easiest way is to split this page in two:

1) your page outputs just it's html, including an <img> tag with a src attribute pointing to a new page 2) in that new page, use just that image generation code.

Upvotes: 1

Related Questions