Watermark2.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Numerics;
  11. using GrapeCity.Documents.Drawing;
  12. using GrapeCity.Documents.Text;
  13. using GrapeCity.Documents.Imaging;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsImagingWeb.Demos
  18. {
  19. // This sample demonstrates how to render a watermark on an image
  20. // at an angle using a rotation transformation on the bitmap graphics.
  21. public class Watermark2
  22. {
  23. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  24. {
  25. // Note: we can use Color.Transparent instead of a solid background,
  26. // but the resulting image format must support transparency for this
  27. // to work as expected:
  28. var backColor = Color.FromArgb(unchecked((int)0xff0066cc));
  29. var foreColor = Color.FromArgb(unchecked((int)0xffffcc00));
  30. float angle = -30;
  31. var rad = (float)(angle * Math.PI) / 180f;
  32.  
  33. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  34. using (var bmpSrc = new GcBitmap(Path.Combine("Resources", "ImagesBis", "alpamayo-sq.jpg")))
  35. {
  36. // BitBlt requires the opacity of both images to be the same:
  37. bmpSrc.Opaque = opaque;
  38. // Render source image onto the target bitmap
  39. // (generally we might want to resize the source image first,
  40. // but in this case we just know that the source image has
  41. // the same size as the target, so skip this step):
  42. bmp.BitBlt(bmpSrc, 0, 0);
  43. }
  44.  
  45. using (var g = bmp.CreateGraphics())
  46. {
  47. // Draw watermark text in a loop over all image at an angle:
  48. g.Transform = Matrix3x2.CreateRotation((float)(angle * Math.PI) / 180f, new Vector2(pixelSize.Width / 2, pixelSize.Height / 2));
  49. var tf = new TextFormat()
  50. {
  51. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibrib.ttf")),
  52. FontSize = 14,
  53. ForeColor = Color.FromArgb(64, Color.White),
  54.  
  55. };
  56. var tl = g.CreateTextLayout();
  57. tl.Append("Copyright (c) MESCIUS", tf);
  58. tl.PerformLayout(true);
  59. int dx = (int)tl.ContentWidth * 3;
  60. int dy = (int)tl.ContentHeight * 5;
  61. int n = 0;
  62. int offX = -(int)(Math.Cos(rad) * pixelSize.Height / 2); ;
  63. int offY = (int)(Math.Sin(rad) * pixelSize.Width / 2);
  64. for (float y = offY; y < pixelSize.Height - offY; y += dy, ++n)
  65. for (float x = offX + dx / 2 * (n % 2); x < pixelSize.Width - offX; x += dx)
  66. g.DrawTextLayout(tl, new PointF(x, y));
  67. }
  68. return bmp;
  69. }
  70. }
  71. }
  72.