RoundCLip.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 make a round clipping of a part of an image
  19. // and render it into a GcBitmap.
  20. public class RoundCLip
  21. {
  22. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  23. {
  24. // Note: we can use Color.Transparent instead of a solid background,
  25. // but the resulting image format must support transparency for this
  26. // to work as expected:
  27. var backColor = Color.FromArgb(unchecked((int)0xff0066cc));
  28. var foreColor = Color.FromArgb(unchecked((int)0xffffcc00));
  29. const int bottom = 144, pad = 36;
  30. int minSize = Math.Min(pixelSize.Width, pixelSize.Height) / 6, maxSize = (int)(Math.Min(pixelSize.Width, pixelSize.Height) / 1.5);
  31.  
  32. // Randomize some parameters of the sample:
  33. var rnd = Common.Util.NewRandom();
  34.  
  35. // It is a good idea to dispose bitmaps which are no longer needed,
  36. // so we are 'using' all bitmaps except the one returned:
  37. using (var bmpSrc = new GcBitmap(Path.Combine("Resources", "Stock", "woman-brick-wall.jpg")))
  38. {
  39. // Make sure source and target opacity match:
  40. bmpSrc.Opaque = opaque;
  41.  
  42. // Coordinates and size of the clipping in the source image:
  43. const int x = 143, y = 0, w = 655, h = 655;
  44.  
  45. // Create a clipping region excluding all outside of the specified circle:
  46. var rgn = new GrapeCity.Documents.Imaging.Region(new RectangularFigure(0, 0, bmpSrc.PixelWidth, bmpSrc.PixelHeight));
  47. var ellipse = new EllipticFigure(x, y, w, h);
  48. rgn.CombineWithRegion(new GrapeCity.Documents.Imaging.Region(ellipse), RegionCombineMode.Exclude, false);
  49.  
  50. // To clip using a Region, we need to use the BitmapRenderer.
  51. // NOTE (new in v2sp2): the renderer is not created by default,
  52. // we must call EnsureRendererCreated() prior to using it:
  53. bmpSrc.EnsureRendererCreated();
  54. var renderer = bmpSrc.Renderer;
  55. renderer.ClipRegion = rgn;
  56. renderer.Clear(Color.Transparent);
  57. var size = rnd.Next(minSize, maxSize);
  58. using (var bmpRound = bmpSrc.Clip(new Rectangle(x, y, w, h)))
  59. using (var bmpSmall = bmpRound.Resize(size, size))
  60. {
  61. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  62. bmp.Clear(Color.Transparent);
  63. bmp.BitBlt(bmpSmall,
  64. rnd.Next(pad, pixelSize.Width - pad - bmpSmall.PixelWidth),
  65. rnd.Next(pad, pixelSize.Height - pad - bottom - bmpSmall.PixelHeight));
  66. return bmp;
  67. }
  68. }
  69. }
  70. }
  71. }
  72.