Calculate resolution for Signature Pad UWP

teofilocteofiloc USMember ✭✭

Hi Guys,
I have made a couple resources about this, but I could find some that help me yet.

I am trying to implement Signature pad for UWP, its already implement for Androip and IOS let me show You I got. Thanks.

Android Code:

    private void RenderImage(SignatureControlImage img)
     {
                try
                {
                    Path path = new Path();
                    float maxX = float.MinValue;
                    float maxY = float.MinValue;
                    var metrics = Resources.DisplayMetrics;

                    var xOffset = 40;
                    var yOffset = 0;
                    float mainOffSet = 0;
                    if (this.Height > this.Width)
                        mainOffSet = metrics.WidthPixels / metrics.Xdpi;
                    else
                        mainOffSet = metrics.HeightPixels / metrics.Xdpi;

                    if (!string.IsNullOrEmpty(img.SignaturePoints))
                    {
                        var points = img.SignaturePoints.Split(new char[] { ' ' });

                        var currentPoint = new PointF(0, 0);

                        for (int i = 0; i < points.Length - 1; i++)
                        {
                            var pointSplitted = points[i].Split(new char[] { ',' });
                            if (pointSplitted.Length > 0)
                            {
                                var nextPoint = new PointF(float.Parse(pointSplitted[0]), float.Parse(pointSplitted[1]));
                                if (nextPoint.X != 0 && nextPoint.Y != 0)
                                {
                                    if (Math.Abs(currentPoint.X - nextPoint.X) > 10 || Math.Abs(currentPoint.Y - nextPoint.Y) > 10)
                                    {
                                        path.MoveTo(nextPoint.X / mainOffSet + xOffset, nextPoint.Y / mainOffSet - yOffset);
                                        currentPoint = nextPoint;
                                    }
                                    else
                                    {
                                        path.LineTo(nextPoint.X / mainOffSet + xOffset, nextPoint.Y / mainOffSet - yOffset);
                                        currentPoint = nextPoint;
                                    }

                                    //Update the Max Value
                                    if (maxX < nextPoint.X)
                                        maxX = nextPoint.X;
                                    if (maxY < nextPoint.Y)
                                        maxY = nextPoint.Y;
                                }
                            }
                        }

                        var rect = new Rect(0, 0, (int)maxX, (int)maxY);
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.InJustDecodeBounds = true;

                        using (var output = BitmapFactory.DecodeResource(this.Context.Resources, Resource.Drawable.signatureLine))
                        {
                            var scaledBitmap = output.Copy(Bitmap.Config.Argb8888, true);

                            scaledBitmap = Bitmap.CreateScaledBitmap(scaledBitmap, (int)(output.Width), (int)(output.Height), true);

                            var canvas = new Canvas(scaledBitmap);

                            var paint = new Paint(PaintFlags.FilterBitmap);
                            paint.AntiAlias = true;
                            paint.Color = Android.Graphics.Color.Black;
                            paint.SetStyle(Paint.Style.Stroke);
                            paint.StrokeWidth = 2;

                            //canvas.DrawBitmap(scaledBitmap, 0, 0, paint);

                            canvas.DrawPath(path, paint);

                            this.Control.SetImageBitmap(scaledBitmap);

                            // Forces the internal method of InvalidateMeasure to be called.
                            this.Element.WidthRequest = this.Element.WidthRequest;

                            //Invalidate();
                        }
                    }
                    else
                    {
                        this.Control.SetImageBitmap(BitmapFactory.DecodeResource(this.Context.Resources, Resource.Drawable.signature));
                    }
                }
                catch (Exception ex)
                {
                    Xamarin.Insights.Report(ex);
                }
            }
        }

IOS code:

public override void Draw(CGRect rect)
        {
            SignatureControlImage img = this.Element as SignatureControlImage;
            if (!string.IsNullOrEmpty(img.SignaturePoints))
            {
                using (var context = UIGraphics.GetCurrentContext())
                {
                    context.SetFillColor(Color.Black.ToCGColor());
                    context.SetStrokeColor(Color.Black.ToCGColor());
                    context.SetLineWidth(2);

                    UIImage image = UIImage.FromFile("signatureLine.png");

                    nfloat scale = 0;
                    if (UIScreen.MainScreen.Bounds.Height > UIScreen.MainScreen.Bounds.Width)
                        scale = image.Size.Width / UIScreen.MainScreen.Bounds.Height;
                    else
                        scale = image.Size.Width / UIScreen.MainScreen.Bounds.Width;

                    nfloat xOffset = 40 * scale;
                    nfloat yOffset = 0;

                    var points = img.SignaturePoints.Split(new char[] { ' ' });
                    var currentPoint = CGPoint.Empty;

                    CGPath path = new CGPath();
                    nfloat maxX = float.MinValue;
                    nfloat maxY = float.MinValue;

                    for (int i = 0; i < points.Length - 1; i++)
                    {
                        var pointSplitted = points[i].Split(new char[] { ',' });
                        if (pointSplitted.Length > 0)
                        {
                            var nextPoint = new CGPoint(double.Parse(pointSplitted[0]), double.Parse(pointSplitted[1]));
                            if (!nextPoint.IsEmpty)
                            {
                                if (Math.Abs(currentPoint.X - nextPoint.X) > 5 || Math.Abs(currentPoint.Y - nextPoint.Y) > 5)
                                {
                                    path.MoveToPoint(new CGPoint(nextPoint.X * scale + xOffset, nextPoint.Y * scale - yOffset));
                                    currentPoint = nextPoint;
                                }
                                else
                                {
                                    path.AddLineToPoint(new CGPoint(nextPoint.X * scale + xOffset, nextPoint.Y * scale - yOffset));
                                    currentPoint = nextPoint;
                                }

                                //Update the Max Value
                                if (maxX < nextPoint.X)
                                    maxX = nextPoint.X;
                                if (maxY < nextPoint.Y)
                                    maxY = nextPoint.Y;
                            }
                        }
                    }

                    this.Control.Image = CreateImageWithPath(path, image);
                }
            }
            else
            {
                this.Control.Image = UIImage.FromFile("signature.png");
            }
        }


        public static UIImage CreateImageWithPath(CGPath path, UIImage myImage)
        {
            var CGmyImage = RotateImage(myImage.CGImage);
            CGRect desiredSize = new CGRect(0, 0, myImage.Size.Width, myImage.Size.Height);
            CGSize finalSize = new CGSize(myImage.Size.Width, myImage.Size.Height);
            UIGraphics.BeginImageContextWithOptions(finalSize, false, 0);

            UIColor.DarkGray.SetColor();
            CGContext oContext = UIGraphics.GetCurrentContext();

            oContext.SetFillColor(Color.Black.ToCGColor());
            oContext.SetStrokeColor(Color.Black.ToCGColor());
            oContext.SetLineWidth(2);

            oContext.DrawImage(desiredSize, CGmyImage);

            oContext.AddPath(path);

            oContext.DrawPath(CGPathDrawingMode.Stroke);

            oContext.SaveState();

            var resultImage = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();


            return resultImage;

        }

        public static CGImage RotateImage(CGImage oImage)
        {
            CGContext oContext;
            UIImage oRotatedImage;
            CGRect oImageBounds;
            //
            oImageBounds = new CGRect(0, 0, oImage.Width, oImage.Height);
            //
            UIGraphics.BeginImageContext(oImageBounds.Size);
            oContext = UIGraphics.GetCurrentContext();
            //
            oContext.DrawImage(oImageBounds, oImage);
            oRotatedImage = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
            //
            return oRotatedImage.CGImage;
        }

Best Answer

  • teofilocteofiloc US ✭✭
    Accepted Answer

    Thank you very much, this will serve me a lot.

Answers

Sign In or Register to comment.