如何使用system.drawing绘制一条线 [英] How do I draw a line using system.drawing

查看:116
本文介绍了如何使用system.drawing绘制一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用使用System.Drawing编写了这个程序:



我不知道它是否有效,因为没有黄色只是一个空白的网页。



我需要在Default.aspx中放一些东西才能使用吗?



I have written this program with "using System.Drawing;":

I do not know if it worked because there is no yellow just a blank webpage.

Is there something I need to put in Default.aspx so this will work?

Bitmap xPanel = new Bitmap(200, 200);
Graphics objGraphicPanel = Graphics.FromImage(xPanel);
//Background White
SolidBrush yellowBrush = new SolidBrush(Color.Yellow);
objGraphicPanel.FillRectangle(yellowBrush, 0, 0, 200, 200);

//Text
SolidBrush blackBrush = new SolidBrush(Color.Black);
Font horzFont = new Font("Verdana", 10, FontStyle.Regular);
objGraphicPanel.DrawString("Test", horzFont, blackBrush, 15, 80);

//Line
Pen colorPen = new Pen(Color.Green, 2);
objGraphicPanel.DrawLine(colorPen, 10, 100, 120, 190);

xPanel.Save("c:\path\graphicline.png");
objGraphicPanel.Dispose();
xPanel.Dispose();





我的尝试:



xPanel.Save(Server.MapPath(graphicline.png),System.Drawing.Imaging.ImageFormat.Png);



What I have tried:

xPanel.Save(Server.MapPath("graphicline.png"), System.Drawing.Imaging.ImageFormat.Png);

推荐答案

唯一更改我需要对你的代码添加一个atsign:

The only change I needed to make to your code was to add an atsign:
xPanel.Save(@"c:\path\graphicline.png");

没有它,您的代码将无法编译。将其更改为PC上的有效文件夹,然后我可以查看它生成的文件: Dropbox - graphicline.png [ ^ ]如你所见,它是黄色的,有文字和绿色对角线。

但是...不会显示在网页上。除非你明确告诉页面显示它,否则你不会!

而不是将图像保存到文件并处理位图 - 这在网站上不是一个好主意,因为多个用户可能想要不同的图片 - 您需要直接显示位图。

有很多方法可以做到这一点,但最简单的方法是在页面中添加一个Image控件并将其赋予构成图像的字节:

Without it your code would not compile. Changing that to a valid folder on my PC, I can then look at the file it generated: Dropbox - graphicline.png[^] As you can see, it's yellow, has the word "Text" and a green diagonal line.
But...that won't display on a webpage. Not unless you specifically tell the page to display it, which you don't!
Instead of saving the image to a file and disposing of the bitmap - which isn't a good idea in a website, as multiple users may want different pictures - You need to display the bitmap directly.
There are a lot of ways to do that, but the simplest is to add an Image control to your page and give it the bytes that make up the image:

<img ID="MyImage" runat="server" />




using (Bitmap xPanel = new Bitmap(200, 200))
    {
    using (Graphics objGraphicPanel = Graphics.FromImage(xPanel))
        {
        //Background White
        SolidBrush yellowBrush = new SolidBrush(Color.Yellow);
        objGraphicPanel.FillRectangle(yellowBrush, 0, 0, 200, 200);
        //Text
        SolidBrush blackBrush = new SolidBrush(Color.Black);
        Font horzFont = new Font("Verdana", 10, FontStyle.Regular);
        objGraphicPanel.DrawString("Test", horzFont, blackBrush, 15, 80);
        //Line
        Pen colorPen = new Pen(Color.Green, 2);
        objGraphicPanel.DrawLine(colorPen, 10, 100, 120, 190);
        MemoryStream ms = new MemoryStream();
        xPanel.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        string base64 = Convert.ToBase64String(ms.ToArray());
        MyImage.Src = "data:image/gif;base64," + base64;
        }
    }


这篇关于如何使用system.drawing绘制一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆