public FileStreamResult BlurImage([FromForm] BlurImageRequest request) { using (var s = request.Image.OpenReadStream()) { using (var originalImageBitmap = SKBitmap.Decode(s)) { using (var paint = new SKPaint { ImageFilter = SKImageFilter.CreateBlur(15, 15) }) { using (var canvas = new SKCanvas(originalImageBitmap)) { canvas.DrawBitmap(originalImageBitmap, 0, 0, paint); canvas.Flush(); var resultStream = new MemoryStream(); using (var data = SKImage.FromBitmap(originalImageBitmap) .Encode(SKEncodedImageFormat.Jpeg, 100)) { data.SaveTo(resultStream); }; resultStream.Position = 0; return(new FileStreamResult(resultStream, "image/jpeg") { FileDownloadName = "blurred", EnableRangeProcessing = true }); } } } } }
protected override void OnPaintSurface(SKPaintSurfaceEventArgs e) { Trace.WriteLine($"{GetType().Name} OnPaintSurface"); SKImageInfo info = e.Info; SKSurface surface = e.Surface; SKCanvas canvas = surface.Canvas; using (var snap = surface.Snapshot()) { using (var paint = new SKPaint()) { Debug.WriteLine($"Source={Source.ToString()}"); if (Source is StreamImageSource) { // 一部だけ切り出そうにも土台のサイズがわからない StreamImageSource streamImageSource = (StreamImageSource)Source; CancellationToken cancellationToken = CancellationToken.None; Task <Stream> task = streamImageSource.Stream(cancellationToken); Stream stream = task.Result; var sKBitmap = SKBitmap.Decode(stream); paint.ImageFilter = SKImageFilter.CreateBlur(1, 1); canvas.DrawBitmap(sKBitmap, info.Rect, paint); } } } }
public override void Render(IDrawingContextImpl drw_context) { base.Render(drw_context); if (drw_context is ISkiaDrawingContextImpl context) { var canvas = context.SkCanvas; if (_data == null) { return; } using (var bitmap = SKBitmap.Decode(_data.ToArray())) using (var paint = new SKPaint()) { if (bitmap == null) { Debug.WriteLine("bitmap is null"); return; } var img = SKImage.FromBitmap(bitmap); paint.ImageFilter = SKImageFilter.CreateBlur(_levelx, _levely); canvas.DrawImage(img, _dest.ToSKRect(), Bounds.ToSKRect(), paint); } } }
public void Open() { paint.ImageFilter = SKImageFilter.CreateBlur(blurLevel, blurLevel, SKShaderTileMode.Mirror); paint.IsAntialias = true; canvas.Save(); }
private void SKCanvasView_OnPaintSurface(object sender, SKPaintSurfaceEventArgs e) { var skCanvas = e.Surface.Canvas; var skinfo = e.Info; skCanvas.Clear(); var RectangleStyleFillShadowColor = new SKColor(24, 24, 24, 70); var RectangleStyleFillShadow = SKImageFilter.CreateDropShadow( (float)SliderShadowX.Value, (float)SliderShadowY.Value, (float)SliderSigmaX.Value, (float)SliderSigmaY.Value, RectangleStyleFillShadowColor, SKDropShadowImageFilterShadowMode.DrawShadowAndForeground, SKImageFilter.CreateBlur((float)SliderBlurX.Value, (float)SliderBlurY.Value), null); var RectangleStyleFillPaint = new SKPaint() { Style = SKPaintStyle.Fill, Color = SKColors.White, BlendMode = SKBlendMode.SrcOver, IsAntialias = true, ImageFilter = RectangleStyleFillShadow }; var shadowMargin = (float)SliderShadowMargin.Value; skCanvas.DrawRect(new SKRect(shadowMargin, shadowMargin, skinfo.Width - shadowMargin, skinfo.Height - shadowMargin), RectangleStyleFillPaint); }
public void Render(IDrawingContextImpl context) { var canvas = (context as ISkiaDrawingContextImpl)?.SkCanvas; if (canvas == null) { using (var c = new DrawingContext(context, false)) { c.DrawText(_noSkia, new Point()); } } else { canvas.Save(); // create the first shader var colors = new SKColor[] { new SKColor(0, 255, 255), new SKColor(255, 0, 255), new SKColor(255, 255, 0), new SKColor(0, 255, 255) }; var sx = Animate(100, 2, 10); var sy = Animate(1000, 5, 15); var lightPosition = new SKPoint( (float)(Bounds.Width / 2 + Math.Cos(St.Elapsed.TotalSeconds) * Bounds.Width / 4), (float)(Bounds.Height / 2 + Math.Sin(St.Elapsed.TotalSeconds) * Bounds.Height / 4)); using (var sweep = SKShader.CreateSweepGradient(new SKPoint((int)Bounds.Width / 2, (int)Bounds.Height / 2), colors, null)) using (var turbulence = SKShader.CreatePerlinNoiseFractalNoise(0.05f, 0.05f, 4, 0)) using (var shader = SKShader.CreateCompose(sweep, turbulence, SKBlendMode.SrcATop)) using (var blur = SKImageFilter.CreateBlur(Animate(100, 2, 10), Animate(100, 5, 15))) using (var paint = new SKPaint { Shader = shader, ImageFilter = blur }) canvas.DrawPaint(paint); using (var pseudoLight = SKShader.CreateRadialGradient( lightPosition, (float)(Bounds.Width / 3), new [] { new SKColor(255, 200, 200, 100), SKColors.Transparent, new SKColor(40, 40, 40, 220), new SKColor(20, 20, 20, (byte)Animate(100, 200, 220)) }, new float[] { 0.3f, 0.3f, 0.8f, 1 }, SKShaderTileMode.Clamp)) using (var paint = new SKPaint { Shader = pseudoLight }) canvas.DrawPaint(paint); canvas.Restore(); } }
public async ValueTask <IActionResult> GetBlurAsync(int size, string topic = null, int blurStrength = 5) { using (var filter = SKImageFilter.CreateBlur(blurStrength, blurStrength)) { var response = await GetWithFilterAsync(size, topic, filter); return(response); } }
private void Draw_ImageBlur(SKCanvas skCanvas, SKBitmap skBitmap) { // Image Filter var filter = SKImageFilter.CreateBlur(5, 5); var skPaint = new SKPaint(); skPaint.ImageFilter = filter; skCanvas.DrawBitmap(skBitmap, SKRect.Create(-50, -50, 100, 100), skPaint); }
void DrawField(SKCanvas canvas) { SKPaint paint = new SKPaint { Shader = SKShader.CreateRadialGradient( ViewModel.GridCenter, ViewModel.CenterRadius, new SKColor[] { SKColor.Parse("#3949AB").WithAlpha(127), backgroundSKColor }, null, 0), MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 10) }; canvas.DrawCircle(ViewModel.GridCenter, ViewModel.CenterRadius, paint); paint = new SKPaint { Color = SKColors.White, Style = SKPaintStyle.Stroke, StrokeWidth = 1, MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 1) }; for (int i = 0; i <= ViewModel.GridSize; i++) { canvas.DrawLine( ViewModel.GetGridPoint(new SKPoint(i, 0)), ViewModel.GetGridPoint(new SKPoint(i, ViewModel.GridSize)), paint ); canvas.DrawLine( ViewModel.GetGridPoint(new SKPoint(0, i)), ViewModel.GetGridPoint(new SKPoint(ViewModel.GridSize, i)), paint ); } paint.StrokeWidth = 5; paint.Color = ViewModel.BorderColor; SKPoint p1 = ViewModel.GetGridPoint(new SKPoint(0, 0)); SKPoint p2 = ViewModel.GetGridPoint(new SKPoint(ViewModel.GridSize, ViewModel.GridSize)); canvas.DrawRect(p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y, paint); paint = new SKPaint { Color = paint.Color.WithAlpha(32), Style = SKPaintStyle.Fill, StrokeWidth = 1, ImageFilter = SKImageFilter.CreateBlur(20, 20), }; canvas.DrawRect(p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y, paint); }
public void Render(IDrawingContextImpl context) { if (context is not ISkiaDrawingContextImpl skia) { return; } if (!skia.SkCanvas.TotalMatrix.TryInvert(out var currentInvertedTransform)) { return; } // One or both dimensions has zero size. // Skia doesnt like that. if (_bounds.Size.AspectRatio == 0) { return; } using var backgroundSnapshot = skia.SkSurface.Snapshot(); using var preBlur = DrawingContextHelper.CreateDrawingContext( new Size(backgroundSnapshot.Width, backgroundSnapshot.Height), new Vector(96, 96), skia.GrContext); using (var filter = SKImageFilter.CreateBlur((int)_blurRadius.X, (int)_blurRadius.Y, SKShaderTileMode.Clamp)) using (var blurPaint = new SKPaint { ImageFilter = filter }) { var canvas = preBlur.SkSurface.Canvas; canvas.DrawSurface(skia.SkSurface, 0f, 0f, blurPaint); canvas.Flush(); } using var preBlurredSnapshot = preBlur.SkSurface.Snapshot(); using var backdropShader = SKShader.CreateImage(preBlurredSnapshot, SKShaderTileMode.Clamp, SKShaderTileMode.Clamp, currentInvertedTransform); using var blurred = DrawingContextHelper.CreateDrawingContext(_bounds.Size, new Vector(96, 96), skia.GrContext); using (var blurPaint = new SKPaint { Shader = backdropShader }) { blurred.SkSurface.Canvas.DrawRect(0, 0, (float)_bounds.Width, (float)_bounds.Height, blurPaint); } blurred.DrawTo(skia); }
protected override void OnPaintSurface(SKPaintSurfaceEventArgs e) { base.OnPaintSurface(e); var canvas = e.Surface.Canvas; var size = Math.Min(e.Info.Width, e.Info.Height) - ProgressThickness; canvas.Clear(); using var paint = new SKPaint(); paint.Style = SKPaintStyle.Stroke; paint.StrokeCap = SKStrokeCap.Round; paint.StrokeWidth = ProgressThickness; paint.IsAntialias = true; if (Jagged) { paint.PathEffect = SKPathEffect.CreateDiscrete(12f, 4f, (uint)Guid.NewGuid().GetHashCode()); } using var path = new SKPath(); var left = (e.Info.Width - size) / 2f; var top = (e.Info.Height - size) / 2f; var right = left + size; var bottom = top + size; path.AddArc(new SKRect(left, top, right, bottom), 0, 360); paint.Color = ProgressColor.AddLuminosity(-.3d).ToSKColor(); canvas.DrawPath(path, paint); path.Reset(); path.AddArc(new SKRect(left, top, right, bottom), StartingDegrees, EndingDegrees); paint.Color = SKColors.Black; paint.ImageFilter = SKImageFilter.CreateBlur(3f, 3f); paint.BlendMode = SKBlendMode.SrcATop; canvas.DrawPath(path, paint); paint.ImageFilter = null; paint.BlendMode = SKBlendMode.SrcOver; paint.Color = ProgressColor.ToSKColor(); canvas.DrawPath(path, paint); }
protected override void OnDrawSample(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); using (var stream = new SKManagedStream(SampleMedia.Images.Baboon)) using (var bitmap = SKBitmap.Decode(stream)) using (var filter = SKImageFilter.CreateBlur(5, 5)) using (var paint = new SKPaint()) { paint.ImageFilter = filter; canvas.DrawBitmap(bitmap, SKRect.Create(width, height), paint); } }
private void ApplyFilters(SKPaint paint, RasterizeLayerStyleDefinition layerStyleDef, RasterizeLayerStyle layerStyle) { paint.ColorFilter = SKColorFilter.CreateBlendMode(layerStyleDef.Color, SKBlendMode.SrcIn); if (layerStyleDef.IsBlurEnabled && layerStyleDef.IsDilateEnabled) { paint.ImageFilter = SKImageFilter.CreateBlur(layerStyle.Blur.X, layerStyle.Blur.Y, SKImageFilter.CreateDilate((int)layerStyle.Dilate.X, (int)layerStyle.Dilate.Y)); } else if (layerStyleDef.IsBlurEnabled) { paint.ImageFilter = SKImageFilter.CreateBlur(layerStyle.Blur.X, layerStyle.Blur.Y); } else if (layerStyleDef.IsDilateEnabled) { paint.ImageFilter = SKImageFilter.CreateDilate((int)layerStyle.Dilate.X, (int)layerStyle.Dilate.Y); } }
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); } }
public void Blur(int radius) { using (var surface = SKSurface.Create(new SKImageInfo(Context.Image.Width, Context.Image.Height))) using (var canvas = surface.Canvas) using (var paint = new SKPaint()) { paint.ImageFilter = SKImageFilter.CreateBlur(radius, radius); SKRect rect = new SKRect(0, 0, Context.Image.Width, Context.Image.Height); rect.Inflate(10, 10); //removes black border canvas.DrawBitmap(Context.Image, rect, paint); canvas.Flush(); // save Context.Image = SKBitmap.FromImage(surface.Snapshot()); } }
public async ValueTask <IActionResult> GetMergeAsync( int size, string topic = null) { Task <byte[]> t1 = DoManipAsync(); Task <byte[]> t2 = DoManipAsync(); async Task <byte[]> DoManipAsync() { using (var filter = SKImageFilter.CreateBlur(5, 5)) { var result = await GetBytesWithFilterAsync(size, topic, filter);//.ConfigureAwait(false); return(result); } } var results = await Task.WhenAll(t1, t2).ConfigureAwait(false); var imageInfo = new SKImageInfo(size, size); var mergeInfo = new SKImageInfo(size * 2, size); using (var outStream = new MemoryStream()) using (var bitmap0 = SKBitmap.Decode(results[0], imageInfo)) using (var bitmap1 = SKBitmap.Decode(results[1], imageInfo)) using (var surface = SKSurface.Create(mergeInfo)) using (var paint = new SKPaint()) { SKCanvas canvas = surface.Canvas; canvas.DrawColor(SKColors.White); // draw the bitmap through the filter var rect = SKRect.Create(imageInfo.Size); canvas.DrawBitmap(bitmap0, rect, paint); rect = SKRect.Create(size, 0, size, size); canvas.DrawBitmap(bitmap1, rect, paint); SKData data = surface.Snapshot().Encode(SKEncodedImageFormat.Jpeg, 80); data.SaveTo(outStream); byte[] manipedImage = outStream.ToArray(); var response = File(manipedImage, MEDIA_TYPE); return(response); } }
public RectParticle(SKColor color, SKPoint3 rotationSpeed, float translationSpeed, float direction, SKPoint3 orientation, SKPoint position, SKSize size, float blurFactor) : base(rotationSpeed, translationSpeed, direction, orientation, position, size) { _blurFactor = blurFactor; Paint = new SKPaint { IsAntialias = true, Color = color, Style = SKPaintStyle.Fill, ImageFilter = SKImageFilter.CreateBlur(blurFactor, blurFactor) }; }
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); }
/// <summary> /// Make the background image a little bit blurry. /// </summary> /// <remarks> /// It's clearly the UI layer's job. /// </remarks> void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { var surface = args.Surface; var canvas = surface.Canvas; var snap = surface.Snapshot(); Assembly assembly = GetType().GetTypeInfo().Assembly; Stream stream = assembly.GetManifestResourceStream("RandomUser_Xamarin_MVVM.background.jpg"); canvas.DrawColor(SKColors.Black); using var bitmap = SKBitmap.Decode(stream); using var paint = new SKPaint(); using var filter = SKImageFilter.CreateBlur(10, 10); paint.ImageFilter = filter; canvas.DrawBitmap(bitmap, SKRect.Create(snap.Width, snap.Height), paint); }
public void drawAdaptiveBackground(SKCanvas canvas, SKBitmap bitmap) { if (background.type != SSBackgroundType.Adaptive) { return; } if (bitmap.Width != canvasSize.width || bitmap.Height != canvasSize.height) { Print.Warning("Adaptive background size is not compatible with screenshot size, resizing adaptive background..."); var info = new SKImageInfo(canvasSize.width, canvasSize.height); bitmap = bitmap.Resize(info, SKFilterQuality.High); } var filter = SKImageFilter.CreateBlur(20, 20); var paint = new SKPaint { ImageFilter = filter }; canvas.DrawBitmap(bitmap, new SKPoint(0, 0), paint); }
public static BoxShadowFilter Create(SKPaint paint, BoxShadow shadow, double opacity) { var ac = shadow.Color; SKImageFilter filter = null; filter = SKImageFilter.CreateBlur(SkBlurRadiusToSigma(shadow.Blur), SkBlurRadiusToSigma(shadow.Blur)); var color = new SKColor(ac.R, ac.G, ac.B, (byte)(ac.A * opacity)); paint.Reset(); paint.IsAntialias = true; paint.Color = color; paint.ImageFilter = filter; return(new BoxShadowFilter { Paint = paint, _filter = filter, ClipOperation = shadow.IsInset ? SKClipOperation.Intersect : SKClipOperation.Difference }); }
public override void Update(double deltaTime) { if (Math.Abs(Properties.BlurAmount.CurrentValue.Width - _lastWidth) > 0.01 || Math.Abs(Properties.BlurAmount.CurrentValue.Height - _lastHeight) > 0.01) { if (Properties.BlurAmount.CurrentValue.Width <= 0 && Properties.BlurAmount.CurrentValue.Height <= 0) { _imageFilter = null; } else { _imageFilter = SKImageFilter.CreateBlur( Properties.BlurAmount.CurrentValue.Width, Properties.BlurAmount.CurrentValue.Height ); } _lastWidth = Properties.BlurAmount.CurrentValue.Width; _lastHeight = Properties.BlurAmount.CurrentValue.Height; } }
private void WindowOnPaintSurface(object sender, SKPaintGLSurfaceEventArgs e) { var canvas = e.Surface.Canvas; canvas.Clear(ClearColor); var RectangleStyleFillShadowColor = new SKColor(255, 0, 0, 255); var RectangleStyleFillShadow = SKImageFilter.CreateDropShadow(0f, 0f, 20f, 20f, RectangleStyleFillShadowColor, SKDropShadowImageFilterShadowMode.DrawShadowOnly, null, null); var RectangleStyleFillColor = new SKColor(255, 255, 255, 255); canvas.DrawRect(100, 100, 800, 800, new SKPaint { Color = new SKColor(202, 157, 93), Style = SKPaintStyle.Fill, // Color = RectangleStyleFillColor, BlendMode = SKBlendMode.SrcOver, IsAntialias = true, ImageFilter = SKImageFilter.CreateBlur(5, 5) // ImageFilter = RectangleStyleFillShadow }); canvas.DrawCircle(200, 200, 50, new SKPaint { IsAntialias = true, Color = new SKColor(115, 61, 19), ImageFilter = SKImageFilter.CreateBlur(15, 15) }); // var pos = new Position // { // X = 300, // Y = 100 // }; // DrawTreeElements(canvas, Window.Document.ChildNodes.OfType<Element>().ToList(), ref pos); // // _renderer.Render(canvas); }
private void SKElement_OnPaintSurface(object sender, SKPaintSurfaceEventArgs e) { SKCanvas canvas = e.Surface.Canvas; canvas.Clear(); //TODO: bad check, should handle exception if (SelectedFile?.Exists != true) { return; } var element = (SKElement)sender; //TODO: Horribly inefficient using (Stream imageStream = SelectedFile.OpenRead()) using (SKBitmap bitmap = SKBitmap.Decode(imageStream)) using (SKPaint paint = new SKPaint()) using (SKImageFilter filter = GetImageFilters().FirstOrDefault()) { paint.ImageFilter = filter; float x = 0, y = 0; if (Offset != null) { x = (float)Offset.Value.X; y = (float)Offset.Value.Y; } var source = new SKRect(x, y, x + (float)element.ActualWidth, y + (float)element.ActualHeight); canvas.DrawBitmap(bitmap, source, new SKRect(0, 0, (float)element.ActualWidth, (float)element.ActualHeight), paint); } IEnumerable <SKImageFilter> GetImageFilters() { if (int.TryParse(Blur.Text, out int blurValue) && blurValue > 0) { yield return(SKImageFilter.CreateBlur(blurValue, blurValue)); } } }
public void DrawSketch(SKCanvas canvas) { // Build OvalStyleFill Blur var OvalStyleFillBlur = SKImageFilter.CreateBlur(9.522291f, 4.249391f, null, null); // Fill color for Oval Style var OvalStyleFillColor = new SKColor(255, 0, 128, 255); // New Oval Style fill paint var OvalStyleFillPaint = new SKPaint() { Style = SKPaintStyle.Fill, Color = OvalStyleFillColor, BlendMode = SKBlendMode.SrcOver, IsAntialias = true, PathEffect = SKPathEffect.CreateDiscrete(11.70298f, 8.239141f), ImageFilter = OvalStyleFillBlur }; // Frame color for Oval Style var OvalStyleFrameColor = new SKColor(204, 102, 255, 255); // New Oval Style frame paint var OvalStyleFramePaint = new SKPaint() { Style = SKPaintStyle.Stroke, Color = OvalStyleFrameColor, BlendMode = SKBlendMode.SrcOver, IsAntialias = true, StrokeWidth = 1f, StrokeMiter = 4f, StrokeJoin = SKStrokeJoin.Miter, StrokeCap = SKStrokeCap.Butt }; // Draw Oval shape canvas.DrawOval(new SKRect(51.79688f, 47.54688f, 235.5156f, 216.3242f), OvalStyleFillPaint); canvas.DrawOval(new SKRect(51.79688f, 47.54688f, 235.5156f, 216.3242f), OvalStyleFramePaint); }
private void SkCanvasView_OnPaintSurface(object sender, SKPaintSurfaceEventArgs e) { // Init skcanvas SKImageInfo skImageInfo = e.Info; SKSurface skSurface = e.Surface; SKCanvas skCanvas = skSurface.Canvas; skCanvas.Clear(SKColors.White); var skCanvasWidth = skImageInfo.Width; var skCanvasheight = skImageInfo.Height; // move canvas X,Y to center of screen skCanvas.Translate((float)skCanvasWidth / 2, (float)skCanvasheight / 2); // set the pixel scale of the canvas skCanvas.Scale(skCanvasWidth / 200f); string resourceID = "Demo3.Resources.xamarinmonkey.png"; Assembly assembly = GetType().GetTypeInfo().Assembly; SKBitmap skBitmap; using (Stream stream = assembly.GetManifestResourceStream(resourceID)) using (SKManagedStream skStream = new SKManagedStream(stream)) { skBitmap = SKBitmap.Decode(skStream); } // Blur Image Filter var filter = SKImageFilter.CreateBlur(5, 5); var skPaint = new SKPaint(); skPaint.ImageFilter = filter; skCanvas.DrawBitmap(skBitmap, SKRect.Create(-50, -50, 100, 100), skPaint); }
private void drawImageBackground(SKCanvas canvas) { SKBitmap bgBitmap = SkiaHelper.createPersistentBitmap(background.imagePath, canvasSize.width, canvasSize.height); if (bgBitmap.Width != canvasSize.width || bgBitmap.Height != canvasSize.height) { var info = new SKImageInfo(canvasSize.width, canvasSize.height); bgBitmap = bgBitmap.Resize(info, SKFilterQuality.High); } if (background.blur.HasValue && background.blur != 0) { var filter = SKImageFilter.CreateBlur((int)background.blur, (int)background.blur); var paint = new SKPaint { ImageFilter = filter }; canvas.DrawBitmap(bgBitmap, new SKPoint(0, 0), paint); } else { canvas.DrawBitmap(bgBitmap, new SKPoint(0, 0)); } }
/// <summary> /// Paint svg image /// </summary> protected override void OnPaintSurface(SKPaintSurfaceEventArgs e) { if (string.IsNullOrEmpty(ResourceKey) || string.IsNullOrEmpty(AssemblyName)) { return; } SKImageInfo info = e.Info; SKSurface surface = e.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); if (_image == null) { Assembly assembly = Assembly.Load(new AssemblyName(ActualAssemblyName)); using (var stream = assembly.GetManifestResourceStream(ActualAssemblyName + "." + ResourceKey)) { SKBitmap bitmap = SKBitmap.Decode(stream); _image = SKImage.FromBitmap(bitmap); } } float deviceScale = (float)(info.Width / Width); float width = (float)(Width * deviceScale); float height = (float)(Height * deviceScale); double scale = 1; if (Aspect == Aspect.AspectFit) { // Scale based on min dimension and keep aspect ratio scale = Math.Min(width, height) / Math.Max(_image.Height, _image.Width); } else if (Aspect == Aspect.AspectFill) { // Scale based on max dimension and keep aspect ratio scale = Math.Max(width, height) / Math.Min(_image.Height, _image.Width); } else { // no scale } ActualImageSize = new Size(Math.Round((_image.Width * scale) / deviceScale, MidpointRounding.ToEven), Math.Round((_image.Height * scale) / deviceScale, MidpointRounding.ToEven)); var paint = new SKPaint(); if (Blur > 0) { paint.ImageFilter = SKImageFilter.CreateBlur((float)Blur, (float)Blur, null, null); } if (ActualImageSize.Height > Height) { canvas.Translate(0, -(float)((ActualImageSize.Height - Height) / 2) * deviceScale); } canvas.DrawImage(_image, SKRect.Create(new SKPoint(0, 0), new SKSize((float)ActualImageSize.Width * deviceScale, (float)ActualImageSize.Height * deviceScale)), paint); }
public string EncodeImage(string inputPath, DateTime dateModified, string outputPath, bool autoOrient, ImageOrientation?orientation, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat) { if (string.IsNullOrWhiteSpace(inputPath)) { throw new ArgumentNullException(nameof(inputPath)); } if (string.IsNullOrWhiteSpace(inputPath)) { throw new ArgumentNullException(nameof(outputPath)); } var skiaOutputFormat = GetImageFormat(selectedOutputFormat); var hasBackgroundColor = !string.IsNullOrWhiteSpace(options.BackgroundColor); var hasForegroundColor = !string.IsNullOrWhiteSpace(options.ForegroundLayer); var blur = options.Blur ?? 0; var hasIndicator = options.AddPlayedIndicator || options.UnplayedCount.HasValue || !options.PercentPlayed.Equals(0); using (var bitmap = GetBitmap(inputPath, options.CropWhiteSpace, autoOrient, orientation)) { if (bitmap == null) { throw new ArgumentOutOfRangeException(string.Format("Skia unable to read image {0}", inputPath)); } var originalImageSize = new ImageDimensions(bitmap.Width, bitmap.Height); if (!options.CropWhiteSpace && options.HasDefaultOptions(inputPath, originalImageSize) && !autoOrient) { // Just spit out the original file if all the options are default return(inputPath); } var newImageSize = ImageHelper.GetNewImageSize(options, originalImageSize); var width = newImageSize.Width; var height = newImageSize.Height; using (var resizedBitmap = new SKBitmap(width, height, bitmap.ColorType, bitmap.AlphaType)) { // scale image bitmap.ScalePixels(resizedBitmap, SKFilterQuality.High); // If all we're doing is resizing then we can stop now if (!hasBackgroundColor && !hasForegroundColor && blur == 0 && !hasIndicator) { Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); using (var outputStream = new SKFileWStream(outputPath)) using (var pixmap = new SKPixmap(new SKImageInfo(width, height), resizedBitmap.GetPixels())) { pixmap.Encode(outputStream, skiaOutputFormat, quality); return(outputPath); } } // create bitmap to use for canvas drawing used to draw into bitmap using (var saveBitmap = new SKBitmap(width, height)) //, bitmap.ColorType, bitmap.AlphaType)) using (var canvas = new SKCanvas(saveBitmap)) { // set background color if present if (hasBackgroundColor) { canvas.Clear(SKColor.Parse(options.BackgroundColor)); } // Add blur if option is present if (blur > 0) { // create image from resized bitmap to apply blur using (var paint = new SKPaint()) using (var filter = SKImageFilter.CreateBlur(blur, blur)) { paint.ImageFilter = filter; canvas.DrawBitmap(resizedBitmap, SKRect.Create(width, height), paint); } } else { // draw resized bitmap onto canvas canvas.DrawBitmap(resizedBitmap, SKRect.Create(width, height)); } // If foreground layer present then draw if (hasForegroundColor) { if (!double.TryParse(options.ForegroundLayer, out double opacity)) { opacity = .4; } canvas.DrawColor(new SKColor(0, 0, 0, (byte)((1 - opacity) * 0xFF)), SKBlendMode.SrcOver); } if (hasIndicator) { DrawIndicator(canvas, width, height, options); } Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); using (var outputStream = new SKFileWStream(outputPath)) { using (var pixmap = new SKPixmap(new SKImageInfo(width, height), saveBitmap.GetPixels())) { pixmap.Encode(outputStream, skiaOutputFormat, quality); } } } } } return(outputPath); }
public IEffect CreateBlurEffect(SizeF blurSize) { return(new SkiaEffect(SKImageFilter.CreateBlur(blurSize.Width, blurSize.Height, SKShaderTileMode.Decal))); }