RoundCLip.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 System.Collections.Generic
  8. Imports System.Linq
  9. Imports System.Numerics
  10. Imports GrapeCity.Documents.Drawing
  11. Imports GrapeCity.Documents.Text
  12. Imports GrapeCity.Documents.Imaging
  13.  
  14. '' This sample demonstrates how to make a round clipping of a part of an image
  15. '' and render it into a GcBitmap.
  16. Public Class RoundCLip
  17. Function GenerateImage(
  18. ByVal pixelSize As Size,
  19. ByVal dpi As Single,
  20. ByVal opaque As Boolean,
  21. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  22.  
  23. '' Note: we can use Color.Transparent instead of a solid background,
  24. '' but the resulting image format must support transparency for this
  25. '' to work as expected:
  26. Dim backColor = Color.FromArgb(&HFF0066CC)
  27. Dim foreColor = Color.FromArgb(&HFFFFCC00)
  28. Const bottom = 144, pad = 36
  29. Dim minSize = Math.Min(pixelSize.Width, pixelSize.Height) / 6, maxSize = (Math.Min(pixelSize.Width, pixelSize.Height) / 1.5)
  30.  
  31. '' Randomize some parameters of the sample:
  32. Dim rnd = Util.NewRandom()
  33.  
  34. '' It is a good idea to dispose bitmaps which are no longer needed,
  35. '' so we are 'using' all bitmaps except the one returned:
  36. Using bmpSrc = New GcBitmap(Path.Combine("Resources", "Stock", "woman-brick-wall.jpg"))
  37. '' Make sure source and target opacity match:
  38. bmpSrc.Opaque = opaque
  39.  
  40. '' Coordinates and size of the clipping in the source image:
  41. Const x = 143, y = 0, w = 655, h = 655
  42.  
  43. '' Create a clipping region excluding all outside of the specified circle:
  44. Dim rgn = New GrapeCity.Documents.Imaging.Region(New RectangularFigure(0, 0, bmpSrc.PixelWidth, bmpSrc.PixelHeight))
  45. Dim ellipse = New EllipticFigure(x, y, w, h)
  46. rgn.CombineWithRegion(New GrapeCity.Documents.Imaging.Region(ellipse), RegionCombineMode.Exclude, False)
  47.  
  48. '' To clip using a Region, we need to use the BitmapRenderer:
  49. '' NOTE (New in v2sp2) the renderer Is Not created by Default,
  50. '' we must call EnsureRendererCreated() prior to using it:
  51. bmpSrc.EnsureRendererCreated()
  52. Dim renderer = bmpSrc.Renderer
  53. renderer.ClipRegion = rgn
  54. renderer.Clear(Color.Transparent)
  55. Dim size = rnd.Next(minSize, maxSize)
  56. Using bmpRound = bmpSrc.Clip(New Rectangle(x, y, w, h))
  57. Using bmpSmall = bmpRound.Resize(size, size)
  58. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
  59. bmp.Clear(Color.Transparent)
  60. bmp.BitBlt(bmpSmall,
  61. rnd.Next(pad, pixelSize.Width - pad - bmpSmall.PixelWidth),
  62. rnd.Next(pad, pixelSize.Height - pad - bottom - bmpSmall.PixelHeight))
  63. Return bmp
  64. End Using
  65. End Using
  66. End Using
  67. End Function
  68. End Class
  69.