xiaojunzhu1 2015-01-14 05:56 采纳率: 100%
浏览 1844
已采纳

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条回答

  • 宝_爸 2015-01-14 06:24
    关注

    下面这句话引起的,你以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);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮