Watermark.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 add a text watermark
  20. // to an image. The image is rendered using its native
  21. // resolution on a GcBitmap, then the watermark text
  22. // is drawn on top using a semitransparent color.
  23. // The resulting bitmap with the added watermark
  24. // can be saved to any of the supported image formats.
  25. public class Watermark
  26. {
  27. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  28. {
  29. var Inch = dpi;
  30. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  31. using (var g = bmp.CreateGraphics(Color.LightGray))
  32. using (var image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "reds.jpg")))
  33. {
  34. var rc = new RectangleF((pixelSize.Width - image.Width) / 2, (pixelSize.Height - image.Height) / 2, image.Width, image.Height);
  35. g.DrawImage(image, rc, null, ImageAlign.Default);
  36.  
  37. g.DrawString(
  38. "Watermark",
  39. new TextFormat()
  40. {
  41. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibrib.ttf")),
  42. FontSize = Inch,
  43. ForeColor = Color.FromArgb(128, Color.Yellow),
  44.  
  45. },
  46. rc, TextAlignment.Center, ParagraphAlignment.Center, false);
  47. }
  48. return bmp;
  49. }
  50. }
  51. }
  52.