theyugin

Untitled

Feb 10th, 2022
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. public class Caption : ImageUtility
  2. {
  3.     private const string PangoCaptionFormat = "pango:<span font_family=\"Roboto Condensed\" weight=\"bold\">{0}</span>";
  4.     private readonly IMagickImageCollection<byte> _image;
  5.  
  6.     public Caption(byte[] bImage)
  7.     {
  8.         _image = new MagickImageCollection(bImage);
  9.     }
  10.  
  11.     public byte[] Top(string caption)
  12.     {
  13.         var captionWidth = _image.First().Width;
  14.         var captionSettings = new MagickReadSettings
  15.         {
  16.             TextGravity = Gravity.Center,
  17.             BackgroundColor = MagickColors.White,
  18.             Width = captionWidth,
  19.             FontPointsize = captionWidth / 13.0
  20.         };
  21.  
  22.         using (
  23.             var captionImage = new MagickImage(
  24.                 string.Format(PangoCaptionFormat, caption),
  25.                 captionSettings
  26.             )
  27.         )
  28.         {
  29.             captionImage.Extent(
  30.                 new MagickGeometry(
  31.                     new Percentage(100),
  32.                     new Percentage(110)
  33.                 ),
  34.                 Gravity.Center
  35.             );
  36.             captionImage.RePage();
  37.             for (var i = 0; i < _image.Count; i++)
  38.             {
  39.                 var captionedFrameImages = new MagickImageCollection();
  40.                 captionImage.Format = _image[i].Format;
  41.                 captionedFrameImages.Add(captionImage);
  42.                 captionedFrameImages.Add(_image[i]);
  43.                 _image[i] = captionedFrameImages.AppendVertically();
  44.             }
  45.         }
  46.  
  47.         return _image.ToByteArray();
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment