private SKBitmap BuildSquareCollageBitmap(string[] paths, int width, int height) { var bitmap = new SKBitmap(width, height); var imageIndex = 0; var cellWidth = width / 2; var cellHeight = height / 2; using var canvas = new SKCanvas(bitmap); for (var x = 0; x < 2; x++) { for (var y = 0; y < 2; y++) { using var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex); imageIndex = newIndex; if (currentBitmap == null) { continue; } // Scale image. The FromBitmap creates a copy var imageInfo = new SKImageInfo(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType, currentBitmap.ColorSpace); using var resizedBitmap = SKBitmap.FromImage(SkiaEncoder.ResizeImage(currentBitmap, imageInfo)); // draw this image into the strip at the next position var xPos = x * cellWidth; var yPos = y * cellHeight; canvas.DrawBitmap(resizedBitmap, xPos, yPos); } } return(bitmap); }
/// <summary> /// Gets the next valid image as a bitmap. /// </summary> /// <param name="skiaEncoder">The current skia encoder.</param> /// <param name="paths">The list of image paths.</param> /// <param name="currentIndex">The current checked indes.</param> /// <param name="newIndex">The new index.</param> /// <returns>A valid bitmap, or null if no bitmap exists after <c>currentIndex</c>.</returns> public static SKBitmap?GetNextValidImage(SkiaEncoder skiaEncoder, IReadOnlyList <string> paths, int currentIndex, out int newIndex) { var imagesTested = new Dictionary <int, int>(); SKBitmap?bitmap = null; while (imagesTested.Count < paths.Count) { if (currentIndex >= paths.Count) { currentIndex = 0; } bitmap = skiaEncoder.Decode(paths[currentIndex], false, null, out _); imagesTested[currentIndex] = 0; currentIndex++; if (bitmap != null) { break; } } newIndex = currentIndex; return(bitmap); }
private SKBitmap GetNextValidImage(string[] paths, int currentIndex, out int newIndex) { var imagesTested = new Dictionary <int, int>(); SKBitmap bitmap = null; while (imagesTested.Count < paths.Length) { if (currentIndex >= paths.Length) { currentIndex = 0; } bitmap = SkiaEncoder.Decode(paths[currentIndex], false, _fileSystem, null, out var origin); imagesTested[currentIndex] = 0; currentIndex++; if (bitmap != null) { break; } } newIndex = currentIndex; return(bitmap); }
private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height, string?libraryName) { var bitmap = new SKBitmap(width, height); using var canvas = new SKCanvas(bitmap); canvas.Clear(SKColors.Black); using var backdrop = GetNextValidImage(paths, 0, out _); if (backdrop == null) { return(bitmap); } // resize to the same aspect as the original var backdropHeight = Math.Abs(width * backdrop.Height / backdrop.Width); using var residedBackdrop = SkiaEncoder.ResizeImage(backdrop, new SKImageInfo(width, backdropHeight, backdrop.ColorType, backdrop.AlphaType, backdrop.ColorSpace)); // draw the backdrop canvas.DrawImage(residedBackdrop, 0, 0); // draw shadow rectangle var paintColor = new SKPaint { Color = SKColors.Black.WithAlpha(0x78), Style = SKPaintStyle.Fill }; canvas.DrawRect(0, 0, width, height, paintColor); // draw library name var textPaint = new SKPaint { Color = SKColors.White, Style = SKPaintStyle.Fill, TextSize = 112, TextAlign = SKTextAlign.Center, Typeface = SKTypeface.FromFamilyName("sans-serif", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright), IsAntialias = true }; // scale down text to 90% of the width if text is larger than 95% of the width var textWidth = textPaint.MeasureText(libraryName); if (textWidth > width * 0.95) { textPaint.TextSize = 0.9f * width * textPaint.TextSize / textWidth; } canvas.DrawText(libraryName, width / 2f, (height / 2f) + (textPaint.FontMetrics.XHeight / 2), textPaint); return(bitmap); }
private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height) { var bitmap = new SKBitmap(width, height); using var canvas = new SKCanvas(bitmap); canvas.Clear(SKColors.Black); // number of images used in the thumbnail var iCount = 3; // determine sizes for each image that will composited into the final image var iSlice = Convert.ToInt32(width / iCount); int iHeight = Convert.ToInt32(height * 1.00); int imageIndex = 0; for (int i = 0; i < iCount; i++) { using var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex); imageIndex = newIndex; if (currentBitmap == null) { continue; } // resize to the same aspect as the original int iWidth = Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height); using var resizedImage = SkiaEncoder.ResizeImage(currentBitmap, new SKImageInfo(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType, currentBitmap.ColorSpace)); // crop image int ix = Math.Abs((iWidth - iSlice) / 2); using var subset = resizedImage.Subset(SKRectI.Create(ix, 0, iSlice, iHeight)); // draw image onto canvas canvas.DrawImage(subset ?? resizedImage, iSlice * i, 0); } return(bitmap); }
/// <summary> /// Initializes a new instance of the <see cref="StripCollageBuilder"/> class. /// </summary> /// <param name="skiaEncoder">The encoder to use for building collages.</param> public StripCollageBuilder(SkiaEncoder skiaEncoder) { _skiaEncoder = skiaEncoder; }
private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height, string?libraryName) { var bitmap = new SKBitmap(width, height); using var canvas = new SKCanvas(bitmap); canvas.Clear(SKColors.Black); using var backdrop = GetNextValidImage(paths, 0, out _); if (backdrop == null) { return(bitmap); } // resize to the same aspect as the original var backdropHeight = Math.Abs(width * backdrop.Height / backdrop.Width); using var residedBackdrop = SkiaEncoder.ResizeImage(backdrop, new SKImageInfo(width, backdropHeight, backdrop.ColorType, backdrop.AlphaType, backdrop.ColorSpace)); // draw the backdrop canvas.DrawImage(residedBackdrop, 0, 0); // draw shadow rectangle var paintColor = new SKPaint { Color = SKColors.Black.WithAlpha(0x78), Style = SKPaintStyle.Fill }; canvas.DrawRect(0, 0, width, height, paintColor); var typeFace = SKTypeface.FromFamilyName("sans-serif", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright); // use the system fallback to find a typeface for the given CJK character var nonCjkPattern = @"[^\p{IsCJKUnifiedIdeographs}\p{IsCJKUnifiedIdeographsExtensionA}\p{IsKatakana}\p{IsHiragana}\p{IsHangulSyllables}\p{IsHangulJamo}]"; var filteredName = Regex.Replace(libraryName ?? string.Empty, nonCjkPattern, string.Empty); if (!string.IsNullOrEmpty(filteredName)) { typeFace = SKFontManager.Default.MatchCharacter(null, SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright, null, filteredName[0]); } // draw library name var textPaint = new SKPaint { Color = SKColors.White, Style = SKPaintStyle.Fill, TextSize = 112, TextAlign = SKTextAlign.Center, Typeface = typeFace, IsAntialias = true }; // scale down text to 90% of the width if text is larger than 95% of the width var textWidth = textPaint.MeasureText(libraryName); if (textWidth > width * 0.95) { textPaint.TextSize = 0.9f * width * textPaint.TextSize / textWidth; } canvas.DrawText(libraryName, width / 2f, (height / 2f) + (textPaint.FontMetrics.XHeight / 2), textPaint); return(bitmap); }
/// <summary> /// Initializes a new instance of the <see cref="SplashscreenBuilder"/> class. /// </summary> /// <param name="skiaEncoder">The SkiaEncoder.</param> public SplashscreenBuilder(SkiaEncoder skiaEncoder) { _skiaEncoder = skiaEncoder; }