首页 > 代码库 > Graphics 导出图片使用【目前只是代码,以后完善。】
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=0;i<signalOpinionList.Count;i++) { seriesOpinionOffsetDic.Add(signalOpinionList[i].Name, 80 + 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 = 80 + signalOpinionList.Count * itemWidth; Bitmap bmp = new Bitmap(imgWidth, (list.Count + 2) * 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 = 80 + seriesNameArr.Length * itemWidth;//画布宽 int imgHeight = (signalFlowInfos.Count + 2) * itemHeight;//画布高 Font fontTitle = new Font("宋体", 12, FontStyle.Bold); Font font = new Font("宋体", 9, FontStyle.Regular); #region 定义表头 Point cellLocation = new Point(5, 5); StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; Rectangle cellRect; Pen pen = new Pen(Brushes.Black, 1); ; for (int i = 0; i < seriesNameArr.Length; i++) { string name=seriesNameArr[i]; cellRect = new Rectangle(seriesOpinionOffsetDic[name] + (itemWidth-100)/2, 5, 100, 30);//100为矩形tIttle的宽度 //g.FillRectangle(seriesOpinionBrushDic[name], cellRect); CreateRoundRectangle.FillRoundRectangle(g, seriesOpinionBrushDic[name], cellRect, 5); pen.Brush = Brushes.Black; pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;//画虚线 CreateRoundRectangle.DrawRoundRectangle(g, pen, cellRect, 5); g.DrawString(name, fontTitle, Brushes.Black, seriesOpinionOffsetDic[name] + itemWidth/2, 20, format); //pen = new Pen(seriesOpinionBrushDic[name], 2);//柱子颜色 pen.Brush = seriesOpinionBrushDic[name]; pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;//画虚线 pen.DashPattern = new float[] { 3, 2 }; int xline=seriesOpinionOffsetDic[name] + itemWidth/2; g.DrawLine(pen, xline, 35, xline, imgHeight);//画柱子 } #endregion int hight = 35; 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 = 0; i < signalFlowInfos.Count; i++) { #region 第一行 fromX1Name = signalFlowInfos[i].From; toX2Name = signalFlowInfos[i].To; seriesOffsetTo = seriesOpinionOffsetDic[toX2Name] + itemWidth / 2; seriesOffsetFrom = seriesOpinionOffsetDic[fromX1Name] + itemWidth / 2; cellRect = new Rectangle(6, hight + 1, imgWidth, itemHeight); format.Alignment = StringAlignment.Near; g.DrawString(signalFlowInfos[i].Time, font, Brushes.Black, cellRect, format); //连接线和箭头 pen.Width = 2; if (Array.IndexOf(seriesNameArr, fromX1Name) < Array.IndexOf(seriesNameArr, toX2Name))//画向右箭头 { pen.Color = colorTO; g.DrawLine(pen, seriesOffsetTo - 10, hight + itemHeight/2-5, seriesOffsetTo - 5, hight + itemHeight / 2);//线上 g.DrawLine(pen, seriesOffsetTo - 10, hight + itemHeight/2+5, seriesOffsetTo - 5, hight + itemHeight / 2);//线下 } else//画向左箭头 { pen.Color = colorFrom; g.DrawLine(pen, seriesOffsetTo + 5, hight + itemHeight / 2, seriesOffsetTo + 10, hight + itemHeight/2-5);//线上 g.DrawLine(pen, seriesOffsetTo + 5, hight + itemHeight / 2, seriesOffsetTo + 10, hight + itemHeight/2+5);//线下 } pen.Width = 1; g.DrawLine(pen, seriesOffsetFrom, hight + itemHeight / 2, seriesOffsetTo, hight + itemHeight/2); cellRect = new Rectangle(seriesOffsetFrom - 5, hight + itemHeight/2-5, 10, 10); g.FillEllipse(seriesOpinionBrushDic[fromX1Name], cellRect);//画圆 g.DrawEllipse(Pens.Black, cellRect);//画边框 cellRect = new Rectangle(seriesOffsetTo - 5, hight + itemHeight/2-5, 10, 10); g.FillEllipse(seriesOpinionBrushDic[toX2Name], cellRect); g.DrawEllipse(Pens.Black, cellRect); //写Message cellRect = new Rectangle(seriesOffsetFrom < seriesOffsetTo ? seriesOffsetFrom : seriesOffsetTo, hight, itemWidth - 10, itemHeight); format.Alignment = StringAlignment.Center; g.DrawString(signalFlowInfos[i].Message, font, Brushes.Black, cellRect, format); #endregion hight += itemHeight; } }
Graphics 导出图片使用【目前只是代码,以后完善。】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。