public static (SKSizeI Scaled, SKSize Unscaled) GetCanvasSize(DpiPath dpi, SKSize?baseSize = null, SkiaSharpTools baseTools = null) { // if an explicit size was given by the type of image, use that if (dpi.Size is SKSize size) { var scale = (float)dpi.Scale; var scaled = new SKSizeI( (int)(size.Width * scale), (int)(size.Height * scale)); return(scaled, size); } // if an explicit size was given in the csproj, use that if (baseSize is SKSize bs) { var scale = (float)dpi.Scale; var scaled = new SKSizeI( (int)(bs.Width * scale), (int)(bs.Height * scale)); return(scaled, bs); } // try determine the best size based on the loaded image if (baseTools is not null) { var baseOriginalSize = baseTools.GetOriginalSize(); var(baseScaledSize, _) = baseTools.GetScaledSize(baseOriginalSize, dpi.Scale); return(baseScaledSize, baseOriginalSize); } throw new InvalidOperationException("The canvas size cannot be calculated if there is no size to start from (DPI size, BaseSize or image size)."); }
public DpiPath(string path, decimal scale, string suffix = null, SKSize?size = null, string[] idioms = null) { Path = path; Scale = scale; FileSuffix = suffix; Size = size; Idioms = idioms; }
public SkiaSharpBitmapTools(string filename, SKSize?baseSize, SKColor?tintColor, ILogger logger) : base(filename, baseSize, tintColor, logger) { var sw = new Stopwatch(); sw.Start(); bmp = SKBitmap.Decode(filename); sw.Stop(); Logger?.Log($"Open RASTER took {sw.ElapsedMilliseconds}ms ({filename})"); }
public SkiaSharpSvgTools(string filename, SKSize?baseSize, SKColor?backgroundColor, SKColor?tintColor, ILogger logger) : base(filename, baseSize, backgroundColor, tintColor, logger) { var sw = new Stopwatch(); sw.Start(); svg = new SKSvg(); svg.Load(filename); sw.Stop(); Logger?.Log($"Open SVG took {sw.ElapsedMilliseconds}ms ({filename})"); }
/** * <summary>Creates a new page within the specified document context.</summary> * <param name="context">Document where to place this page.</param> * <param name="size">Page size. In case of <code>null</code>, uses the default SKSize.</param> */ public Page(Document context, SKSize?size) : base( context, new PdfDictionary( new PdfName[] { PdfName.Type, PdfName.Contents }, new PdfDirectObject[] { PdfName.Page, context.File.Register(new PdfStream()) } ) ) { if (size.HasValue) { Size = size.Value; } }
/** * <summary>Creates a new predefined stamp on the specified page.</summary> * <param name="page">Page where this stamp has to be placed.</param> * <param name="location">Position where this stamp has to be centered.</param> * <param name="size">Dimension of the stamp: * <list type="bullet"> * <item><c>null</c> to apply the natural size</item> * <item><c>size(0, height)</c> to scale the width proportionally to the height</item> * <item><c>size(width, 0)</c> to scale the height proportionally to the width</item> * </list> * </param> * <param name="text">Annotation text.</param> * <param name="type">Predefined stamp type.</param> */ public Stamp(Page page, SKPoint location, SKSize?size, string text, StandardStampEnum type) : base(page, PdfName.Stamp, GeomUtils.Align(size.HasValue ? SKRect.Create(0, 0, size.Value.Width > 0 ? size.Value.Width : size.Value.Height * type.GetAspect(), size.Value.Height > 0 ? size.Value.Height : size.Value.Width / type.GetAspect()) : SKRect.Create(0, 0, 40 * type.GetAspect(), 40), location, new SKPoint(0, 0)), text) { TypeName = type.GetName().StringValue; }
public SkiaSharpTools(string filename, SKSize?baseSize, SKColor?tintColor, ILogger logger) { Logger = logger; Filename = filename; BaseSize = baseSize; if (tintColor is SKColor tint) { Logger?.Log($"Detected a tint color of {tint}"); Paint = new SKPaint { ColorFilter = SKColorFilter.CreateBlendMode(tint, SKBlendMode.SrcIn) }; } }
public static SkiaSharpTools Create(bool isVector, string filename, SKSize?baseSize, SKColor?tintColor, ILogger logger) => isVector ? new SkiaSharpSvgTools(filename, baseSize, tintColor, logger) as SkiaSharpTools : new SkiaSharpBitmapTools(filename, baseSize, tintColor, logger);
/** * <summary>Scans a content level looking for images.</summary> */ /* * NOTE: Page contents are represented by a sequence of content objects, * possibly nested into multiple levels. */ private void Scan(ContentScanner level, Page page) { if (level == null) { return; } while (level.MoveNext()) { ContentObject current = level.Current; if (current is ContainerObject) { // Scan the inner level! Scan(level.ChildLevel, page); } else { GraphicsObjectWrapper objectWrapper = level.CurrentWrapper; if (objectWrapper == null) { continue; } /* * NOTE: Images can be represented on a page either as * external objects (XObject) or inline objects. */ SKSize?imageSize = null; // Image native size. if (objectWrapper is XObjectWrapper) { XObjectWrapper xObjectWrapper = (XObjectWrapper)objectWrapper; xObjects::XObject xObject = xObjectWrapper.XObject; // Is the external object an image? if (xObject is xObjects::ImageXObject) { Console.Write( "External Image '" + xObjectWrapper.Name + "' (" + xObject.BaseObject + ")" // Image key and indirect reference. ); imageSize = xObject.Size; // Image native size. } } else if (objectWrapper is InlineImageWrapper) { Console.Write("Inline Image"); InlineImage inlineImage = ((InlineImageWrapper)objectWrapper).InlineImage; imageSize = inlineImage.Size; // Image native size. } if (imageSize.HasValue) { SKRect box = objectWrapper.Box.Value; // Image position (location and size) on the page. Console.WriteLine( " on page " + page.Number + " (" + page.BaseObject + ")" // Page index and indirect reference. ); Console.WriteLine(" Coordinates:"); Console.WriteLine(" x: " + Math.Round(box.Left)); Console.WriteLine(" y: " + Math.Round(box.Top)); Console.WriteLine(" width: " + Math.Round(box.Width) + " (native: " + Math.Round(imageSize.Value.Width) + ")"); Console.WriteLine(" height: " + Math.Round(box.Height) + " (native: " + Math.Round(imageSize.Value.Height) + ")"); } } } }
public (SKSizeI, float) GetScaledSize(SKSize originalSize, DpiPath dpi, SKSize?absoluteSize = null) =>
public static void DrawShadowView( ThumbnailsRenderContext ctx, ISkView view, SKImage?decorationImage = null, SKColor?backgroundColor = null, SKSize?maxSize = null, SKSize?minSize = null, SKFilterQuality resizeQuality = SKFilterQuality.None) { // max width = 128 - 12 - 12 // max height = 128 - 20 - 20 maxSize ??= new SKSize(DefaultMaxWidth, DefaultMaxHeight); minSize ??= new SKSize(0, 0); backgroundColor ??= new SKColor(0xff, 0xff, 0xff); var imageSize = ContainSize(new SKSize(view.Size.Width, view.Size.Height), maxSize.Value); var imageRect = CentralRect(imageSize); var borderSize = new SKSize(Math.Max(imageSize.Width, minSize.Value.Width), Math.Max(imageSize.Height, minSize.Value.Height)); var borderRect = CentralRect(new SKSize(borderSize.Width + 1, borderSize.Height + 1)); using (new SKAutoCanvasRestore(ctx.Canvas)) { ctx.Canvas.Clear(); using (var rectFillPaint = new SKPaint { Style = SKPaintStyle.Fill, Color = backgroundColor.Value, ImageFilter = SKImageFilter.CreateDropShadow(0, 1, 4, 4, new SKColor(136, 136, 136, 128)) }) using (var rectStrokePaint = new SKPaint { Style = SKPaintStyle.Stroke, StrokeWidth = 1, Color = new SKColor(136, 136, 136, 64), BlendMode = SKBlendMode.Src }) { // draw border using SKRoundRect roundRect = new(borderRect, 5); ctx.Canvas.DrawRoundRect(roundRect, rectFillPaint); ctx.Canvas.DrawRoundRect(roundRect, rectStrokePaint); } using (new SKAutoCanvasRestore(ctx.Canvas)) using (var imagePaint = new SKPaint { FilterQuality = resizeQuality }) using (var decorationImagePaint = new SKPaint { FilterQuality = SKFilterQuality.Medium }) { using SKRoundRect roundRect = new(imageRect, 4.5f); using (new SKAutoCanvasRestore(ctx.Canvas)) { ctx.Canvas.ClipRoundRect(roundRect); // draw image view.Draw(ctx.Canvas, imageRect, imagePaint); } if (decorationImage != null) { var decorationRect = SKRect.Create(borderRect.Right - 24.5f, borderRect.Bottom - 22.5f, 36, 36); ctx.Canvas.DrawImage(decorationImage, decorationRect, decorationImagePaint); } } } }