PushClip.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 GrapeCity.Documents.Drawing;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Imaging;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15.  
  16. namespace DsImagingWeb.Demos
  17. {
  18. // This sample demonstrates how to use GcGraphics.PushClip/PopClip
  19. // methods to clip drawing operations to an arbitrary region.
  20. public class PushClip
  21. {
  22. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  23. {
  24. var backColor = Color.LightGray;
  25. var foreColor = Color.DarkRed;
  26. float cw = 400, ch = 300, pad = 40, bord = 4, textpad = 10;
  27. var clipRc = new RectangleF(pad, pixelSize.Height - ch - pad, cw, ch);
  28. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  29. using (var g = bmp.CreateGraphics(backColor))
  30. {
  31. // We create a path consisting of two nested rectangles,
  32. // outer for the whole bitmap, inner for a text box.
  33. // We then use that path to create a clip region and
  34. // draw an image covering the whole bitmap but excluding
  35. // the text box:
  36. using (var gpath = g.CreatePath())
  37. {
  38. gpath.BeginFigure(PointF.Empty);
  39. gpath.AddLine(new PointF(pixelSize.Width, 0));
  40. gpath.AddLine(new PointF(pixelSize.Width, pixelSize.Height));
  41. gpath.AddLine(new PointF(0, pixelSize.Height));
  42. gpath.EndFigure(FigureEnd.Closed);
  43. gpath.BeginFigure(new PointF(clipRc.Left, clipRc.Top));
  44. gpath.AddLine(new PointF(clipRc.Right, clipRc.Top));
  45. gpath.AddLine(new PointF(clipRc.Right, clipRc.Bottom));
  46. gpath.AddLine(new PointF(clipRc.Left, clipRc.Bottom));
  47. gpath.EndFigure(FigureEnd.Closed);
  48. using (var cliprgn = g.CreateClipRegion(gpath))
  49. using (var img = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "clivia.jpg")))
  50. {
  51. g.PushClip(cliprgn);
  52. g.DrawImage(
  53. img,
  54. new RectangleF(0, 0, pixelSize.Width, pixelSize.Height),
  55. null,
  56. ImageAlign.StretchImage);
  57. g.PopClip(cliprgn);
  58. }
  59. }
  60. // We now draw some text inside the text box,
  61. // clipping to it (this overload of PushClip
  62. // returns an IDisposable that removes the clipping
  63. // when disposed):
  64. using (g.PushClip(clipRc))
  65. {
  66. clipRc.Inflate(-textpad, -textpad);
  67. g.DrawString(
  68. Common.Util.LoremIpsum(1, 3, 4, 10, 20),
  69. new TextFormat()
  70. {
  71. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  72. FontSize = 16,
  73. ForeColor = foreColor
  74. },
  75. clipRc
  76. );
  77. }
  78. // Draw a border around the whole image,
  79. // demonstrating that the clip has been removed:
  80. g.DrawRectangle(
  81. new RectangleF(bord / 2, bord / 2, pixelSize.Width - bord, pixelSize.Height - bord),
  82. new GCDRAW.Pen(foreColor, bord));
  83. }
  84. return bmp;
  85. }
  86. }
  87. }
  88.