0

This is a .net web site. I have a list of 100 x,y coordinates.

I need to display 100 octagonal shapes the centre point of which sits at the respective co-ordinate.

I toyed with doing this by passing an array to javascript and dynamically creating and positioning the images. But it looks awful and resizing the browser window causes problems etc.

So, can I create a graphic myself? I saw this code answering a similar question:

    using (Bitmap bmp = new Bitmap(10, 10))
    { 
        using (Graphics g = Graphics.FromImage(bmp)) 
        { 
            g.Clear(Color.White); 
            g.DrawLine(Pens.Black, new PointF(9.56f, 4.1f), 
                                           new PointF(3.456789f, 2.12345f)); 
        } 
        bmp.Save(@"c:\myimage.jpg", ImageFormat.Jpeg);
    }

Can you draw octagons easily enough using this?

However, when I try to run the code above, I am getting this error.

A generic error occurred in GDI+

I don't want to spend a long time working out out to draw octagons etc. if I can't get it to run.

One last thing, I don't need to save the graphic, I just want to display it as though it were an image I'm getting from the images folder.

Edit: Further to suggestion below I have modified the code like this:

using (MemoryStream ms = new MemoryStream())
            {
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] buffer = ms.GetBuffer();
                HttpContext.Current.Response.OutputStream.Write(buffer, 0, buffer.Length); 
            }

This is putting my drawing on the screen. Is there anything that should be tweaked?

3
  • Martin, It may be good idea to add correct tags (i.e. SLacks's answer assumes ASP.Net tag, which is probably what you want to add). Also consider asking about one question per question... Commented Oct 5, 2012 at 18:02
  • I don't know if this can be of any help but you can use HTML5 canvas to do some of this stuff with very lightweight coding... Commented Oct 5, 2012 at 18:14
  • What SLaks has said. But also you should/might be able to get write access to the App_Data folder... Commented Oct 5, 2012 at 23:10

1 Answer 1

2

You probably don't have write access to C:\.

Instead of saving to disk, you should save to a MemoryStream, then render it to the HTTP response. (inside of an ASHX or MVC action)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. I have done as you suggested - code is in original post. I have drawn a polygon ... can you colour a polygon? I can't see any method for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.