在uwp应用程序中替换Graphics和Pen类是什么?

时间:2017-05-30 05:59:57

标签: c# winforms graphics bitmap uwp

这是一个WinForm代码

Fragment_1 fragment = (Fragment_1) getSupportFragmentManager().findFragmentById(R.layout.fr1);

fragment.updateSearchView();

我需要在我的UWP应用中转换它。但是,我可以使用WriteableBitmap代替Bitmap和SolidColorBrush代替Pen。但是对于Graphics类应该是什么。

如果我将图形视为WriteableBitmap,除了以下这些线之外,无论如何一切都解决了 private EasyScript eScript; /// <summary> /// Graphics object we'll draw on to in order to produce a signature /// image. /// </summary> private Graphics graphics; /// <summary> /// Raster backing the graphics object. /// </summary> private Bitmap raster; /// <summary> /// Pen we'll use to create strokes on our graphics object. /// </summary> private Pen pen; /// <summary> /// The last point we captured. /// </summary> private Coordinate lastPoint = null; /// <summary> /// Whether or not the next event we receive should clear the signature. /// </summary> private bool clearOnNext = false; /// <summary> /// The current stroke count. /// </summary> private int strokeCount = 0; /// <summary> /// The amount to scale the coordinates by. /// </summary> private double scaleFactor = 1; /// <summary> /// Initializes a new instance of the <see cref="ExampleForm"/> class. /// </summary> public ExampleForm() { //Create a new EasyScript object. this.eScript = new EasyScript(); //Register ourselves as a signature listener. eScript.AddListener(this); //Initialize our form. this.InitializeComponent(); //Initialize our drawing components. raster = new Bitmap(signaturePictureBox.Width, signaturePictureBox.Height); graphics = Graphics.FromImage(raster); //Enable high quality drawing. graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.SmoothingMode = SmoothingMode.AntiAlias; pen = new Pen(Brushes.Black); //Calculate our scale factor based on the size of the picture box. scaleFactor = signaturePictureBox.Width / eScript.GetSignatureProtocol().GetWidth(); //Clear the picture box. ClearSignatureBox(); // this allows the form to preview all keyboard events before other parts of the form are allowed // to get them. If a particular keyboard event is from a ScripTouch device, // we can prevent the event from propogating to other form elements, such as a TextBox. this.KeyPreview = true; this.cardSwipeInfoTextBox.ReadOnly = true; this.signatureInfoTextBox.ReadOnly = true; }

graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.SmoothingMode = SmoothingMode.AntiAlias;

graphics.FillRectangle(Colors.White, 0, 0, signature.Width, signature.Height);

签名是我的图像对象。

提前致谢。

2 个答案:

答案 0 :(得分:2)

System.Drawing命名空间下的Graphics类更像CanvasDrawingSessionWin2D。 Win2D是一款易于使用的Windows运行时API,可用于UWP应用程序的GPU加速即时模式2D图形渲染。

例如,对于graphics.InterpolationMode媒体资源,您可以尝试使用CanvasImageInterpolationCanvasDrawingSession的{​​{3}}属性定义了与SmoothingMode类似的功能。 CanvasDrawingSession还提供了GraphicsAntialiasing方法,如上所示README.md

因此,您可以尝试在UWP应用程序中使用Win2D库来实现相同的功能。有关如何使用Win2D的更多详细信息,请参考FillRectangle 1:23:0.20 2:34:0.50 3:67:0.90 4:87:0.10 5:23:0.12 ,有关示例,请参阅Drawline

答案 1 :(得分:0)

尝试使用WritableBitmapEx库 https://www.nuget.org/packages/WriteableBitmapEx

您可以使用nuget将此软件包添加到您的项目中。

使用WritableBitmapEx,可以很容易地直接在图像上绘制多边形,形状,线条等。

例如 bmp.DrawRectangle(cornerPoints [0] .X,cornerPoints [0] .Y,cornerPoints [2] .X,cornerPoints [2] .Y,Colors.Red);

您可以在github上使用WritableBitmapEx找到大量的绘图示例 https://github.com/reneschulte/WriteableBitmapEx