简体   繁体   English

使用C#动态创建图形图像

[英]Create a graphic image dynamically using C#

This is a .net web site. 这是一个.net网站。 I have a list of 100 x,y coordinates. 我有一个100 x,y坐标列表。

I need to display 100 octagonal shapes the centre point of which sits at the respective co-ordinate. 我需要显示100个八角形,其中心点位于相应的坐标上。

I toyed with doing this by passing an array to javascript and dynamically creating and positioning the images. 我通过将数组传递给javascript并动态创建和定位图像来玩弄这个。 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+ 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. 最后一件事,我不需要保存图形,我只是想显示它,好像它是我从images文件夹中获取的图像。

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? 有什么应该调整的吗?

You probably don't have write access to C:\\ . 您可能没有C:\\写入权限。

Instead of saving to disk, you should save to a MemoryStream , then render it to the HTTP response. 您应该保存到MemoryStream ,然后将其呈现给HTTP响应,而不是保存到磁盘。 (inside of an ASHX or MVC action) (在ASHX或MVC行动中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM