当前位置: 首页 > article >正文

利用WPF绘制轮廓并保存为图片

1.前言        

        WPF作为显示工具也挺好用,用C#开发应用软件会比较省力,当然也有其缺点,如在对效率要求较高的情况下有性能问题,本文记录用WPF绘制轮廓并保存为图片相关内容。

         显示效果也还不错,满足调试使用了,

2.代码

private static void DrawCurve(DrawingContext drawingContext, Pen pen, ICurve iCurve)
      {
         if(iCurve is HLine2d)
         {
            var point0 = iCurve.GetEndPoint(0);
            var point1 = iCurve.GetEndPoint(1);
            drawingContext.DrawLine(pen, new Point(point0.X, point0.Y), new Point(point1.X, point1.Y));
         }
         else if(iCurve is HArc2d)
         {
            var point0 = iCurve.GetEndPoint(0);
            var point1 = iCurve.GetEndPoint(1);
            var pt0 = new Point(point0.X, point0.Y);
            var pt1 = new Point(point1.X, point1.Y);

            var hArc = iCurve as HArc2d;
            Point center = new Point(hArc.Center.X, hArc.Center.Y);
            bool bLarge = hArc.Large;
            bool bClockWise = !hArc.ClockWise;  //窗体坐标系为左手
            double radius = hArc.Radius;

            PathFigure figure = new PathFigure() { StartPoint = pt0, IsFilled = false };
            PathGeometry path = new PathGeometry();
            path.Figures.Add(figure);
            SweepDirection dirSweep = bClockWise ? SweepDirection.Clockwise : SweepDirection.Counterclockwise;
            ArcSegment line = new ArcSegment(pt1, new Size(radius, radius), 0.0, bLarge, dirSweep, true);
            figure.Segments.Add(line);

            drawingContext.DrawGeometry(null, pen, path);
         }
      }
public static System.Drawing.Bitmap ConvertPatternToBitmap2(List<ICurve> lstICurve, List<List<ICurve>> lstIHatchProfile,
         System.Windows.Size imageSize, double? dLengthLabel = null, double? dWidthLabel = null)
      {
         var curvesTemp = new List<ICurve>();
         curvesTemp.AddRange(lstICurve);
         lstIHatchProfile?.ForEach(s => { curvesTemp.AddRange(s); });

         //  获取满布居中转换矩阵
         var matTrans = CurveUtils.GetScaleMatrix(curvesTemp, new Size(imageSize.Width, imageSize.Height - 12));

         //  转换到满布居中
         HMatrix3 mat = new HMatrix3();
         mat.M00 = matTrans.Value.M11;
         mat.M01 = matTrans.Value.M12;
         mat.M10 = matTrans.Value.M21;
         mat.M11 = matTrans.Value.M22;
         mat.M02 = matTrans.Value.OffsetX;
         mat.M12 = matTrans.Value.OffsetY;

         lstICurve = CurveUtils.CreateTransform(lstICurve, mat);
         for (int cntItem = 0; cntItem < lstIHatchProfile.Count; cntItem++)
         {
            lstIHatchProfile[cntItem] = CurveUtils.CreateTransform(lstIHatchProfile[cntItem], mat);
         }

         //  创建虚拟画布
         DrawingVisual drawingVisual = new DrawingVisual();
         DrawingContext drawingContext = drawingVisual.RenderOpen();
         Rect rect = new Rect(new Point(0, 0), imageSize);
         drawingContext.DrawRectangle(Brushes.White, (Pen)null, rect);
         Pen pen = new Pen(Brushes.Black, 2);
         foreach (var icurve in lstICurve)
         {
            DrawCurve(drawingContext, pen, icurve);
         }

         int cntPro = -1;
         foreach (var profile in lstIHatchProfile)
         {
            DrawProfile(drawingContext, Brushes.LightBlue, pen, profile);

            var boundBox = CurveUtils.GetBoundBox(profile);
            var dir = boundBox.MaxPoint - boundBox.MinPoint;
            double fontSize = Math.Max(15.0, Math.Min(dir.X, dir.Y) * 0.2);
            DrawText(drawingContext, (++cntPro).ToString(), boundBox.Center, fontSize);
         }

         DrawLabel(drawingContext, dLengthLabel, dWidthLabel, imageSize);

         drawingContext.Close();

         //截虚拟画布并生成为本地图片文件
         RenderTargetBitmap bmp = new RenderTargetBitmap((int)imageSize.Width, (int)imageSize.Height, 96, 96, PixelFormats.Pbgra32);
         bmp.Render(drawingVisual);
         BitmapEncoder encoder = new JpegBitmapEncoder();
         encoder.Frames.Add(BitmapFrame.Create(bmp));
         using (MemoryStream stream = new MemoryStream())
         {
            encoder.Save(stream);
            return new System.Drawing.Bitmap(stream);
         }

      }

 3.效果

 


http://www.kler.cn/a/315948.html

相关文章:

  • Leecode刷题C语言之统计好节点的数目
  • stream学习
  • 67页PDF |埃森哲_XX集团信息发展规划IT治理优化方案(限免下载)
  • docker更改数据目录
  • EXCEL延迟退休公式
  • JavaScript 观察者设计模式
  • Logstash 安装与部署(无坑版)
  • Vue:加载本地视频
  • 【Opencv知识】图像梯度如何理解?
  • 图文深入理解SQL语句的执行过程
  • Android下反调试与反反调试
  • 开源 AI 智能名片链动 2+1 模式 S2B2C 商城小程序与社交电商的崛起
  • 网络管理:防火墙和安全组配置详解
  • JVM 调优篇7 调优案例4- 线程溢出
  • 【Git】深入理解 Git 版本回退:方法与实践
  • node.js居家养老服务系统—计算机毕业设计源码35247
  • Codeforces Round 974 (Div. 3)D题解析
  • 集群服务器主机实现主机名与IP绑定
  • Linux下操作文件时提示:Operation not permitted
  • 基于软件项目开发流程的软件综合实训室解决方案
  • Windows10安装cuda11.3.0+cudnn8.5.0,以及创建conda虚拟环境(pytorch)
  • sklearn特征选取之RFE
  • Django高级特性和最佳实践
  • SpringBoot 配置⽂件
  • 【数据结构与算法】LeetCode:二分查找
  • MATLAB给一段数据加宽频噪声的方法(随机噪声+带通滤波器)