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)就会在图片中出现一个黑色的矩形框
而且经过尝试,发现只要改大,就会出现黑色的框,同时这个框也会随着修改的值而变。

C#画图:用C#画图的时候遇到了问题
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
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);本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报