Ejemplo n.º 1
0
 public void DrawBitmap(IBitmap bitmap, CanvasPoint point)
 {
     _canvas?.DrawBitmap((SKBitmap)bitmap.EngineElement, point.ToSKPoint(), new SKPaint()
     {
         FilterQuality = this.InterpolationMode.ToSKFilterQuality(),
     });
 }
Ejemplo n.º 2
0
        private SKBitmap CreateAspectCorrectedBitmap(string resourceName, SKRect destRect)
        {
            // load the bitmap
            var sourceBitmap = BitmapExtensions.LoadBitmapResource(this.GetType(), resourceName);

            // create a bitmap
            SKBitmap aspectFixedBitmap = new SKBitmap
                                             ((int)destRect.Width, (int)destRect.Height);

            // creaete a canvas for that bitmap
            using (SKCanvas aspectBitmapCanvas = new SKCanvas(aspectFixedBitmap))
            {
                // render the image onto the canvas
                aspectBitmapCanvas.DrawBitmap(sourceBitmap, destRect, BitmapStretch.AspectFill);
            }
            return(aspectFixedBitmap);
        }
Ejemplo n.º 3
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            float scale = Math.Min((float)info.Width / bitmap.Width,
                                   (float)info.Height / bitmap.Height);
            float  x        = (info.Width - scale * bitmap.Width) / 2;
            float  y        = (info.Height - scale * bitmap.Height) / 2;
            SKRect destRect = new SKRect(x, y, x + scale * bitmap.Width,
                                         y + scale * bitmap.Height);

            canvas.DrawBitmap(bitmap, destRect);
        }
Ejemplo n.º 4
0
        protected static void SaveBitmap(SKBitmap bmp, string filename = "output.png")
        {
            using (var bitmap = new SKBitmap(bmp.Width, bmp.Height))
                using (var canvas = new SKCanvas(bitmap))
                {
                    canvas.Clear(SKColors.Transparent);
                    canvas.DrawBitmap(bmp, 0, 0);
                    canvas.Flush();

                    using (var stream = File.OpenWrite(Path.Combine(PathToImages, filename)))
                        using (var image = SKImage.FromBitmap(bitmap))
                            using (var data = image.Encode())
                            {
                                data.SaveTo(stream);
                            }
                }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Draws the header logo and text onto the document.
        /// </summary>
        /// <param name="canvas">The skia canvas to draw to.</param>
        private static void DrawHeader(SKCanvas canvas)
        {
            float imgX = PAGE_WIDTH / 2 - HEADER_ICON_SIZE / 2;
            float imgY = MARGIN_TOP;

            var logoStream = _assembly.GetManifestResourceStream(_assetsPath + "SQRL_icon_normal_256.png");

            logoStream.Seek(0, SeekOrigin.Begin);

            canvas.DrawBitmap(SKBitmap.Decode(logoStream),
                              new SKRect(imgX, imgY, imgX + HEADER_ICON_SIZE, imgY + HEADER_ICON_SIZE));

            DrawTextBlock(canvas,
                          _loc.GetLocalizationValue("SQRLTag"),
                          imgY + HEADER_ICON_SIZE + 20,
                          _fontBold, 12, SKColors.Black);
        }
Ejemplo n.º 6
0
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            canvas.Clear(SKColors.White);

            // load the embedded resource stream
            using (var stream = new SKManagedStream(SampleMedia.Images.ColorWheel))
                using (var codec = SKCodec.Create(stream))
                    using (var paint = new SKPaint())
                        using (var tf = SKTypeface.FromFamilyName("Arial"))
                        {
                            var info        = codec.Info;
                            var encodedInfo = codec.EncodedInfo;

                            paint.IsAntialias = true;
                            paint.TextSize    = 14;
                            paint.Typeface    = tf;
                            paint.Color       = SKColors.Black;

                            // decode the image
                            using (var bitmap = new SKBitmap(info.Width, info.Height, info.ColorType, info.IsOpaque ? SKAlphaType.Opaque : SKAlphaType.Premul))
                            {
                                IntPtr length;
                                var    result = codec.GetPixels(bitmap.Info, bitmap.GetPixels(out length));
                                if (result == SKCodecResult.Success || result == SKCodecResult.IncompleteInput)
                                {
                                    var x = 25;
                                    var y = 25;

                                    canvas.DrawBitmap(bitmap, x, y);
                                    x += bitmap.Info.Width + 25;
                                    y += 14;

                                    canvas.DrawText(string.Format("Result: {0}", result), x, y, paint);
                                    y += 20;

                                    canvas.DrawText(string.Format("Size: {0}px x {1}px", bitmap.Width, bitmap.Height), x, y, paint);
                                    y += 20;

                                    canvas.DrawText(string.Format("Pixels: {0} @ {1}b/px", bitmap.Pixels.Length, bitmap.BytesPerPixel), x, y, paint);
                                    y += 20;

                                    canvas.DrawText(string.Format("Encoding: {0} ({1}) @ {2}-bit color", encodedInfo.Color, encodedInfo.Alpha, encodedInfo.BitsPerComponent), x, y, paint);
                                }
                            }
                        }
        }
Ejemplo n.º 7
0
        private static SKBitmap ZoomAndRotate(SKBitmap originalBitmap, double zoom, bool isRotated, float rotation, bool isKeystoned, float keystone)
        {
            var rotatedAndZoomed = new SKBitmap(originalBitmap.Width, originalBitmap.Height);

            using (var tempCanvas = new SKCanvas(rotatedAndZoomed))
            {
                var zoomedX      = originalBitmap.Width * zoom / -2f;
                var zoomedY      = originalBitmap.Height * zoom / -2f;
                var zoomedWidth  = originalBitmap.Width * (1 + zoom);
                var zoomedHeight = originalBitmap.Height * (1 + zoom);
                if (isRotated)
                {
                    tempCanvas.RotateDegrees(rotation, originalBitmap.Width / 2f,
                                             originalBitmap.Height / 2f);
                }
                tempCanvas.DrawBitmap(
                    originalBitmap,
                    SKRect.Create(
                        (float)zoomedX,
                        (float)zoomedY,
                        (float)zoomedWidth,
                        (float)zoomedHeight
                        )); // blows up the bitmap, which is cut off later
                if (isRotated)
                {
                    tempCanvas.RotateDegrees(rotation, -1 * originalBitmap.Width / 2f,
                                             originalBitmap.Height / 2f);
                }
            }

            SKBitmap keystoned = null;

            if (isKeystoned)
            {
                keystoned = new SKBitmap(originalBitmap.Width, originalBitmap.Height);
                using (var tempCanvas = new SKCanvas(keystoned))
                {
                    tempCanvas.SetMatrix(TaperTransform.Make(new SKSize(originalBitmap.Width, originalBitmap.Height),
                                                             keystone > 0 ? TaperSide.Left : TaperSide.Right, TaperCorner.Both, 1 - Math.Abs(keystone)));
                    tempCanvas.DrawBitmap(rotatedAndZoomed, 0, 0);
                    rotatedAndZoomed.Dispose();
                }
            }

            return(keystoned ?? rotatedAndZoomed);
        }
Ejemplo n.º 8
0
 internal static void DrawBitmap(this SKCanvas canvas, TouchManipulationBitmap bitmap, float transX = 0, float transY = 0, float scale = 1)
 {
     using (SKPaint paint = new SKPaint())
     {
         paint.IsAntialias = true;
         if (!bitmap.IsHide)
         {
             canvas.Save();
             SKMatrix matrix       = bitmap.Matrix;
             SKMatrix bitmapMatrix = new SKMatrix(matrix.ScaleX * scale, matrix.SkewX * scale, (matrix.TransX + transX) * scale, matrix.SkewY * scale, matrix.ScaleY * scale, (matrix.TransY + transY) * scale, 0, 0, 1);
             //canvas.Concat(ref bitmapMatrix);
             canvas.SetMatrix(bitmapMatrix);
             canvas.DrawBitmap(bitmap.Bitmap, 0, 0, paint);
             canvas.Restore();
         }
     }
 }
Ejemplo n.º 9
0
        // Draw "You are here" waypoint
        private static void drawWaypoint(SKCanvas canvas, Point origin, Point position)
        {
            // Compute draw position
            Assembly assembly     = typeof(MapViewer).GetTypeInfo().Assembly;
            Size     waypointSize = new Size(100, 100);
            Point    drawPosition = new Point(origin.X + position.X, origin.Y + position.Y);

            // Allocate memory
            using (Stream waypointStream = assembly.GetManifestResourceStream("Waypoint.res.img.waypoint.png"))
                using (var skStream = new SKManagedStream(waypointStream))
                    using (var bitmap = SKBitmap.Decode(skStream))
                    {
                        // Draw to canvas
                        canvas.DrawBitmap(bitmap, SKRect.Create((float)(drawPosition.X - (waypointSize.Width / 2.0)),
                                                                (float)(drawPosition.Y - waypointSize.Height), (float)waypointSize.Width, (float)waypointSize.Height));
                    }
        }
        private SKBitmap RemoveTransparency(SKImage orig)
        {
            var original = SKBitmap.FromImage(orig);

            // create a new bitmap with the same dimensions
            // also avoids the first copy if the color type is index8
            var copy = new SKBitmap(original.Width, original.Height);

            using var canvas = new SKCanvas(copy);
            // clear the bitmap with the desired color for transparency
            canvas.Clear(SKColors.White);

            // draw the bitmap on top
            canvas.DrawBitmap(original, 0, 0);

            return(copy);
        }
Ejemplo n.º 11
0
        private void Crop()
        {
            try
            {
                // create options and image crop
                var options = new Options(this.CropWidth, this.CropHeight)
                {
                    Debug = true
                };

                var crop = new ImageCrop(options);

                var watch = Stopwatch.StartNew();
                // load the source image
                using (var bitmap = SKBitmap.Decode(this.SourceImagePath))
                {
                    // calculate the best crop area
                    var result = crop.Crop(bitmap);
                    watch.Stop();

                    this.DebugImage = this.CreateImageSource(result.DebugInfo.Output);

                    watch.Start();

                    // crop the image
                    SKRect cropRect = new SKRect(result.Area.Left, result.Area.Top, result.Area.Right, result.Area.Bottom);
                    using (SKBitmap croppedBitmap = new SKBitmap((int)cropRect.Width, (int)cropRect.Height))
                        using (SKCanvas canvas = new SKCanvas(croppedBitmap))
                        {
                            SKRect source = new SKRect(cropRect.Left, cropRect.Top,
                                                       cropRect.Right, cropRect.Bottom);
                            SKRect dest = new SKRect(0, 0, cropRect.Width, cropRect.Height);
                            canvas.DrawBitmap(bitmap, source, dest);
                            watch.Stop();

                            this.CroppedImage = this.CreateImageSource(croppedBitmap);
                        };
                }

                this.ErrorText = null;
            }
            catch (Exception e)
            {
                this.ErrorText = e.Message;
            }
        }
Ejemplo n.º 12
0
        private static SKBitmap AutoOrient(SKBitmap bitmap, SKEncodedOrigin origin)
        {
            Stopwatch orient = new Stopwatch("SkiaSharpOrient");

            SKBitmap rotated;

            switch (origin)
            {
            case SKEncodedOrigin.BottomRight:
                rotated = new SKBitmap(bitmap.Height, bitmap.Width);
                using (var canvas = new SKCanvas(rotated))
                {
                    canvas.RotateDegrees(180, bitmap.Width / 2, bitmap.Height / 2);
                    canvas.DrawBitmap(bitmap, 0, 0);
                }
                break;

            case SKEncodedOrigin.RightTop:
                rotated = new SKBitmap(bitmap.Height, bitmap.Width);
                using (var canvas = new SKCanvas(rotated))
                {
                    canvas.Translate(rotated.Width, 0);
                    canvas.RotateDegrees(90);
                    canvas.DrawBitmap(bitmap, 0, 0);
                }
                break;

            case SKEncodedOrigin.LeftBottom:
                rotated = new SKBitmap(bitmap.Height, bitmap.Width);
                using (var canvas = new SKCanvas(rotated))
                {
                    canvas.Translate(0, rotated.Height);
                    canvas.RotateDegrees(270);
                    canvas.DrawBitmap(bitmap, 0, 0);
                }
                break;

            default:
                rotated = bitmap.Copy();
                break;
            }

            orient.Stop();

            return(rotated);
        }
Ejemplo n.º 13
0
 /// <summary>Draws the specified image ontop of this one.</summary>
 /// <param name="other">The image to draw.</param>
 /// <param name="x">The x position.</param>
 /// <param name="y">The y position.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="translation">The translation.</param>
 public void Draw(SKBitmap other, float x, float y, float width, float height, Translation?translation = null)
 {
     if (canvas == null)
     {
         canvas       = new SKCanvas(bitmap);
         canvasThread = Thread.CurrentThread;
     }
     else
     {
         if (Thread.CurrentThread != canvasThread)
         {
             throw new InvalidOperationException("Canvas Thread Boundary Incursion!");
         }
     }
     using (SKPaint paint = new SKPaint()
     {
         BlendMode = SKBlendMode.SrcOver, FilterQuality = SKFilterQuality.High,
     })
     {
         if (translation.HasValue)
         {
             float mx = Width / 2f;
             float my = Height / 2f;
             if (translation.Value.Rotation != 0)
             {
                 canvas.RotateRadians(translation.Value.Rotation, other.Width / 2, other.Height / 2);
             }
             if (translation.Value.FlipHorizontally)
             {
                 canvas.Scale(-1, 1);
                 canvas.Translate(-Width, 0);
             }
             if (translation.Value.FlipVertically)
             {
                 canvas.Scale(1, -1);
                 canvas.Translate(1, -Height);
             }
         }
         canvas.DrawBitmap(other, SKRect.Create(x, y, width, height), paint);
     }
     if (translation.HasValue)
     {
         canvas.ResetMatrix();
     }
 }
Ejemplo n.º 14
0
        /// <summary>
        /// 裁剪图片
        /// </summary>
        public static byte[] Crop(Stream imageStream, ImageSize cropSize, int quality = 100)
        {
            if (imageStream == null)
            {
                throw new ArgumentNullException(nameof(imageStream));
            }

            if (cropSize == null)
            {
                throw new ArgumentNullException(nameof(cropSize));
            }

            using (SKManagedStream managedStream = new SKManagedStream(imageStream))
            {
                using (SKBitmap bitmap = SKBitmap.Decode(managedStream))
                {
                    if (bitmap == null)
                    {
                        throw new ImageException("传递的参数不是有效图片");
                    }

                    ImageSize sourceSize = new ImageSize(bitmap.Width, bitmap.Height);
                    if (sourceSize.Height < cropSize.Height || sourceSize.Width < cropSize.Width)
                    {
                        throw new ImageException("源图片的尺寸不能小于裁剪尺寸");
                    }

                    using (SKSurface surface = SKSurface.Create(new SKImageInfo(cropSize.Width, cropSize.Height)))
                    {
                        SKCanvas canvas = surface.Canvas;
                        canvas.Clear(SKColors.White);
                        int deltaHeight = cropSize.Height - sourceSize.Height;
                        int deltaWidth  = cropSize.Width - sourceSize.Width;
                        canvas.DrawBitmap(bitmap, deltaWidth / 2, deltaHeight / 2);
                        using (SKImage image = surface.Snapshot())
                        {
                            using (SKData data = image.Encode(SKEncodedImageFormat.Jpeg, quality))
                            {
                                return(data.ToArray());
                            }
                        }
                    }
                }
            }
        }
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            // Find rectangle to display bitmap
            float scale = Math.Min((float)info.Width / bitmap.Width,
                                   (float)info.Height / bitmap.Height);

            SKRect rect = SKRect.Create(scale * bitmap.Width, scale * bitmap.Height);

            float x = (info.Width - rect.Width) / 2;
            float y = (info.Height - rect.Height) / 2;

            rect.Offset(x, y);

            // Display bitmap in rectangle
            canvas.DrawBitmap(bitmap, rect);

            // Adjust center and radius for scaled and offset bitmap
            SKPoint center = new SKPoint(scale * CENTER.X + x,
                                         scale * CENTER.Y + y);
            float radius = scale * RADIUS;

            using (SKPaint paint = new SKPaint())
            {
                paint.Shader = SKShader.CreateRadialGradient(
                    center,
                    radius,
                    new SKColor[] { SKColors.Black,
                                    SKColors.Transparent },
                    new float[] { 0.6f, 1 },
                    SKShaderTileMode.Clamp);

                paint.BlendMode = SKBlendMode.DstIn;

                // Display rectangle using that gradient and blend mode
                canvas.DrawRect(rect, paint);
            }

            canvas.DrawColor(SKColors.Pink, SKBlendMode.DstOver);
        }
Ejemplo n.º 16
0
 private void DrawingLoop(SKCanvas canvas)
 {
     canvas.Clear(SKColors.DarkBlue);
     canvas.DrawBitmap(
         backgroundBitmap,
         new SKRect(
             0,
             0,
             Settings.General.xAxisLength,
             Settings.General.yAxisLength
             )
         );
     player.Draw(canvas);
     enemyManager.DrawEnemies(canvas);
     projectileManager.DrawProjectiles(canvas);
     GraphicalUserInterface.DrawScore(canvas, enemyManager.score);
     GraphicalUserInterface.DrawFPS(canvas, (int)loopTimer.GetFPS());
 }
Ejemplo n.º 17
0
        /// <summary>
        /// Stacks the list of images vertically, stretching them to all have the same width
        /// Note that we stack them bottom-up (by reversing image order), because uv coordinate system has v=0 starting at the bottom
        /// </summary>
        /// <param name="images">The images to stack</param>
        /// <returns>The stacked images</returns>
        public static SKImage StackImages(List <SKImage> images)
        {
            images.Reverse();
            SKBitmap newBitmap = new SKBitmap(images.Max(i => i.Width), images.Select(i => i.Height).Aggregate((h1, h2) => h1 + h2));

            using (SKCanvas canvas = new SKCanvas(newBitmap))
            {
                canvas.Clear();
                var h = 0;
                foreach (var img in images)
                {
                    var bitmap = SKBitmap.FromImage(img).Resize(new SKSizeI(newBitmap.Width, img.Height), SKFilterQuality.High);
                    canvas.DrawBitmap(bitmap, new SKPoint(0, h));
                    h += img.Height;
                }
            }
            return(SKImage.FromBitmap(newBitmap));
        }
Ejemplo n.º 18
0
        public void DrawImage(Image img, long i, long i1, long width, long height)
        {
            var data = ((Bitmap)img).LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly,
                                              PixelFormat.Format32bppArgb);

            using (var skbmp = new SKBitmap(new SKImageInfo(img.Width, img.Height, SKColorType.Bgra8888)))
            {
                skbmp.SetPixels(data.Scan0);

                //skbmp.InstallPixels(new SKPixmap(skbmp.Info, data.Scan0));

                _image.DrawBitmap(skbmp, new SKRect(i, i1, i + width, i1 + height), _paint);
            }

            ((Bitmap)img).UnlockBits(data);

            data = null;
        }
Ejemplo n.º 19
0
        public static byte[] Recolor(byte[] data, int size)
        {
            var bitmap      = SKBitmap.Decode(data);
            var widthHeight = Math.Min(bitmap.Width, bitmap.Height);
            var scale       = (float)size / widthHeight;

            using (var resultBitmap = new SKBitmap(size, size))
                using (var canvas = new SKCanvas(resultBitmap))
                    using (var paint = new SKPaint())
                    {
                        var colorTable = Enumerable.Range(0, 256).Select(i => (byte)(0xC0 & i)).ToArray();
                        paint.ColorFilter = SKColorFilter.CreateTable(null, null, colorTable, colorTable);
                        canvas.Scale(scale, scale);
                        canvas.DrawBitmap(bitmap, (widthHeight - bitmap.Width) / 2, (widthHeight - bitmap.Height) / 2, paint);

                        return(ToPngData(resultBitmap));
                    }
        }
Ejemplo n.º 20
0
        public static void DrawChicFace(SKCanvas c, SKColor color, int x, int size = 128)
        {
            using (var path = SKPath.ParseSvgPathData(ChicData.ChicHeadPath))
                using (var b = new SKBitmap(320, 320))
                {
                    using (var ca = new SKCanvas(b))
                        using (var paint = new SKPaint {
                            FilterQuality = SKFilterQuality.High, IsAntialias = true, Color = color
                        })
                            ca.DrawPath(path, paint);

                    using (var shadow = SKImageFilter.CreateDropShadow(0, 0, 5, 5, SKColors.Black))
                        using (var paint = new SKPaint {
                            FilterQuality = SKFilterQuality.High, IsAntialias = true, ImageFilter = shadow
                        })
                            c.DrawBitmap(b, new SKRect(x, 0, size, size));
                }
        }
        // Bitmap transformations
        public static SKBitmap RotateBitmap(string path, int degrees)
        {
            SKBitmap rotatedBitmap;

            using (var bitmap = SKBitmap.Decode(path))
            {
                rotatedBitmap = new SKBitmap(bitmap.Height, bitmap.Width);

                using (var surface = new SKCanvas(rotatedBitmap))
                {
                    surface.Translate(rotatedBitmap.Width, 0);
                    surface.RotateDegrees(degrees);
                    surface.DrawBitmap(bitmap, 0, 0);
                }
            }

            return(rotatedBitmap);
        }
Ejemplo n.º 22
0
 public void Draw(SKCanvas canvas)
 {
     if (!exploded)
     {
         canvas.DrawBitmap(ChooseBitmap(), GetRect());
         GraphicalUserInterface.DrawHealthbar(
             canvas,
             pos.X,
             pos.Y,
             (float)(health / maxHealth)
             );
         animatorJetPack.Draw(canvas, GetRectJetPackFlame());
     }
     else
     {
         animatorExpl.Draw(canvas, GetRectExpl());
     }
 }
Ejemplo n.º 23
0
        private void DrawRevenge(SKCanvas canvas)
        {
            SKRect rect_Piece;
            SKRect tempRect;

            tempRect = MainGraphics !.GetActualRectangle(4, 5, 109, 27);
            var ThisPaint = MiscHelpers.GetTextPaint(SKColors.Aqua, MainGraphics.GetFontSize(19));

            ThisPaint.FakeBoldText = true;
            canvas.DrawCustomText("Vengeance", TextExtensions.EnumLayoutOptions.Start, TextExtensions.EnumLayoutOptions.Start, ThisPaint, tempRect, out _);
            tempRect = MainGraphics.GetActualRectangle(-1, 27, 109, 27);
            canvas.DrawCustomText("2500", TextExtensions.EnumLayoutOptions.Center, TextExtensions.EnumLayoutOptions.Start, ThisPaint, tempRect, out _);
            rect_Piece = MainGraphics.GetActualRectangle(6, 59, 95, 80);
            canvas.DrawRect(rect_Piece, _redFill);
            var ThisImage = ImageExtensions.GetSkBitmap(_thisAssembly !, "revenge.png");

            canvas.DrawBitmap(ThisImage, rect_Piece, MainGraphics.BitPaint);
        }
Ejemplo n.º 24
0
        void Display(SKCanvas canvas, int index, SKRect rect)
        {
            string text = String.Format("{0}: {1:F2} msec", descriptions[index],
                                        (double)elapsedTimes[index] / REPS);

            SKRect bounds = new SKRect();

            using (SKPaint textPaint = new SKPaint())
            {
                textPaint.TextSize  = (float)(12 * canvasView.CanvasSize.Width / canvasView.Width);
                textPaint.TextAlign = SKTextAlign.Center;
                textPaint.MeasureText("Tly", ref bounds);

                canvas.DrawText(text, new SKPoint(rect.MidX, rect.Bottom - bounds.Bottom), textPaint);
                rect.Bottom -= bounds.Height;
                canvas.DrawBitmap(bitmaps[index], rect, BitmapStretch.Uniform);
            }
        }
Ejemplo n.º 25
0
    private void DrawImageWatermark(IBitmapList bitmaps)
    {
        foreach (var bitmap in bitmaps)
        {
            bitmap.AppendSaveAsSuffix(Options.Watermark.FileNameSuffix);

            var realBitmap = (SKBitmap)bitmap.Source;
            using (var canvas = new SKCanvas(realBitmap))
            {
                var imageSize = new Size(realBitmap.Width, realBitmap.Height);
                var coord     = ImageHelper.CalculateCoord(imageSize,
                                                           Options.Watermark.InitialCoord, Options.Watermark.IsRandomCoord);

                // 绘制图像水印
                canvas.DrawBitmap(_watermarkBitmap, coord.X, coord.Y);
            }
        }
    }
Ejemplo n.º 26
0
        private unsafe void DrawPixels(PixelBuffer pixelBuffer, SKCanvas canvas, SKPaint paint, Rectangle r, int x, int y)
        {
            if (!Monitor.IsEntered(_syncRoot))
            {
                throw new InvalidOperationException();
            }

            r.Offset(-x, -y);

            Rectangle bitmapRect = new Rectangle(0, 0, pixelBuffer.Width, pixelBuffer.Height);

            r.Intersect(bitmapRect);
            if (r.Width == 0 || r.Height == 0)
            {
                return;
            }
            canvas.DrawBitmap(pixelBuffer.Source, new SKPoint(r.X + x, r.Y + y), paint);
        }
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            canvas.Clear(SKColors.White);

            var size  = width > height ? height : width;
            var small = size / 5;

            using (var stream = new SKManagedStream(SampleMedia.Images.Baboon))
                using (var bitmap = SKBitmap.Decode(stream))
                    using (var filterMag = SKImageFilter.CreateMagnifier(SKRect.Create(small * 2, small * 2, small * 3, small * 3), small))
                        using (var filterBlur = SKImageFilter.CreateBlur(5, 5, filterMag))
                            using (var paint = new SKPaint())
                            {
                                paint.ImageFilter = filterBlur;

                                canvas.DrawBitmap(bitmap, SKRect.Create(width, height), paint);
                            }
        }
Ejemplo n.º 28
0
        // Got the code from https://stackoverflow.com/a/45620498/1074470
        private static SKBitmap HandleOrientation(SKBitmap bitmap, SKCodecOrigin orientation)
        {
            SKBitmap rotated;

            switch (orientation)
            {
            case SKCodecOrigin.BottomRight:

                using (var surface = new SKCanvas(bitmap))
                {
                    surface.RotateDegrees(180, bitmap.Width / 2, bitmap.Height / 2);
                    surface.DrawBitmap(bitmap.Copy(), 0, 0);
                }

                return(bitmap);

            case SKCodecOrigin.RightTop:
                rotated = new SKBitmap(bitmap.Height, bitmap.Width);

                using (var surface = new SKCanvas(rotated))
                {
                    surface.Translate(rotated.Width, 0);
                    surface.RotateDegrees(90);
                    surface.DrawBitmap(bitmap, 0, 0);
                }

                return(rotated);

            case SKCodecOrigin.LeftBottom:
                rotated = new SKBitmap(bitmap.Height, bitmap.Width);

                using (var surface = new SKCanvas(rotated))
                {
                    surface.Translate(0, rotated.Height);
                    surface.RotateDegrees(270);
                    surface.DrawBitmap(bitmap, 0, 0);
                }

                return(rotated);

            default:
                return(bitmap);
            }
        }
Ejemplo n.º 29
0
        /*
         * Tela di disegno, disegna tutti gli elementi presenti nel Canvas
         */
        private void OnCanvasViewBitMapImgSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            display.CalculateDisplayVertexPixelCoordinate(info);

            display.AspectRatio = Math.Min((float)info.Width / bitMapArea.BitMap.Width,
                                           (float)info.Height / bitMapArea.BitMap.Height);

            bitMapArea.CalculateBitMapSize(info, display);

            rectangleBitMapImg = new SKRect(bitMapArea.Left, bitMapArea.Top, bitMapArea.Right, bitMapArea.Bottom);

            canvas.DrawBitmap(bitMapArea.BitMap, rectangleBitMapImg);

            DisplayChange(canvas);

            foreach (RectangleArea rectangleArea in allRectangleArea)
            {
                SKRect rectSelectionArea = new SKRect(rectangleArea.Left, rectangleArea.Top, rectangleArea.Right, rectangleArea.Bottom);

                if (rectangleArea.ResizeMove)
                {
                    paintResizeRectSelectionArea = new SKPaint
                    {
                        IsAntialias = true,
                        Style       = SKPaintStyle.Fill,
                        Color       = SKColors.LightSkyBlue,
                    };
                    canvas.DrawCircle(rectangleArea.Left, rectangleArea.Top, rectangleArea.RadiousOfCircleRect, paintResizeRectSelectionArea);
                    canvas.DrawCircle(rectangleArea.Left, rectangleArea.Bottom, rectangleArea.RadiousOfCircleRect, paintResizeRectSelectionArea);
                    canvas.DrawCircle(rectangleArea.Right, rectangleArea.Top, rectangleArea.RadiousOfCircleRect, paintResizeRectSelectionArea);
                    canvas.DrawCircle(rectangleArea.Right, rectangleArea.Bottom, rectangleArea.RadiousOfCircleRect, paintResizeRectSelectionArea);
                    rectangleArea.ResizeMove = false;
                }
                rectangleArea.CalculateNewPositionIDRect();
                canvas.DrawRect(rectSelectionArea, rectangleArea.PaintRect);
                canvas.DrawText(rectangleArea.Id, rectangleArea.PositionIDRect.X, rectangleArea.PositionIDRect.Y, rectangleArea.PaintIDRect);
            }
        }
Ejemplo n.º 30
0
        internal static SKBitmap GetBlurBitmap(SKBitmap bitmap, SKRect rect)
        {
            SKBitmap outBitmap = new SKBitmap((int)rect.Width, (int)rect.Height);

            using (SKBitmap tempBitmap = new SKBitmap(CalcBackgraundBitmapsize(bitmap.Width), CalcBackgraundBitmapsize(bitmap.Height)))
                using (SKCanvas canvas = new SKCanvas(outBitmap))
                    using (SKPaint paint = new SKPaint())
                    {
                        bitmap.ScalePixels(tempBitmap, SKFilterQuality.Low);
                        paint.IsAntialias = true;
                        float blur = 0.08f * Math.Max(rect.Width, rect.Height);
                        blur = blur < 100 ? blur : 100;
                        paint.ImageFilter = SKImageFilter.CreateBlur(blur, blur);
                        canvas.Clear();
                        canvas.DrawBitmap(tempBitmap, rect, paint);
                    }
            GC.Collect(0);
            return(outBitmap);
        }