C#画图:用C#画图的时候遇到了问题
 using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Collections.Generic;
using Tools;
using DBAPI;
using Functions;
using System.Web.Script.Serialization;
using System.IO;//文件存取
using System.Drawing;//画图基本功能
using System.Drawing.Drawing2D;//二维画图
using System.Drawing.Imaging;//高级功能

public partial class V2_NodeState_AirCleaner2_Chart2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Bitmap img = new Bitmap(400, 200);//创建Bitmap对象
        MemoryStream stream = draw();

        img.Save(stream, ImageFormat.Jpeg);          //保存绘制的图片
        Response.Clear();
        Response.ContentType = "image/jpeg";
        Response.BinaryWrite(stream.ToArray());
    }
    public MemoryStream draw()
    {
        Bitmap img = new Bitmap(400, 200);//创建Bitmap对象
        Graphics g = Graphics.FromImage(img);//创建Graphics对象

        Pen Bp = new Pen(Color.Black); //定义黑色画笔
        Pen Rp = new Pen(Color.Red);//红色画笔
        Pen Sp = new Pen(Color.Blue);//蓝色
        SolidBrush redBrush = new SolidBrush(Color.Red);
        AdjustableArrowCap aac;  //定义箭头帽

        aac = new System.Drawing.Drawing2D.AdjustableArrowCap(4, 4);
        Sp.CustomStartCap = aac;  //开始端箭头帽
        //Sp.CustomEndCap = aac;

        Font Bfont = new Font("Arial", 12, FontStyle.Bold);//大标题字体
        Font font = new Font("Arial", 6);//一般字
        Font Tfont = new Font("Arial", 9);//较大字体

        float w = 1.5F;
        float h = 1.5F;
        g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height); //矩形 底色


        //黑色过度型笔刷
        LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.Black, 1.2F, true);


        //蓝色过度型笔刷
        LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);

        //g.DrawString("我的实验",Bfont,brush,40,5); //绘制大标题

        g.DrawRectangle(Bp, 0, 0, img.Width - 100, img.Height - 40); //绘制矩形与上面的矩形即是边框

        //原点坐标(150,90),一个单位坐标=8px
        g.DrawLine(Sp, 150, 10, 150, 150);//纵坐标
        g.DrawLine(Sp, 300, 90, 20, 90);//横坐标


        g.DrawString("5", font, brush, 188, 91); //横坐标数字
        g.DrawString("10", font, brush, 226, 91);
        g.DrawString("15", font, brush, 266, 91);

        //g.DrawString("0.5", font, brush, 135, 86); //纵坐标数字
        g.DrawString("1", font, brush, 135, 78);
        //g.DrawString("1.5", font, brush, 135, 78);
        g.DrawString("2", font, brush, 135, 70);
        g.DrawString("3", font, brush, 135, 62);

        g.DrawLine(Bp, 190, 85, 190, 90); //横坐标刻度线
        g.DrawLine(Bp, 230, 85, 230, 90);
        g.DrawLine(Bp, 270, 85, 270, 90);

        g.DrawLine(Bp, 155, 82, 150, 82);//纵坐标刻度线
        g.DrawLine(Bp, 155, 74, 150, 74);
        g.DrawLine(Bp, 155, 66, 150, 66);

        //画函数曲线   这样以   y=x^2  为例   用穷举函数曲线中的两点画曲线
        //画0-2中的点,步长为0.1
        for (double i = 0; i <= 2; )
        {
            double Write11 = (150 + 8 * i);
            double Write12 = 90 - 8 * (Math.Pow(i, 2));

            double Write21 = (150 + 8 * (i + 0.1));
            double Write22 = 90 - 8 * (Math.Pow(i + 0.1, 2));

            g.DrawLine(Bp, (float)Write11, (float)Write12, (float)Write21, (float)Write22);
            //img.SetPixel((int)Write11, (int)Write12, Color.Red);
            g.FillEllipse(redBrush,(float)Write11,(float)Write12, w, h);


            i = i + 0.1;
        }

        for (double i = -2; i <= 0; )
        {
            double Write11 = (150 + 8 * i);
            double Write12 = 90 - 8 * (Math.Pow(i, 2));

            double Write21 = (150 + 8 * (i + 0.1));
            double Write22 = 90 - 8 * (Math.Pow(i + 0.1, 2));

            g.DrawLine(Bp, (float)Write11, (float)Write12, (float)Write21, (float)Write22);
            i = i + 0.1;
        }

        MemoryStream stream = new MemoryStream();   //保存绘制的图片
        img.Save(stream, ImageFormat.Jpeg);          //保存绘制的图片
        return stream;
    }

}

 Bitmap img = new Bitmap(1000, 500);//创建Bitmap对象
将new Bitmap(400, 200)中的值修改为(1000,500)就会在图片中出现一个黑色的矩形框
而且经过尝试,发现只要改大,就会出现黑色的框,同时这个框也会随着修改的值而变。

如下图所示:
图片说明

3个回答

下面这句话引起的,你以400的线粗画了个矩形,但是没有fill. 中间就是黑的类,bitmap尺寸小的时候,线把所有bitmap的地方都占据了,所以没有问题
g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height); //矩形 底色

要填充底色用下面的语句
g.FillRectangle(new SolidBrush(Color.White), 0, 0, img.Width, img.Height);

g.DrawRectangle(new Pen(Color.White, 400)
这个被限定为400了,肯定大了就会有框

定义变量表示宽度,前后一致即可

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐