Advertisement
Guest User

Untitled

a guest
Oct 17th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. public void DrawText(String text, String path)
  2. {
  3. BitmapImage bi = new BitmapImage();
  4. bi.BeginInit();
  5. bi.UriSource = new Uri(@"E:\Hulk.gif", UriKind.RelativeOrAbsolute);
  6. bi.EndInit();
  7.  
  8. string sText = "This is a text";
  9. int nTextHeight = 60;
  10. DrawingVisual drawingVisual = new DrawingVisual();
  11. using (DrawingContext drawingContext = drawingVisual.RenderOpen())
  12. {
  13. drawingContext.DrawImage(bi, new Rect(0, 0, bi.Width, bi.Height));
  14.  
  15. Rect rect = new Rect(0, bi.Height, bi.Width, nTextHeight);
  16. drawingContext.DrawRectangle(System.Windows.Media.Brushes.LightGray, new Pen(Brushes.Red, 2), rect);
  17.  
  18. var formattedText = new System.Windows.Media.FormattedText(sText, System.Globalization.CultureInfo.InvariantCulture,
  19. FlowDirection.LeftToRight,
  20. new Typeface("Arial"), 12, System.Windows.Media.Brushes.Red);
  21. formattedText.TextAlignment = TextAlignment.Center;
  22. double nX = bi.Width / 2;
  23. double nY = bi.Height + nTextHeight / 2;
  24. System.Windows.Point pt = new System.Windows.Point(nX, nY - formattedText.Height / 2);
  25. drawingContext.DrawText(formattedText, pt);
  26. }
  27. RenderTargetBitmap bmp = new RenderTargetBitmap((int)bi.Width, (int)bi.Height + nTextHeight, 96, 96, PixelFormats.Pbgra32);
  28. bmp.Render(drawingVisual);
  29.  
  30. GifBitmapEncoder gif = new GifBitmapEncoder();
  31. gif.Frames.Add(BitmapFrame.Create(bmp));
  32. using (System.IO.Stream stm = System.IO.File.Create(@"E:\Hulk_2.gif"))
  33. {
  34. gif.Save(stm);
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement