PicEffTransparentColor.cs
  1. //
  2. // This code is part of Document Solutions for Word 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 GrapeCity.Documents.Word;
  11.  
  12. namespace DsWordWeb.Demos
  13. {
  14. // Set transparent color picture in picture.
  15. public class PicEffTransparentColor
  16. {
  17. public GcWordDocument CreateDocx()
  18. {
  19. var doc = new GcWordDocument();
  20. Util.SetNarrowMargins(doc);
  21. var captionStyle = doc.Styles.Add("CaptionStyle", doc.Styles[BuiltInStyleId.BodyTextFirstIndent2]);
  22.  
  23. // Read image from a file:
  24. var bytes = File.ReadAllBytes(Path.Combine("Resources", "Images", "wargravepink.jpg"));
  25. // Original picture:
  26. doc.Body.AddParagraph().AddRun().AddPicture(bytes, @"image/jpeg", 450, 300);
  27. doc.Body.AddParagraph("Original picture", captionStyle);
  28. // Set black to transparent:
  29. var picture = doc.Body.AddParagraph().AddRun().AddPicture(bytes, @"image/jpeg", 450, 300);
  30. picture.ImageData.SetColorTransparent(new UserColor(Color.Black));
  31. // To show that parts of the picture are now transparent, fill the background with confetti:
  32. picture.Fill.Type = FillType.Pattern;
  33. picture.Fill.PatternFill.Type = PatternFillType.LargeConfetti;
  34. picture.Fill.PatternFill.BackColor.RGB = Color.LightYellow;
  35. picture.Fill.PatternFill.ForeColor.RGB = Color.Red;
  36. doc.Body.AddParagraph($"Black is transparent, with confetti fill", captionStyle);
  37. // Done:
  38. return doc;
  39. }
  40. }
  41. }
  42.