PushClip.vb
  1. ''
  2. '' This code is part of Document Solutions for Imaging demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Drawing
  8. Imports GrapeCity.Documents.Text
  9. Imports GrapeCity.Documents.Imaging
  10. Imports GCTEXT = GrapeCity.Documents.Text
  11. Imports GCDRAW = GrapeCity.Documents.Drawing
  12.  
  13. '' This sample demonstrates how to use GcGraphics.PushClip/PopClip
  14. '' methods to specify clipping.
  15. Public Class PushClip
  16. Function GenerateImage(
  17. ByVal pixelSize As Size,
  18. ByVal dpi As Single,
  19. ByVal opaque As Boolean,
  20. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  21.  
  22. Dim backColor = Color.FromArgb(&HFF0066CC)
  23. Dim foreColor = Color.FromArgb(&HFFFFCC00)
  24. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
  25. Dim cw = 400, ch = 300, pad = 10, bord = 4
  26. Dim clipRc = New RectangleF(pixelSize.Width - cw - pad, pad, cw, ch)
  27. Using g = bmp.CreateGraphics(backColor)
  28. '' We create a path consisting of two nested rectangles,
  29. '' outer for the whole bitmap, inner for a text box.
  30. '' We then use that path to create a clip region And
  31. '' draw an image covering the whole bitmap but excluding
  32. '' the text box
  33. Using gpath = g.CreatePath()
  34. gpath.BeginFigure(PointF.Empty)
  35. gpath.AddLine(New PointF(pixelSize.Width, 0))
  36. gpath.AddLine(New PointF(pixelSize.Width, pixelSize.Height))
  37. gpath.AddLine(New PointF(0, pixelSize.Height))
  38. gpath.EndFigure(FigureEnd.Closed)
  39. gpath.BeginFigure(New PointF(clipRc.Left, clipRc.Top))
  40. gpath.AddLine(New PointF(clipRc.Right, clipRc.Top))
  41. gpath.AddLine(New PointF(clipRc.Right, clipRc.Bottom))
  42. gpath.AddLine(New PointF(clipRc.Left, clipRc.Bottom))
  43. gpath.EndFigure(FigureEnd.Closed)
  44. Using cliprgn = g.CreateClipRegion(gpath)
  45. Using img = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "clivia.jpg"))
  46. g.PushClip(cliprgn)
  47. g.DrawImage(
  48. img,
  49. New RectangleF(0, 0, pixelSize.Width, pixelSize.Height),
  50. Nothing,
  51. ImageAlign.StretchImage)
  52. g.PopClip(cliprgn)
  53. End Using
  54. End Using
  55. End Using
  56. '' We now draw some text inside the text box,
  57. '' clipping to it (this overload of PushClip
  58. '' returns an IDisposable that removes the clipping
  59. '' when disposed)
  60. Using (g.PushClip(clipRc))
  61. g.DrawString(
  62. Util.LoremIpsum(),
  63. New TextFormat() With
  64. {
  65. .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  66. .FontSize = 16,
  67. .ForeColor = foreColor
  68. },
  69. clipRc
  70. )
  71. End Using
  72. '' Draw a border around the whole image,
  73. '' demonstrating that the clip has been removed:
  74. g.DrawRectangle(
  75. New RectangleF(bord / 2, bord / 2, pixelSize.Width - bord, pixelSize.Height - bord),
  76. New GCDRAW.Pen(foreColor, bord))
  77. End Using
  78. '' Done
  79. Return bmp
  80. End Function
  81. End Class
  82.