Graphics 导出图片使用【这个主要是画图类图的使用,记录一下】

 /// <summary>
/// 导出信令流程矢量图
/// </summary>
/// <param name="signalFlowInfos">信令流程</param>
/// <returns>路径</returns>
public string ExportToImage(List<SignalFlowInfo> list)
{
List<SignalOpinion> signalOpinionList=GetSignalOpinion();
seriesOpinionOffsetDic = new Dictionary<string, int>();
seriesOpinionBrushDic = new Dictionary<string, Brush>();
string[] seriesNameArr = new string[signalOpinionList.Count];
for(int i=;i<signalOpinionList.Count;i++)
{
seriesOpinionOffsetDic.Add(signalOpinionList[i].Name, + itemWidth * i);
seriesOpinionBrushDic.Add(signalOpinionList[i].Name, new SolidBrush(Color.FromArgb(signalOpinionList[i].ColorA,signalOpinionList[i].ColorR,signalOpinionList[i].ColorG,signalOpinionList[i].ColorB)));
seriesNameArr[i] = signalOpinionList[i].Name;
} int imgWidth = + signalOpinionList.Count * itemWidth;
Bitmap bmp = new Bitmap(imgWidth, (list.Count + ) * itemHeight);
Graphics gs = Graphics.FromImage(bmp);
if (!Directory.Exists(TempFilesPath))
{
Directory.CreateDirectory(TempFilesPath);
}
string fileName = "信令流程图" + System.Guid.NewGuid().ToString() + ".zip";
string filePath = TempFilesPath + "\\" + fileName;
Metafile mf = null;
Graphics g = null;
try
{
mf = new Metafile(filePath, gs.GetHdc());
g = Graphics.FromImage(mf);
DrawNew(g, list, seriesNameArr);
g.Save();
}
catch (Exception err)
{
WebLogLayout.Error(new List<string>() { "导出信令流程矢量图失败:" + err.Message });
//return "";
Exception e = new Exception("导出信令流程矢量图失败", err);
throw e;
}
finally
{
if (g != null)
g.Dispose();
if (mf != null)
mf.Dispose();
}
return fileName;
} /// <summary>
/// 画图
/// </summary>
/// <param name="g">GDI+ 绘图图面</param>
/// <param name="signalFlowInfos">数据对象集合</param>
private void DrawNew(Graphics g, List<SignalFlowInfo> signalFlowInfos,string[] seriesNameArr )
{
int imgWidth = + seriesNameArr.Length * itemWidth;//画布宽
int imgHeight = (signalFlowInfos.Count + ) * itemHeight;//画布高
Font fontTitle = new Font("宋体", , FontStyle.Bold);
Font font = new Font("宋体", , FontStyle.Regular);
#region 定义表头
Point cellLocation = new Point(, );
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center; Rectangle cellRect;
Pen pen = new Pen(Brushes.Black, ); ;
for (int i = ; i < seriesNameArr.Length; i++)
{
string name=seriesNameArr[i];
cellRect = new Rectangle(seriesOpinionOffsetDic[name] + (itemWidth-)/, , , );//100为矩形tIttle的宽度
//g.FillRectangle(seriesOpinionBrushDic[name], cellRect);
CreateRoundRectangle.FillRoundRectangle(g, seriesOpinionBrushDic[name], cellRect, );
pen.Brush = Brushes.Black;
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;//画虚线
CreateRoundRectangle.DrawRoundRectangle(g, pen, cellRect, );
g.DrawString(name, fontTitle, Brushes.Black, seriesOpinionOffsetDic[name] + itemWidth/, , format); //pen = new Pen(seriesOpinionBrushDic[name], 2);//柱子颜色
pen.Brush = seriesOpinionBrushDic[name];
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;//画虚线
pen.DashPattern = new float[] { , };
int xline=seriesOpinionOffsetDic[name] + itemWidth/;
g.DrawLine(pen, xline, , xline, imgHeight);//画柱子
} #endregion
int hight = ;
Color colorTO = ColorTranslator.FromHtml("#008000");//向右方向颜色
Color colorFrom = ColorTranslator.FromHtml("#FFA500");//向左方向颜色 string fromX1Name;
string toX2Name;
int seriesOffsetTo;
int seriesOffsetFrom;
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;//画实线
for (int i = ; i < signalFlowInfos.Count; i++)
{
#region 第一行
fromX1Name = signalFlowInfos[i].From;
toX2Name = signalFlowInfos[i].To;
seriesOffsetTo = seriesOpinionOffsetDic[toX2Name] + itemWidth / ;
seriesOffsetFrom = seriesOpinionOffsetDic[fromX1Name] + itemWidth / ;
cellRect = new Rectangle(, hight + , imgWidth, itemHeight);
format.Alignment = StringAlignment.Near;
g.DrawString(signalFlowInfos[i].Time, font, Brushes.Black, cellRect, format); //连接线和箭头
pen.Width = ;
if (Array.IndexOf(seriesNameArr, fromX1Name) < Array.IndexOf(seriesNameArr, toX2Name))//画向右箭头
{
pen.Color = colorTO;
g.DrawLine(pen, seriesOffsetTo - , hight + itemHeight/-, seriesOffsetTo - , hight + itemHeight / );//线上
g.DrawLine(pen, seriesOffsetTo - , hight + itemHeight/+, seriesOffsetTo - , hight + itemHeight / );//线下
}
else//画向左箭头
{
pen.Color = colorFrom;
g.DrawLine(pen, seriesOffsetTo + , hight + itemHeight / , seriesOffsetTo + , hight + itemHeight/-);//线上
g.DrawLine(pen, seriesOffsetTo + , hight + itemHeight / , seriesOffsetTo + , hight + itemHeight/+);//线下
}
pen.Width = ;
g.DrawLine(pen, seriesOffsetFrom, hight + itemHeight / , seriesOffsetTo, hight + itemHeight/);
cellRect = new Rectangle(seriesOffsetFrom - , hight + itemHeight/-, , );
g.FillEllipse(seriesOpinionBrushDic[fromX1Name], cellRect);//画圆
g.DrawEllipse(Pens.Black, cellRect);//画边框
cellRect = new Rectangle(seriesOffsetTo - , hight + itemHeight/-, , );
g.FillEllipse(seriesOpinionBrushDic[toX2Name], cellRect);
g.DrawEllipse(Pens.Black, cellRect);
//写Message
cellRect = new Rectangle(seriesOffsetFrom < seriesOffsetTo ? seriesOffsetFrom : seriesOffsetTo, hight, itemWidth - , itemHeight);
format.Alignment = StringAlignment.Center;
g.DrawString(signalFlowInfos[i].Message, font, Brushes.Black, cellRect, format);
#endregion
hight += itemHeight;
}
}

这是项目源码的一部分。

这个例子一些常用的绘图方法都有了,不够的情况可以参考msdn Graphics类

上一篇:安装mac os x时about a second remaining解决方法


下一篇:m序列