Skip to content

Instantly share code, notes, and snippets.

@wutipong
Last active February 14, 2018 12:08
Show Gist options
  • Save wutipong/68c5ae865a5eb32d861f75c6542bc867 to your computer and use it in GitHub Desktop.
Save wutipong/68c5ae865a5eb32d861f75c6542bc867 to your computer and use it in GitHub Desktop.
skiasharp test
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SkiaSharp;
namespace TestPreview
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void skControl_Click(object sender, EventArgs e)
{
}
private void skControl_PaintSurface(object sender, SkiaSharp.Views.Desktop.SKPaintSurfaceEventArgs e)
{
var surface = e.Surface;
var surfaceWidth = e.Info.Width;
var surfaceHeight = e.Info.Height;
var bounds = new SKRect ( 0, 0, surfaceWidth, surfaceHeight );
int offset1;
int offset2;
var canvas = surface.Canvas;
var label = CreateLabel("Hello World", SKColors.DarkOliveGreen);
var panel = CreatePanel(new SKRect(0, 0, 200, 100), SKColors.White);
var shadow = CreateShadow(panel, 2, 4, out offset1 );
var shadow2 = CreateShadow(panel, 6, 3, out offset2);
canvas.DrawBitmap(shadow,
bounds.MidX - label.Width / 2 + offset1,
bounds.MidY + offset1 + 1,
new SKPaint { Color = new SKColor(0xff, 0xff, 0xff, 0x28) });
canvas.DrawBitmap(shadow2,
bounds.MidX - label.Width / 2 + offset2,
bounds.MidY + offset2 + 2,
new SKPaint { Color = new SKColor(0xff, 0xff, 0xff, 0x3D) });
canvas.DrawBitmap(panel, bounds.MidX - label.Width / 2 , bounds.MidY,
new SKPaint { Color = new SKColor(0xff, 0xff, 0xff, 0xFF) });
canvas.DrawBitmap(label, bounds.MidX - label.Width/2 + 20, bounds.MidY + 20);
canvas.Flush();
}
private SKBitmap CreateLabel(string text, SKColor color)
{
var paint = new SKPaint(){
Color = color,
Typeface = SKTypeface.FromFamilyName("Roboto"),
TextSize = 24,
IsAntialias = true
};
var bounds = new SKRect();
var width = paint.MeasureText(text, ref bounds);
var output = new SKBitmap(
(int)Math.Ceiling(bounds.Width),
(int)Math.Ceiling(bounds.Height));
var canvas = new SKCanvas(output);
canvas.Clear();
canvas.DrawText(text, -bounds.Left, -bounds.Top, paint);
canvas.Flush();
return output;
}
private SKBitmap CreatePanel(SKRect rect, SKColor color)
{
var paint = new SKPaint()
{
Color = color,
IsAntialias = true
};
var output = new SKBitmap(
(int)Math.Ceiling(rect.Width),
(int)Math.Ceiling(rect.Height));
var canvas = new SKCanvas(output);
canvas.Clear();
canvas.DrawRoundRect(rect, 1, 1, paint);
canvas.Flush();
return output;
}
private SKBitmap CreateShadow(SKBitmap src, int spread, float radius, out int offset)
{
var output = new SKBitmap(
(int)Math.Ceiling(src.Width + (spread * 2) + (2*radius)),
(int)Math.Ceiling(src.Height + (spread * 2) + (2*radius)));
var resized = src.Resize(
new SKImageInfo(src.Width + (spread * 2), src.Height + (spread * 2)),
SKBitmapResizeMethod.Lanczos3);
var canvas = new SKCanvas(output);
canvas.Clear();
canvas.DrawBitmap(resized, 0 + radius, 0 + radius, new SKPaint
{
MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal,
SKMaskFilter.ConvertRadiusToSigma(radius),
SKBlurMaskFilterFlags.All),
});
canvas.DrawRect(new SKRect(0, 0, output.Width, output.Height), new SKPaint
{
Color = SKColors.Black,
BlendMode = SKBlendMode.SrcATop,
IsAntialias = true
});
canvas.Flush();
offset = -(int)Math.Ceiling(spread + radius);
return output;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment