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

ops/2024/9/23 7:16:49/

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.ppmy.cn/ops/114649.html

相关文章

k8s下的网络通信与调度

目录 一、k8s网络通信 1、k8s通信整体架构 2、flannel网络插件 &#xff08;1&#xff09;flannel跨主机通信原理 &#xff08;2&#xff09;flannel支持的后端模式 3、calico网络插件 &#xff08;1&#xff09;简介 &#xff08;2&#xff09;网络架构 &#xff08;…

Lua中..和...的使用区别

一. .. 的用法 二. ... 的用法 在 Lua 中&#xff0c;... 是一个特殊符号&#xff0c;它用于表示不定数量的参数。当你在函数定义或调用中使用 ... 时&#xff0c;它可以匹配任意数量的参数&#xff0c;并将它们作为列表传递。在您的代码示例中&am…

FinGPT金融大模型

FinGPT仓库https://github.com/AI4Finance-Foundation/FinGPT 功能&#xff1a; Adviser。根据新闻判断市场情绪&#xff08;积极、消极、中性&#xff09;&#xff0c;给出投资建议。Quantitative Trading。定制属于自己的金融助手。叫它关注某几个股票、监测消息等。可以直…

安卓数据存储——SharedPreferences

共享参数 SharedPreferences 1、sharedPreferences是Android的一个轻量级存储工具&#xff0c;采用的存储结构是key - value的键值对方式 2、共享参数的存储介质是符合XML规范的配置文件。保存路径是&#xff1a;/data/data/应用包名/shared_prefs/文件名.xml 使用场景&…

node js版本低导致冲突WARN EBADENGINE package: required: { node: ‘>=18‘ }

重新安装依赖包 1、删除旧的 node_modules 目录和 package-lock.json 文件&#xff1a; rm -rf node_modules rm package-lock.json2、升级node版本 wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bashexport NVM_DIR"$([ -z "${…

Android Glide:让图片加载从未如此简单

在 Android 开发中,图片加载一直是一个关键环节。无论是从网络还是本地加载图片,都需要考虑到性能、内存管理和用户体验等多个方面。而在这方面,Glide 成为了众多开发者的首选库之一。本文将带你深入了解 Glide 的强大之处,并介绍如何在项目中快速集成和使用 Glide。 为什…

旋转机械故障数据集 全网首发

旋转机械故障 数据集 11G资料 泵、齿轮箱、电机、流量、液压系统、轴承(西储大学、辛辛那提大学、FEMTO、MOSFET)、PHM08挑战数据集、我闪发动机降级模拟数据集、铣床等 旋转机械故障数据集 数据集描述 该数据集是一个综合性的旋转机械故障检测和诊断数据集&#xff0c;旨在…

语言模型的在线策略提炼:从自我错误中学习

原论文&#xff1a;On-Policy Distillation of Language Models: Learning from Self-Generated Mistakes 摘要 知识蒸馏&#xff08;KD&#xff09;被广泛用于通过训练较小的学生模型来压缩教师模型&#xff0c;以降低推理成本和内存占用。然而&#xff0c;当前用于自回归序…