コード例 #1
0
        public static bool TryDrawWallOrFloor(this string tile, string background, SKBitmap wall, SKBitmap floor, string[] wallAndFloorColors, out SKBitmap tileToDraw)
        {
            var highlighted = FixHighlight(tile, background, out var correctTile);

            tileToDraw = new SKBitmap(32, 32);

            using (SKCanvas g = new SKCanvas(tileToDraw))
            {
                if (correctTile == "#BLUE")
                {
                    g.DrawBitmap(wall, new SKRect(0, 0, wall.Width, wall.Height));
                    g.DrawColor(new SKColor(0, 0, 0, 150), SKBlendMode.Overlay);
                    return(true);
                }

                if (correctTile[0] == '#' && correctTile.Substring(1).Equals(wallAndFloorColors[0]))
                {
                    g.DrawBitmap(wall, new SKRect(0, 0, wall.Width, wall.Height));
                    return(true);
                }

                if (correctTile == ".BLUE")
                {
                    g.DrawBitmap(floor, new SKRect(0, 0, floor.Width, floor.Height));
                    g.DrawColor(new SKColor(0, 0, 0, 150), SKBlendMode.Overlay);
                    return(true);
                }

                if (correctTile[0] == '.' && correctTile.Substring(1).Equals(wallAndFloorColors[1]))
                {
                    g.DrawBitmap(floor, new SKRect(0, 0, floor.Width, floor.Height));
                    return(true);
                }

                if (correctTile == "*BLUE")
                {
                    g.DrawBitmap(wall, new SKRect(0, 0, wall.Width, wall.Height));
                    g.DrawColor(new SKColor(40, 30, 30, 200), SKBlendMode.Overlay);
                    return(true);
                }
                if (correctTile == ",BLUE")
                {
                    g.DrawBitmap(floor, new SKRect(0, 0, floor.Width, floor.Height));
                    g.DrawColor(new SKColor(40, 20, 20, 200), SKBlendMode.Overlay);
                    return(true);
                }
            }

            return(false);
        }
コード例 #2
0
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            // stops at 0x530 on Android
            string text = "\u03A3 and \u0750";

            using (var paint = new SKPaint())
                using (var tf = SKTypeface.FromFamilyName("Tahoma"))
                {
                    canvas.DrawColor(SKColors.White);

                    paint.IsAntialias = true;
                    paint.TextSize    = 60;
                    paint.Typeface    = tf;
                    canvas.DrawText(text, 50, 100, paint);
                }

            using (var paint = new SKPaint())
                using (var tf = SKTypeface.FromFamilyName("Times New Roman"))
                {
                    paint.Color = SampleMedia.Colors.XamarinDarkBlue;

                    paint.IsAntialias = true;
                    paint.TextSize    = 60;
                    paint.Typeface    = tf;
                    canvas.DrawText(text, 50, 200, paint);
                }
        }
コード例 #3
0
        private void OnPaintSurface(SKCanvas canvas, int width, int height)
        {
            var scaledSize = new SKSize(width / _displayScale, height / _displayScale);

            // handle the device screen density
            canvas.Scale(_displayScale);

            var canvasWidth  = scaledSize.Width;  //width;
            var canvasHeight = scaledSize.Height; //height;

            using (var paint = new SKPaint())
            {
                // clear the canvas / fill with white
                canvas.DrawColor(SKColors.White);

                //var RulerWidth = 50;
                //var LargeSteps = 50;
                //var SmallSteps = 10;
                //var drawHorizontalRuler = true;
                //if (drawHorizontalRuler)
                //{
                //    DrawHorizontalRuler(canvas, canvasWidth, RulerWidth, LargeSteps, SmallSteps);
                //}

                //DrawGrid(canvas, canvasWidth, canvasHeight, paint);

                foreach (var layer in _layers)
                {
                    layer.RenderItem.Paint(canvas);
                }
            }
        }
コード例 #4
0
        public async ValueTask <byte[]> GetBytesWithFilterAsync(
            int size,
            string topic = null,
            SKImageFilter imageFilter = null,
            SKColorFilter colorFilter = null,
            SKShader shader           = null,
            SKBlendMode blendMode     = SKBlendMode.Overlay)
        {
            Trace.WriteLine("Start");
            string url;

            if (string.IsNullOrEmpty(topic))
            {
                url = string.Format(URL_PATTERN_RND, size);
            }
            else
            {
                url = string.Format(URL_PATTERN, size, topic);
            }

            var imageInfo = new SKImageInfo(size, size);

            using (var http = new HttpClient())
            {
                var image = await http.GetByteArrayAsync(url);//.ConfigureAwait(false);

                Trace.WriteLine("Downloaded");
                using (var outStream = new MemoryStream())
                    using (var bitmap = SKBitmap.Decode(image, imageInfo))
                        using (var surface = SKSurface.Create(imageInfo))
                            using (var paint = new SKPaint())
                            {
                                SKCanvas canvas = surface.Canvas;
                                canvas.DrawColor(SKColors.White);
                                if (imageFilter != null)
                                {
                                    paint.ImageFilter = imageFilter;
                                }
                                if (colorFilter != null)
                                {
                                    paint.ColorFilter = colorFilter;
                                }

                                // draw the bitmap through the filter
                                var rect = SKRect.Create(imageInfo.Size);
                                canvas.DrawBitmap(bitmap, rect, paint);
                                if (shader != null)
                                {
                                    paint.Shader    = shader;
                                    paint.BlendMode = blendMode;
                                    canvas.DrawPaint(paint);
                                }
                                SKData data = surface.Snapshot().Encode(SKEncodedImageFormat.Jpeg, 80);
                                data.SaveTo(outStream);
                                byte[] manipedImage = outStream.ToArray();
                                Trace.WriteLine("End");
                                return(manipedImage);
                            }
            }
        }
コード例 #5
0
        /// <summary>
        /// 将生成的字符串写入图像文件
        /// </summary>
        /// <param name="code">验证码字符串</param>
        /// <param name="length">生成位数(默认4位)</param>
        public static byte[] Create(out string code, int length = 4)
        {
            code = RandomCode(length);

            byte[] imageBytes;
            int    imageX = 85;
            int    imageY = 32;


            using (SKBitmap image = new SKBitmap(imageX, imageY, SKColorType.Bgra8888, SKAlphaType.Premul))
            {
                using (SKCanvas canvas = new SKCanvas(image))
                {
                    canvas.DrawColor(SKColors.Gray);
                    using (SKPaint drawStyle = CreatePaint())
                    {
                        canvas.DrawText(code, 10, imageY - 10, drawStyle);
                    }

                    using (SKImage img = SKImage.FromBitmap(image))
                    {
                        using (SKData p = img.Encode(SKEncodedImageFormat.Png, 100))
                        {
                            imageBytes = p.ToArray();
                        }
                    }
                }
            }

            return(imageBytes);
        }
コード例 #6
0
        // This code uses System.Drawing to merge images and render text on the image
        // System.Drawing SHOULD NOT be used in a production application
        // It is not supported in server scenarios and is used here as a demo only!
        public static void MergeCardImage(string cardPath, byte[] imageBytes, Stream outputStream, string personName, string personTitle, double score)
        {
            using (MemoryStream faceImageStream = new MemoryStream(imageBytes)) {
                using (var surface = SKSurface.Create(width: CardWidth, height: CardHeight, colorType: SKImageInfo.PlatformColorType, alphaType: SKAlphaType.Premul)) {
                    SKCanvas canvas = surface.Canvas;

                    canvas.DrawColor(SKColors.White); // clear the canvas / fill with white

                    using (var fileStream = File.OpenRead(cardPath))
                        using (var stream = new SKManagedStream(fileStream)) // decode the bitmap from the stream
                            using (var cardBack = SKBitmap.Decode(stream))
                                using (var face = SKBitmap.Decode(imageBytes))
                                    using (var paint = new SKPaint()) {
                                        canvas.DrawBitmap(cardBack, SKRect.Create(0, 0, CardWidth, CardHeight), paint);
                                        canvas.DrawBitmap(face, SKRect.Create(TopLeftFaceX, TopLeftFaceY, FaceWidth, FaceWidth));

                                        RenderText(canvas, NameFontSize, NameTextX, NameTextY, NameWidth, personName);
                                        RenderText(canvas, TitleFontSize, NameTextX, TitleTextY, NameWidth, personTitle);
                                        RenderScore(canvas, ScoreX, ScoreY, ScoreWidth, score.ToString());

                                        canvas.Flush();

                                        using (var jpgImage = surface.Snapshot().Encode(SKEncodedImageFormat.Jpeg, 80)) {
                                            jpgImage.SaveTo(outputStream);
                                        }
                                    }
                }
            }
        }
コード例 #7
0
ファイル: BaseDecayIcon.cs プロジェクト: NanosaysHi/FModel
        private new void DrawBackground(SKCanvas c)
        {
            c.DrawRect(new SKRect(Margin, Margin, Width - Margin, Height - Margin),
                       new SKPaint
            {
                IsAntialias = true, FilterQuality = SKFilterQuality.High,
                Shader      = SKShader.CreateRadialGradient(new SKPoint(Width / 2, Height / 2), Width / 5 * 2,
                                                            new[] { Background[0], Background[1] },
                                                            SKShaderTileMode.Clamp)
            });

            for (var i = 0; i < _backgroundOverlay.Width; i++)
            {
                for (var j = 0; j < _backgroundOverlay.Height; j++)
                {
                    if (_backgroundOverlay.GetPixel(i, j) == SKColors.Black)
                    {
                        _backgroundOverlay.SetPixel(i, j, SKColors.Transparent);
                    }
                }
            }

            c.DrawBitmap(_backgroundOverlay, new SKRect(Margin, Margin, Width - Margin, Height - Margin), new SKPaint
            {
                IsAntialias = true,
                ColorFilter = SKColorFilter.CreateBlendMode(SKColors.Black.WithAlpha(150), SKBlendMode.DstIn),
                ImageFilter = SKImageFilter.CreateDropShadow(2, 2, 4, 4, new SKColor(0, 0, 0))
            });
            c.DrawColor(SKColors.Black.WithAlpha(125), SKBlendMode.DstIn);
        }
コード例 #8
0
        private void surface_PaintSurface(object sender, SkiaSharp.Views.Desktop.SKPaintSurfaceEventArgs e)
        {
            if (!enabled)
            {
                return;
            }
            SKCanvas canvas = e.Surface.Canvas;

            canvas.Clear();
            float width  = e.Info.Width;
            float height = e.Info.Height;

            // Draw axis
            canvas.DrawColor(SKColors.LightGray);
            canvas.DrawLine(0, 0, 0, height, black);
            canvas.DrawLine(0, height - 1.5f, width, height - 1.5f, black);
            canvas.DrawText("X2", 28.0f, 35.0f, black);
            canvas.DrawText("X1", width - 45.0f, height - 35.0f, black);


            // Draw dots
            foreach (SKPoint pt in red_points)
            {
                canvas.DrawCircle(pt, 5, red);
            }

            foreach (SKPoint pt in blue_points)
            {
                canvas.DrawCircle(pt, 5, blue);
            }
        }
コード例 #9
0
    private void DrawCore(string captcha, Action <SKData> postAction)
    {
        var sizeAndPoints = ComputeSizeAndPoints(captcha);
        var imageSize     = sizeAndPoints.Size;
        var imageInfo     = new SKImageInfo(imageSize.Width, imageSize.Height,
                                            SKColorType.Bgra8888, SKAlphaType.Premul);

        using (var bmp = new SKBitmap(imageInfo))
            using (var canvas = new SKCanvas(bmp))
            {
                // Clear
                canvas.DrawColor(_backgroundColor);

                // 绘制噪点
                var points = _options.Captcha.BackgroundNoise.CreatePoints(imageSize);
                canvas.DrawPoints(SKPointMode.Points, points, _noisePaint);

                // 绘制验证码
                foreach (var p in sizeAndPoints.Points)
                {
                    var i         = p.Key;
                    var character = p.Value.Key;
                    var point     = p.Value.Value;

                    canvas.DrawText(character, point.X, point.Y,
                                    i % 2 > 0 ? _alternPaint : _forePaint);
                }

                using (var img = SKImage.FromBitmap(bmp))
                    using (var data = img.Encode(_imageFormat, _options.EncodeQuality))
                    {
                        postAction(data);
                    }
            }
    }
コード例 #10
0
 private static byte[] GenerateImage()
 {
     using (SKBitmap bitmap = new SKBitmap(width: 200, height: 200))
     {
         using (SKCanvas canvas = new SKCanvas(bitmap))
         {
             var random = new Random();
             canvas.DrawColor(SKColor.FromHsl(random.Next(0, 360), 100, 50));
             using (var paint = new SKPaint()
             {
                 TextSize = 25.0f,
                 IsAntialias = true,
                 Color = new SKColor(0, 0, 0),
                 Style = SKPaintStyle.Fill,
                 TextAlign = SKTextAlign.Center
             })
             {
                 canvas.DrawText(DateTime.Now.ToString("T"), 100, 100, paint);
             }
             using (SKData data = SKImage.FromBitmap(bitmap).Encode(SKEncodedImageFormat.Png, 100))
             {
                 return(data.ToArray());
             }
         }
     }
 }
コード例 #11
0
        public static void CreateLandscapeImage(string input, string output)
        {
            using (var bitmap = SKBitmap.Decode(input))
            {
                float newWidth  = (float)666 / (float)bitmap.Width;
                float newHeight = (float)370 / (float)bitmap.Height;
                float scale     = Math.Min(newHeight, newWidth);

                using (var scaledBitmap = bitmap.Resize(new SKImageInfo((int)(bitmap.Width * scale), (int)(bitmap.Height * scale)), SKBitmapResizeMethod.Lanczos3))
                {
                    var toBitmap = new SKBitmap(676, 380);
                    var canvas   = new SKCanvas(toBitmap);

                    var x = (676 - scaledBitmap.Width) / 2;
                    var y = (380 - scaledBitmap.Height) / 2;

                    canvas.DrawColor(SKColors.White);
                    canvas.DrawBitmap(scaledBitmap, x, y);
                    canvas.Flush();

                    using (var image = SKImage.FromBitmap(toBitmap))
                    {
                        using (var png = image.Encode(SKEncodedImageFormat.Png, 100))
                        {
                            using (var filestream = File.OpenWrite(output))
                            {
                                png.SaveTo(filestream);
                            }
                        }
                    }
                }
            }
        }
コード例 #12
0
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            canvas.DrawColor(SKColors.White);

            var rect = SKRect.Create(width / 3, height / 3, width / 3, height / 3);

            float    occluderHeight = 2.0f;
            SKPoint3 lightPos       = new SKPoint3(0, 0, 50);
            float    lightWidth     = 500;
            float    ambientAlpha   = 0.75f;
            float    spotAlpha      = 0.75f;

            using (var paint = new SKPaint())
                using (var filter = SKMaskFilter.CreateShadow(occluderHeight, lightPos, lightWidth, ambientAlpha, spotAlpha))
                {
                    paint.IsAntialias = true;
                    paint.Color       = SKColors.Black;
                    paint.MaskFilter  = filter;

                    // draw the shadow
                    canvas.DrawRect(rect, paint);

                    paint.Color      = SKColors.DarkBlue;
                    paint.MaskFilter = null;

                    // draw the rectangle
                    canvas.DrawRect(rect, paint);
                }
        }
コード例 #13
0
 public static void Draw(this SKPicture skPicture, SKColor background, float scaleX, float scaleY, SKCanvas skCanvas)
 {
     skCanvas.DrawColor(background);
     skCanvas.Save();
     skCanvas.Scale(scaleX, scaleY);
     skCanvas.DrawPicture(skPicture);
     skCanvas.Restore();
 }
コード例 #14
0
        /// <summary>
        /// Draws the provided path in filled mode with the provided color and alpha.
        /// Special thanks to Angelo Suzuki (https://github.com/tinsukE) for this.
        protected void DrawFilledPath(SKCanvas c, SKPath filledPath, SKColor fillColor, byte fillAlpha)
        {
            int save = c.Save();

            c.ClipPath(filledPath);
            c.DrawColor(fillColor.WithAlpha(fillAlpha), SKBlendMode.SrcOver);
            c.RestoreToCount(save);
        }
コード例 #15
0
        public static SKBitmap ApplyMask(this SKBitmap bitmap, SKColor color)
        {
            var skCanvas = new SKCanvas(bitmap);

            skCanvas.DrawColor(color, SKBlendMode.SrcIn);
            skCanvas.Flush();
            skCanvas.Save();
            return(bitmap);
        }
コード例 #16
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static byte[] GetCode(out string code)
        {
            #region 反射SK支持的全部颜色
            //List<SKColor> colors = new List<SKColor>();
            //var skcolors = new SKColors();
            //var type = skcolors.GetType();
            //foreach (FieldInfo field in type.GetFields())
            //{
            //    colors.Add( (SKColor)field.GetValue(skcolors));
            //}
            #endregion

            //int maxcolorindex = colors.Count-1;
            byte[] imageBytes = null;
            code = GenerateRandomNumber(4, DateTime.Now.Second);
            var      zu  = code.ToList();
            SKBitmap bmp = new SKBitmap(80, 30);
            using (SKCanvas canvas = new SKCanvas(bmp))
            {
                //背景色
                canvas.DrawColor(SKColors.White);

                using (SKPaint sKPaint = new SKPaint())
                {
                    sKPaint.TextSize    = 16;                                                      //字体大小
                    sKPaint.IsAntialias = true;                                                    //开启抗锯齿
                    sKPaint.Typeface    = SKTypeface.FromFamilyName("微软雅黑", SKTypefaceStyle.Bold); //字体
                    SKRect size = new SKRect();
                    sKPaint.MeasureText(zu[0].ToString(), ref size);                               //计算文字宽度以及高度

                    float  temp   = (bmp.Width / 4 - size.Size.Width) / 2;
                    float  temp1  = bmp.Height - (bmp.Height - size.Size.Height) / 2;
                    Random random = new Random();

                    for (int i = 0; i < 4; i++)
                    {
                        sKPaint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));
                        canvas.DrawText(zu[i].ToString(), temp + 20 * i, temp1, sKPaint);//画文字
                    }
                    //干扰线
                    for (int i = 0; i < 5; i++)
                    {
                        sKPaint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));
                        canvas.DrawLine(random.Next(0, 40), random.Next(1, 29), random.Next(41, 80), random.Next(1, 29), sKPaint);
                    }
                }
                //页面展示图片
                using (SKImage img = SKImage.FromBitmap(bmp))
                {
                    using (SKData p = img.Encode())
                    {
                        imageBytes = p.ToArray();
                    }
                }
                return(imageBytes);
            }
        }
コード例 #17
0
        public IActionResult ValidateCode()
        {
            #region 反射SK支持的全部颜色
            //List<SKColor> colors = new List<SKColor>();
            //var skcolors = new SKColors();
            //var type = skcolors.GetType();
            //foreach (FieldInfo field in type.GetFields())
            //{
            //    colors.Add( (SKColor)field.GetValue(skcolors));
            //}
            #endregion

            //int maxcolorindex = colors.Count-1;
            string   text = Bucket.Utility.Helpers.Randoms.CreateRandomValue(5, false);
            var      zu   = text.ToList();
            SKBitmap bmp  = new SKBitmap(100, 50);
            using (SKCanvas canvas = new SKCanvas(bmp))
            {
                //背景色
                canvas.DrawColor(SKColors.White);
                using (SKPaint sKPaint = new SKPaint())
                {
                    sKPaint.TextSize    = 24;                                                       //字体大小
                    sKPaint.IsAntialias = true;                                                     //开启抗锯齿
                    sKPaint.Typeface    = SKTypeface.FromFamilyName("Arial", SKTypefaceStyle.Bold); //字体

                    SKRect size = new SKRect();
                    sKPaint.MeasureText(zu[0].ToString(), ref size); //计算文字宽度以及高度

                    float  temp   = (bmp.Width / 4 - size.Size.Width) / 2;
                    float  temp1  = bmp.Height - (bmp.Height - size.Size.Height) / 2;
                    Random random = new Random();

                    for (int i = 0; i < zu.Count; i++)
                    {
                        sKPaint.Color     = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));
                        sKPaint.TextSkewX = (float)random.Next(0, 2);
                        canvas.DrawText(zu[i].ToString(), temp + 20 * i, temp1 + random.Next(-5, 5), sKPaint);//画文字
                    }
                    //干扰线
                    for (int i = 0; i < 5; i++)
                    {
                        sKPaint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));
                        canvas.DrawLine(random.Next(0, 40), random.Next(1, 29), random.Next(41, 80), random.Next(1, 29), sKPaint);
                    }
                }
                //页面展示图片
                using (SKImage img = SKImage.FromBitmap(bmp))
                {
                    using (SKData p = img.Encode())
                    {
                        return(File(p.ToArray(), "image/Png"));
                    }
                }
            }
        }
コード例 #18
0
 public static void bar()
 {
     // SKImageInfo nfo = new SKImageInfo();
     // SKBitmap bmp = new SKBitmap(300, 300, SKColorType.Rgba8888, SKAlphaType.Opaque);
     SKBitmap bmp = new SKBitmap(300, 300, SKColorType.Bgra8888, SKAlphaType.Premul);
     using (SKCanvas canvas = new SKCanvas(bmp))
     {
         canvas.DrawColor(SKColors.White); // Clear 
     }
 }
コード例 #19
0
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns>返回生成图片的二进制数据</returns>
        public override byte[] CreateCode()
        {
            byte[] codeImageBinary = new byte[0];

            #region 获取验证码的值

            string codeString = CreateCodeString();//创建一个新的验证码值
            if (string.IsNullOrWhiteSpace(codeString))
            {
                return(codeImageBinary);
            }
            this.code = codeString;

            #endregion

            #region 生成图片

            int width  = (this.fontSize + spaceBetween) * this.length + 5;
            int height = this.fontSize + 11;
            using (SKBitmap bitmap = new SKBitmap(width, height))
            {
                using (SKCanvas canvas = new SKCanvas(bitmap))
                {
                    canvas.Clear();
                    canvas.DrawColor(new SKColor(backgroundColor.R, backgroundColor.G, backgroundColor.B, backgroundColor.A));
                    using (var paint = new SKPaint())
                    {
                        for (int n = 0; n < this.length; n++)
                        {
                            paint.Color    = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));
                            paint.TextSize = fontSize;
                            canvas.DrawText(codeString[n].ToString(), n * fontSize + spaceBetween, bitmap.Height - (bitmap.Height - fontSize) / 2, paint);
                        }
                        //干扰线
                        for (int i = 0; i < 5; i++)
                        {
                            paint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));
                            canvas.DrawLine(random.Next(0, 40), random.Next(1, 29), random.Next(41, 80), random.Next(1, 29), paint);
                        }
                    }
                    using (var image = SKImage.FromBitmap(bitmap))
                    {
                        using (var skdata = image.Encode(SKEncodedImageFormat.Png, 100))
                        {
                            codeImageBinary = skdata.ToArray();
                        }
                    }
                }
            }

            #endregion

            return(codeImageBinary);
        }
コード例 #20
0
 private void DrawPathCContent(SKCanvas canvas)
 {
     canvas.Save();
     GetPathA((float)Width, (float)Height);
     _region.SetRect(ToSKRectI(canvas.LocalClipBounds));
     ClipRegion(_region, _pathA);
     ClipRegion(_region, GetPathC(), SKRegionOperation.ReverseDifference);
     canvas.ClipRegion(_region);
     canvas.DrawColor(SKColors.Green);
     canvas.Restore();
 }
コード例 #21
0
ファイル: CaptchaService.cs プロジェクト: librame/extensions
        public void DrawCore(string captcha, Action <SKData> postAction)
        {
            Logger.LogInformation($"Captcha text: {captcha}");

            var colors = Options.Captcha.Colors;

            var sizeAndPoints = ComputeSizeAndPoints(captcha);
            var imageSize     = sizeAndPoints.Size;
            var imageInfo     = new SKImageInfo(imageSize.Width, imageSize.Height,
                                                SKColorType.Bgra8888, SKAlphaType.Premul);

            using (var bmp = new SKBitmap(imageInfo))
                using (var canvas = new SKCanvas(bmp))
                {
                    // Clear
                    canvas.DrawColor(colors.Background);

                    // 绘制噪点
                    using (var noisePaint = CreateNoisePaint())
                    {
                        var points = CreateNoisePoints(imageSize);
                        canvas.DrawPoints(SKPointMode.Points, points, noisePaint);
                    }

                    // 绘制验证码
                    using (var forePaint = CreatePaint(colors.Fore))
                        using (var alternPaint = CreatePaint(colors.Alternate))
                        {
                            foreach (var p in sizeAndPoints.Points)
                            {
                                var i         = p.Key;
                                var character = p.Value.Key;
                                var point     = p.Value.Value;

                                canvas.DrawText(character, point.X, point.Y,
                                                i % 2 > 0 ? alternPaint : forePaint);
                            }
                        }

                    using (var img = SKImage.FromBitmap(bmp))
                        using (var data = img.Encode(CurrentImageFormat, Options.Quality))
                        {
                            if (data.IsNull())
                            {
                                throw new InvalidOperationException(InternalResource.InvalidOperationExceptionUnsupportedImageFormat);
                            }

                            postAction.Invoke(data);
                        }
                }
        }
コード例 #22
0
        public static void DrawSample(SKCanvas canvas, int width, int height)
        {
            canvas.DrawColor(SKColors.White);

            using (var paint = new SKPaint())
            {
                paint.TextSize    = 64.0f;
                paint.IsAntialias = true;
                paint.Color       = (SKColor)0xFF4281A4;
                paint.IsStroke    = false;

                canvas.DrawText("SkiaSharp", width / 2f, height / 2f, paint);
            }
        }
コード例 #23
0
        public static byte[] CreateByteByImgVerifyCode(string verifyCode, int width, int height)
        {
            byte[]   bytes;
            var      text = verifyCode.ToUpper().ToList();
            SKBitmap bmp  = new SKBitmap(width, height);

            using (SKCanvas canvas = new SKCanvas(bmp))
            {
                // 背景色
                canvas.DrawColor(SKColors.White);

                using (SKPaint sKPaint = new SKPaint())
                {
                    sKPaint.TextSize     = 18;                                                                       // 字体大小
                    sKPaint.FakeBoldText = true;
                    sKPaint.IsAntialias  = true;                                                                     // 开启抗锯齿
                    sKPaint.Typeface     = SKTypeface.FromFamilyName("WenQuanYi Micro Hei", SKTypefaceStyle.Normal); //字体

                    SKRect size = new SKRect();
                    sKPaint.MeasureText(text[0].ToString(), ref size); // 计算文字宽度以及高度

                    float _x  = (width - size.Width * text.Count) / 2 - size.Width;
                    float _y  = size.Height;
                    int   num = Next(0, 9);
                    sKPaint.Color = colors[num];
                    // 干扰线
                    for (int i = 0; i < 3; i++)
                    {
                        canvas.DrawLine(Next(0, 40), Next(1, 29), Next(41, 80), Next(1, 29), sKPaint);
                    }
                    // 文字
                    for (int i = 0; i < text.Count; i++)
                    {
                        _x += size.Width + Next(0, 3);
                        _y  = size.Height + Next(5, 15);
                        canvas.DrawText(text[i].ToString(), _x, _y, sKPaint);  // 画文字
                    }
                }
                // 页面展示图片
                using (SKImage img = SKImage.FromBitmap(bmp))
                {
                    using (SKData p = img.Encode())
                    {
                        bytes = p.ToArray();
                    }
                }
            }
            return(bytes);
        }
コード例 #24
0
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            canvas.DrawColor(SKColors.White);

            using (var paint = new SKPaint())
                using (var filter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 5.0f))
                {
                    paint.IsAntialias = true;
                    paint.TextSize    = 120;
                    paint.TextAlign   = SKTextAlign.Center;
                    paint.MaskFilter  = filter;

                    canvas.DrawText("SkiaSharp", width / 2f, height / 2f, paint);
                }
        }
コード例 #25
0
        public static byte[] CreateCaptcha(string code)
        {
            var charList = code.ToList();

            var bmp = new SKBitmap((charList.Count() * 20), 30);

            using (var canvas = new SKCanvas(bmp))
            {
                //背景色

                canvas.DrawColor(SKColors.White);

                using (SKPaint sKPaint = new SKPaint())
                {
                    sKPaint.TextSize    = 16;   //字体大小
                    sKPaint.IsAntialias = true; //开启抗锯齿
                    sKPaint.Typeface    = SKTypeface.FromFamilyName("微软雅黑");
                    var size = new SKRect();
                    sKPaint.MeasureText(charList[0].ToString(), ref size);
                    var temp   = (bmp.Width / 4 - size.Size.Width) / 2;
                    var temp1  = bmp.Height - (bmp.Height - size.Size.Height) / 2;
                    var random = new Random();
                    //画文字
                    for (int i = 0; i < charList.Count; i++)
                    {
                        sKPaint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255),
                                                    (byte)random.Next(0, 255));
                        canvas.DrawText(charList[i].ToString(), temp + 20 * i, temp1, sKPaint);
                    }

                    //画干扰线
                    for (int i = 0; i < 3; i++)
                    {
                        sKPaint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255),
                                                    (byte)random.Next(0, 255));
                        canvas.DrawLine(random.Next(0, 40), random.Next(1, 29), random.Next(41, 80), random.Next(1, 29),
                                        sKPaint);
                    }
                }
            }
            using (SKImage img = SKImage.FromBitmap(bmp))
            {
                using (SKData p = img.Encode())
                {
                    return(p.ToArray());
                }
            }
        }
コード例 #26
0
 private void Draw(SKCanvas canvas)
 {
     canvas.DrawColor(new SKColor(255, 255, 255));
     foreach (var figure in _completedInOuts.Concat(_editingInOuts))
     {
         DrawInOut(canvas, figure);
     }
     foreach (var figure in _completedSquares.Concat(_editingSquares))
     {
         DrawSquare(canvas, figure);
     }
     foreach (var figure in _completedEllipses.Concat(_editingEllipses))
     {
         DrawEllipse(canvas, figure);
     }
 }
コード例 #27
0
        public static void CreatePosterImage(string input, string output)
        {
            try
            {
                using (var bitmap = SKBitmap.Decode(input))
                {
                    if (bitmap != null)
                    {
                        float newWidth  = (float)530 / (float)bitmap.Width;
                        float newHeight = (float)800 / (float)bitmap.Height;
                        float scale     = Math.Min(newHeight, newWidth);

                        using (var scaledBitmap = bitmap.Resize(new SKImageInfo((int)(bitmap.Width * scale), (int)(bitmap.Height * scale)), SKBitmapResizeMethod.Lanczos3))
                        {
                            if (scaledBitmap != null)
                            {
                                var toBitmap = new SKBitmap(540, 810);
                                var canvas   = new SKCanvas(toBitmap);

                                var x = (540 - scaledBitmap.Width) / 2;
                                var y = (810 - scaledBitmap.Height) / 2;

                                canvas.DrawColor(SKColors.White);
                                canvas.DrawBitmap(scaledBitmap, x, y);
                                canvas.Flush();

                                using (var image = SKImage.FromBitmap(toBitmap))
                                {
                                    using (var png = image.Encode(SKEncodedImageFormat.Png, 100))
                                    {
                                        using (var filestream = File.OpenWrite(output))
                                        {
                                            png.SaveTo(filestream);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                Plugin.Logger.Info("IMAGEHELPER > Could not create Poster Image: {0}", output);
            }
        }
コード例 #28
0
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            canvas.DrawColor(SKColors.White);

            using var tf          = SKFontManager.Default.MatchCharacter('م') ?? SKTypeface.FromStream(SampleMedia.Fonts.EmbeddedFont);
            using var paint       = new SKPaint { IsAntialias = true, TextSize = 64 };
            using var arabicPaint = new SKPaint { IsAntialias = true, TextSize = 64, Typeface = tf };

            // unshaped
            canvas.DrawText("Unshaped:", 100, 100, paint);
            canvas.DrawText("مرحبا بالعالم", 100, 180, arabicPaint);

            // shaped
            using var shaper = new SKShaper(tf);
            canvas.DrawText("Shaped:", 100, 300, paint);
            canvas.DrawShapedText(shaper, "مرحبا بالعالم", 100, 380, arabicPaint);
        }
コード例 #29
0
ファイル: Captcha.cs プロジェクト: mahuwei/QrCodeTest
        internal static byte[] GetCaptcha(string captchaText, string file = null)
        {
            byte[] imageBytes;

            int image2d_x;
            int image2d_y;

            int compensateDeepCharacters;

            using (var drawStyle = CreatePaint()) {
                compensateDeepCharacters = (int)drawStyle.TextSize / 5;
                if (StringComparer.Ordinal.Equals(captchaText, captchaText.ToUpperInvariant()))
                {
                    compensateDeepCharacters = 0;
                }

                var size = SkiaHelpers.MeasureText(captchaText, drawStyle);
                image2d_x = (int)size.Width + 10;
                image2d_y = (int)size.Height + 10 + compensateDeepCharacters;
            }

            using (var image2d = new SKBitmap(image2d_x, image2d_y, SKColorType.Bgra8888, SKAlphaType.Premul)) {
                using (var canvas = new SKCanvas(image2d)) {
                    canvas.DrawColor(SKColors.Black); // Clear

                    using (var drawStyle = CreatePaint()) {
                        canvas.DrawText(captchaText, 0 + 5, image2d_y - 5 - compensateDeepCharacters, drawStyle);
                    }

                    using (var img = SKImage.FromBitmap(image2d)) {
                        if (file != null)
                        {
                            using (var output = File.OpenWrite(file)) {
                                img.Encode(SKEncodedImageFormat.Jpeg, 75).SaveTo(output);
                            }
                        }

                        using (var p = img.Encode(SKEncodedImageFormat.Png, 100)) {
                            imageBytes = p.ToArray();
                        }
                    }
                }
            }

            return(imageBytes);
        }
コード例 #30
0
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            canvas.DrawColor(SKColors.White);

            SKPoint3 direction = new SKPoint3(1.0f, 1.0f, 1.0f);

            using (var paint = new SKPaint())
                using (var filter = SKMaskFilter.CreateEmboss(2.0f, direction, 0.3f, 0.1f))
                {
                    paint.IsAntialias = true;
                    paint.TextSize    = 120;
                    paint.TextAlign   = SKTextAlign.Center;
                    paint.MaskFilter  = filter;

                    canvas.DrawText("SkiaSharp", width / 2f, height / 2f, paint);
                }
        }