private SKBitmap BuildSquareCollageBitmap(IReadOnlyList <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 = SkiaHelper.GetNextValidImage(_skiaEncoder, 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); }
private SKBitmap BuildThumbCollageBitmap(IReadOnlyList <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 = SkiaHelper.GetNextValidImage(_skiaEncoder, 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 using 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 using 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> /// Generates a collage of posters and landscape pictures. /// </summary> /// <param name="posters">The poster paths.</param> /// <param name="backdrops">The landscape paths.</param> /// <returns>The created collage as a bitmap.</returns> private SKBitmap GenerateCollage(IReadOnlyList <string> posters, IReadOnlyList <string> backdrops) { var posterIndex = 0; var backdropIndex = 0; var bitmap = new SKBitmap(WallWidth, WallHeight); using var canvas = new SKCanvas(bitmap); canvas.Clear(SKColors.Black); int posterHeight = WallHeight / 6; for (int i = 0; i < Rows; i++) { int imageCounter = Random.Shared.Next(0, 5); int currentWidthPos = i * 75; int currentHeight = i * (posterHeight + Spacing); while (currentWidthPos < WallWidth) { SKBitmap?currentImage; switch (imageCounter) { case 0: case 2: case 3: currentImage = SkiaHelper.GetNextValidImage(_skiaEncoder, posters, posterIndex, out int newPosterIndex); posterIndex = newPosterIndex; break; default: currentImage = SkiaHelper.GetNextValidImage(_skiaEncoder, backdrops, backdropIndex, out int newBackdropIndex); backdropIndex = newBackdropIndex; break; } if (currentImage == null) { throw new ArgumentException("Not enough valid pictures provided to create a splashscreen!"); } // resize to the same aspect as the original var imageWidth = Math.Abs(posterHeight * currentImage.Width / currentImage.Height); using var resizedBitmap = new SKBitmap(imageWidth, posterHeight); currentImage.ScalePixels(resizedBitmap, SKFilterQuality.High); // draw on canvas canvas.DrawBitmap(resizedBitmap, currentWidthPos, currentHeight); currentWidthPos += imageWidth + Spacing; currentImage.Dispose(); if (imageCounter >= 4) { imageCounter = 0; } else { imageCounter++; } } } return(bitmap); }