NET简单的一个画图程序
using System;
using System.Drawing;
//HttpUtility.UrlEncode /// <summary>
///Curve 的摘要说明
/// </summary>
public class Curve
{
public Curve()
{
//
//TODO: 在此处添加构造函数逻辑
//
} /*******************************************************属性设置**************************************************************/
/// <summary>
/// 房价数组
/// </summary>
private string[] _CurveData;
/// <summary>
/// 房价时间
/// </summary>
//private string[] _CurveDate;
public string[] CurveData
{
get { return _CurveData; }
set { _CurveData = value; }
}
//public string[] CurveDate
//{
// get { return _CurveDate; }
// set { _CurveDate = value; }
//} /// <summary>
/// 是否显示曲线
/// </summary>
private bool _IsDemonstrationCurve=true;
public bool IsDemonstrationCurve
{
get { return _IsDemonstrationCurve;}
set {
if(value==false)
{
_IsDemonstrationCurve = false;
}else
{
_IsDemonstrationCurve = true;
} }
}
/// <summary>
/// 是否显示 矩形图
/// </summary>
private bool _IsDemonstrationRectangle=true;
public bool IsDemonstrationRectangle
{
get { return _IsDemonstrationRectangle; }
set
{
if(value==false)
{
_IsDemonstrationRectangle = false;
}else
{
_IsDemonstrationRectangle = true;
}
} }
/*****************************************属性设置结束***********************************************************************************/ public bool getCurve(string FileName)
{ try
{
/***********************大图的参数*****************************/
/*自写编码*/
int ImgWidth = ; //1.[总宽度] ***图宽度
int ImgHeight = ; //2.[总高度] ***图高度
int ItemNum = ; //3.[项目数量] ***图表划分的块
//int ChildSpace=15; //6.[各块间的间距]
int ChartLeft = ; //7.[图表左边距] ***图表距图的左边距离
int ChartRight = ; //8.[图表右边距] ***图表距图的右边距离
int ChartTop = ; //9.[图表顶边距] ***图表距图顶边距离
int ChartBottom = ; //10.[图表底边距] ***图表距图底边距离 /*************************坐标位置调整******************************************/
int YMaxValue = ; //11.[纵坐标标尺最大值] ***纵坐标标尺的最大值
int YItemNum = ; //12.[纵坐标标尺段数] ***纵坐标标尺的段数
int YTop = ; //13.[距纵轴顶端间隔]
int YStrStart = ; //14.[纵坐标标尺文字起始X坐标]
int XRight = ; //15.[距横轴右端间隔]
int XStrStart = ; //16.[横坐标标尺文字起始Y坐标] /*************************小刻度位置调整*********************************/
int ChildNum = ; //4.[块数] ***大块中划分的子项的数量
float ChildRate = 0.6f; //5.[各块总占空间比率]
int xiaoSpacing = ; //小标示和小刻度之间的距离
int Xxiaospace_between = ; //矩形距离小刻度的位置
int intjiange = ; //顶部名称说明距离 左边的距离 string[] strGIFName = new string[ItemNum]; //大标示 注意和项目个数有联系
//string strGIFName = "2009年价格走势图";
/*因为 最近要换 所以 只有今年的*/
for (int intGIF = ; intGIF <= ItemNum; intGIF++)
{ strGIFName[intGIF - ] = + intGIF + "年价格走势图";
// arrValueNames[1] = "2010年价格走势图";
// arrValueNames[2] = "2011年价格走势图";
} /*****************************各项数值的计算****************************************/
//[图表总宽度]=[总宽度]-[图表左边距]-[图表右边距]-[距横轴右端间隔]
int chartwidth = ImgWidth - ChartLeft - ChartRight - XRight;
//[项目宽度]=[图表总宽度]/[项目数量]
int itemwidth = chartwidth/ItemNum;
//[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
int factwidth = Convert.ToInt32(Math.Floor(itemwidth*ChildRate));
//[各块矩形宽度]=[各块总占空间比率的实际宽度]/[块数]
int rectanglewidth = factwidth/ChildNum;
//[各块间的间距]=([项目宽度]-[各块总占空间比率的实际宽度])/([块数]+1)
int falge = ((itemwidth - factwidth)/(ChildNum + ));
decimal myFale = falge;
int childspace = Convert.ToInt32(Math.Floor(myFale));
//(itemwidth-factwidth)/(ChildNum+1) /***********************************开始画图******************************************************/
Graphics objGps; //建立画板对象
Bitmap objBitMap = new Bitmap(ImgWidth, ImgHeight); //建立位图对象
objGps = Graphics.FromImage(objBitMap); //根据位图对象建立画板对象
objGps.Clear(Color.White); //设置画板对象的背景色
/***************************横坐标设置*************************************************/
string[] strArrValueName = new string[ChildNum]; //标示名称(月份) 注意长度要和ChildNum 相同 //横坐标设置
for (int intquarter = ; intquarter <= strArrValueName.Length; intquarter++)
{
if (intquarter% == )
{
strArrValueName[intquarter - ] = "1~3月";
}
if (intquarter% == )
{
strArrValueName[intquarter - ] = "4~6月";
}
if (intquarter% == )
{
strArrValueName[intquarter - ] = "7~9月";
}
if (intquarter% == )
{
strArrValueName[intquarter - ] = "10~12月";
}
} /****************************数据的赋值*******************************/
/*********************************************************************/
//显示的数据
int[] arrValues = {, , , , , , , , , , , }; //数据数组
//CurveData for (int inttheNum = ; inttheNum <ChildNum; inttheNum++)
{ /* (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i 纵坐标的计算方法 */
//ImgHeight - ChartBottom - ChartTop - YTop 总高度-距离顶部高度-距离底部高度-y坐标距离顶部高度/标示个数个数=单个坐标的高度
// ImgHeight - ChartBottom-单个坐标的高度 总高度-距离底部高度-单个项目的高度
double dange = (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum; //单个高度的计算 double floatfalge = (double.Parse(CurveData[inttheNum])) * dange;
// double floatfalge = (ImgHeight - ChartBottom - ChartTop - YTop)*(inttheNum + 1)*400/YMaxValue;
arrValues[inttheNum] = Convert.ToInt32(Math.Floor(floatfalge));
} /***************************************纵坐标显示标示设置*******************************************/
//string[] arrValueNames = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"};
string[] arrValueNames = new string[YItemNum]; //这里表示所有的纵坐标的标示度名称
for (int intValueName = ; intValueName < arrValueNames.Length; intValueName++)
{
arrValueNames[intValueName] = ((intValueName*) + ).ToString();
} //得出矩形宽度,和画图X轴位置 //[项目宽度]=[总宽度]/[项目数量]
//======[各块总占空间比率]=([各块矩形宽度]+[各块间的间距])/[项目宽度]
//[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
//[各块矩形宽度]=([各块总占空间比率的实际宽度]-[各块间的间距]*([块数]))/[块数]
//[一边空余空间宽度]=([项目宽度]-[各块所占空间比率的总宽度])/2 System.Drawing.Point[] pi = new Point[arrValues.Length]; //定义折线点的对象数组
System.Drawing.Point[] pit = new Point[]; //定义坐标三角点的对象数组
System.Drawing.Pen pe = new Pen(new SolidBrush(GetColor()), 1f); //定义画直线的对象
//画纵轴
objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom), new Point(ChartLeft, ChartTop));
//画纵轴终点箭头
pit[].X = ImgWidth - ChartRight; //确定三角形三点的位置
pit[].Y = ImgHeight - ChartBottom - ;
pit[].X = ImgWidth - ChartRight;
pit[].Y = ImgHeight - ChartBottom + ;
pit[].X = ImgWidth - ChartRight + ;
pit[].Y = ImgHeight - ChartBottom;
objGps.FillPolygon(new SolidBrush(GetColor()), pit);
//画纵轴标尺和标尺描述
for (int i = ; i <= YItemNum; i++)
{
//画标尺
objGps.DrawLine(pe,
new PointF(ChartLeft,
ImgHeight - ChartBottom -
(ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i),
new PointF(ChartLeft - ,
ImgHeight - ChartBottom -
(ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i));
//画描述
objGps.DrawString(arrValueNames[i - ].ToString(), new Font("宋体", ), Brushes.Black,
new Point(YStrStart,
ImgHeight - ChartBottom -
(ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i - ));
}
//画横轴
objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom),
new Point(ImgWidth - ChartRight, ImgHeight - ChartBottom));
//画横轴终点箭头
pit[].X = ChartLeft - ; //确定三角形三点的位置
pit[].Y = ChartTop;
pit[].X = ChartLeft + ;
pit[].Y = ChartTop;
pit[].X = ChartLeft;
pit[].Y = ChartTop - ;
objGps.FillPolygon(new SolidBrush(GetColor()), pit);
//画横轴标尺和标尺描述 arrValueNames = strGIFName; for (int i = ; i <= ItemNum; i++)
{
//i表示画的 第几个 ImgHeight - ChartBottom + 5 表示线的长度
//pe 表示pen 定义的一只笔
// ChartLeft + itemwidth * i, ImgHeight - ChartBottom 横坐标=左边距+显示个数 ImgHeight - ChartBottom纵坐标=图片总高度-底边距离 objGps.DrawLine(pe, new PointF(ChartLeft + itemwidth*i, ImgHeight - ChartBottom),
new PointF(ChartLeft + itemwidth*i, ImgHeight - ChartBottom + )); //画横坐标大刻度 //画小刻度和标示
for (int j = ; j <= ChildNum; j++)
{
//
float widthX; //定义起点和重点的横坐标
widthX = ChartLeft + itemwidth*j/ChildNum + (i - )*itemwidth;
//画小的刻度
objGps.DrawLine(pe, new PointF(widthX, ImgHeight - ChartBottom),
new PointF(widthX, ImgHeight - ChartBottom + ));
//画小标示 //arrValueNames[]
// ChartLeft + childspace + itemwidth * (i - 1) 横坐标=左边距+间隔+个数
int widthxiao;
widthxiao = ChartLeft + xiaoSpacing + itemwidth*(j - )/ChildNum + itemwidth*(i - );
objGps.DrawString(strArrValueName[j - ].ToString(), new Font("宋体", ), Brushes.Blue,
new Point(widthxiao, ImgHeight - ChartBottom + XStrStart - ));
} objGps.DrawString(arrValueNames[i - ].ToString(), new Font("宋体", ), Brushes.Black,
new Point(ChartLeft + intjiange + itemwidth*(i - ),
ImgHeight - ChartBottom + XStrStart));
} //框架完成 开始画图
for (int intfalge = ; intfalge <= ItemNum; intfalge++)
{ for (int j = ; j < ChildNum; j++) //画矩形图和折线图
{
int theintwidth = ;
//左边距+各块间距
theintwidth = ChartLeft + itemwidth * j / ChildNum + Xxiaospace_between + (intfalge - ) * itemwidth; if(IsDemonstrationRectangle==true)
{
//画矩形
//5表示距离小刻度的位置
//j * (childspace + rectanglewidth)) + childspace + ChartLeft
//画矩形里面的颜色
objGps.FillRectangle(new SolidBrush(GetColor(j)), theintwidth,
ImgHeight - ChartBottom - arrValues[j],
rectanglewidth, arrValues[j]);
//画矩形外框
objGps.DrawRectangle(Pens.Black, theintwidth, ImgHeight - ChartBottom - arrValues[j], rectanglewidth,
arrValues[j]);
} //********************************画折线图****************************************************
if(IsDemonstrationCurve==true)
{
// pi[j].X = (j * (childspace + rectanglewidth)) + childspace + ChartLeft;
pi[j].X = theintwidth; //定义折线的横坐标
pi[j].Y = ImgHeight - ChartBottom - arrValues[j]; //纵坐标=图片总高度-底部高度-实际数值 pe.SetLineCap(System.Drawing.Drawing2D.LineCap.RoundAnchor,
System.Drawing.Drawing2D.LineCap.RoundAnchor,
System.Drawing.Drawing2D.DashCap.Round);
if (j > ) //进行连线
{
objGps.DrawLine(pe, pi[j - ], pi[j]);
}
}
}
} //objBitMap.Save(Response.OutputStream, ImageFormat.Gif);//该位图对象以"GIF"格式输出
//objBitMap.Save();//进行保存
System.Web.HttpServerUtility myServer = System.Web.HttpContext.Current.Server;
string strPath= myServer.MapPath("~/CurveGIF/"); //string strPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath.ToString() +
//"/CurveGIF/"+FileName+".gif"; objBitMap.Save(strPath+FileName+".gif");
pe.Dispose();
objBitMap.Dispose();
GC.Collect(); //资源释放 垃圾回收
return true;
}catch(Exception ex)
{
return false;
}
} /**/
/// <param name="itemIndex">系统定义的颜色,有效值0到7,分别为(Blue,Yellow,Red,Orange,Purple,Brown,Pink,Black)</param>
/// <returns></returns>
public static Color GetColor(int itemIndex)
{
Color objColor = new Color();
switch (itemIndex)
{
case :
objColor = Color.Blue;
break;
case :
objColor = Color.Yellow;
break;
case :
objColor = Color.Red;
break;
case :
objColor = Color.Orange;
break;
case :
objColor = Color.Purple;
break;
case :
objColor = Color.Brown;
break;
case :
objColor = Color.Pink;
break;
case :
objColor = Color.Peru;
break;
case :
objColor = Color.PowderBlue;
break;
case :
objColor = Color.RosyBrown;
break;
case :
objColor = Color.RoyalBlue;
break;
case :
objColor = Color.SaddleBrown;
break;
case :
objColor = Color.SeaGreen;
break;
default:
objColor = Color.Black;
break;
}
return objColor;
} /**/
/// <param name="red">自定义颜色红色分量值,有效值0到255</param>
/// <param name="green">自定义颜色绿色分量值,有效值0到255</param>
/// <param name="blue">自定义颜色蓝色分量值,有效值0到255</param>
/// <returns></returns>
public static Color GetColor(int red, int green, int blue)
{
Color objColor = new Color();
objColor = Color.FromArgb(red, green, blue);
return objColor;
} }
然后这里是 调用的方法:
Curve myCurve = new Curve();
/**设置参数**/
myCurve.IsDemonstrationCurve = true; //是否显示曲折线
myCurve.IsDemonstrationRectangle = false;//是否显示矩形
string[] strValue = {"","","","1.5"};//显示的数据 现在只能显示4 个
myCurve.CurveData = strValue; //赋值
myCurve.getCurve(Guid.NewGuid().ToString());//创建GIF 保存
Response.Write("GIF生成成功!");
不过要注意的是 需要建立文件夹
出处:http://blog.csdn.net/jacky4955/article/details/4061658
NET简单的一个画图程序的更多相关文章
- 最简单的一个win32程序
#include <windows.h> HINSTANCE g_hInst = NULL; //2 窗口处理函数 LRESULT CALLBACK WndProc( HWND hWnd, ...
- Xamarin开发的一个简单画图程序分享
最近Xamarin比较火,于是稍微看了下,感觉接触过MVC的都应该能很快上手,还挺有意思,于是忍不住写了个简单的画图程序,之前看帖子有人说装不上或者无法部署,估计我比较幸运,编译完了一次就安装成功了, ...
- 一个简单的P2P传输程序
写了一个简单的P2P传输程序,在P2P的圈子中传输文件,不过为了简便,这个程序没有真正的传输文件,只是简单的判断一下文件的位置在哪里.这个程序可以处理当有一个peer闪退的情况,在这种情况下,剩下的p ...
- IOS开发之小实例--使用UIImagePickerController创建一个简单的相机应用程序
前言:本篇博文是本人阅读国外的IOS Programming Tutorial的一篇入门文章的学习过程总结,难度不大,因为是入门.主要是入门UIImagePickerController这个控制器,那 ...
- iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一.plist文件和项目结构图 说明:这是一个嵌套模型的示例 二.代码示例: YYcarsgroup.h文件代码: // // YYcar ...
- 写一个ajax程序就是如此简单
写一个ajax程序就是如此简单 ajax介绍: 1:AJAX全称为Asynchronous JavaScript and XML(异步JavaScript和XML),指一种创建交互式网页应用的网页开发 ...
- 一个简单的MDI示范程序(Delphi)
http://www.cnblogs.com/pchmonster/archive/2012/01/07/2316012.html 最为一个巩固之前有关窗体和对象的有关知识,下面就建立一个简单的MDI ...
- 一个简单的DDraw应用程序2
//------------------------------------------------------------------------- // 文件名 : 6_1.cpp// 创建者 : ...
- 一个简单的DDraw应用程序
阅读排行榜 1. C/C++ 笔试.面试题目大汇总(72915) 2. [STL]list基础(21718) 3. COM笔记-CoCreateInstance(14842) 4. C/C++ ...
随机推荐
- CentOS下 Nginx1.13.5 + PHP7.1.10 + MySQL5.7.19 源码编译安装
一.安装Nginx ①安装依赖扩展 # yum -y install wget openssl* gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng l ...
- bzoj1630 / bzoj2023 [Usaco2005 Nov]Ant Counting 数蚂蚁
Description 有一天,贝茜无聊地坐在蚂蚁洞前看蚂蚁们进进出出地搬运食物.很快贝茜发现有些蚂蚁长得几乎一模一样,于是她认为那些蚂蚁是兄弟,也就是说它们是同一个家族里的成员.她也发现整个 ...
- 20145335郝昊《网络对抗》逆向及Bof基础实践
20145335郝昊<网络对抗>逆向及Bof基础实践 1.实践说明 本次实践是对一个名为pwn1的可执行Linux文件进行操作.程序的执行流程是:主函数main调用foo函数,foo将函数 ...
- impress.js初体验——前端装X利器
impress.js 是国外一位开发者受 Prezi 启发,采用 CSS3 与 JavaScript 语言完成的一个可供开发者使用的表现层框架(演示工具).其功能包括画布的无限旋转与缩放,任意角度放置 ...
- sed删除包含指定字符串的所有行
1.以删除文件example.txt中包含字符串"=yes"的行为例,example.txt文件有以下内容: dadasdfsadf=yes=sds dsdadasdkfk dsd ...
- 【Java----字符串转义与反转义】
apache工具包common-lang中有一个很有用的处理字符串的工具类,其中之一就是StringEscapeUtils,这个工具类是在2.3版本以上加上的去的,利用它能很方便的进行html,xml ...
- 【P4语言学习】basic_routing.p4
headers.p4 /* Copyright 2013-present Barefoot Networks, Inc. Licensed under the Apache License, Vers ...
- Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)
http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...
- 双击不能运行可执行的jar文件
1.首先在命令行下运行jar包看文件是否报错(java -jar jar文件名称.jar) 如果程序中有System.out.println()语句,不想让其输出到控制台而保存到文件 ...
- ubuntu 10.04 安装arm交叉编译器
家里有一台cotext-A9(armv7-a) 的盒子,现在不用了, 一直想着废物利用.于是想怎么为这盒子编译程序. 目标机器: root@routon-h1:/# uname -a Linux ro ...