GaussianBlur2.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 System.Numerics;
  11. using GrapeCity.Documents.Drawing;
  12. using GrapeCity.Documents.Text;
  13. using GrapeCity.Documents.Imaging;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16. using DsImagingWeb.Demos.Common;
  17.  
  18. namespace DsImagingWeb.Demos
  19. {
  20. // This example uses the GaussianBlurEffect to draw semi-transparent colored circles ('filters'),
  21. // blurring the portion of the image behind them.
  22. public class GaussianBlur2
  23. {
  24. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  25. {
  26. var colors = new Color[]
  27. {
  28. Color.Fuchsia,
  29. Color.Orange,
  30. Color.Black,
  31. Color.Blue,
  32. Color.Red,
  33. Color.Yellow,
  34. Color.White,
  35. Color.Green,
  36. Color.CornflowerBlue
  37. };
  38. colors.Shuffle();
  39.  
  40. var imagePath = Path.Combine("Resources", "ImagesBis", "bahamas.jpg");
  41. var bmp = new GcBitmap(imagePath);
  42.  
  43. int xx = bmp.PixelWidth / 3;
  44. int yy = bmp.PixelHeight / 3;
  45. int maxr = Math.Min(xx / 2, yy / 2);
  46. var rnd = Util.NewRandom();
  47.  
  48. using var bmpBk = bmp.Clone();
  49.  
  50. // Apply blur to a copy of the original bitmap:
  51. bmpBk.ApplyEffect(GaussianBlurEffect.Get(24, GaussianBlurBorderMode.RepeatEdge));
  52.  
  53. using var g = bmp.CreateGraphics();
  54. var renderer = g.Renderer;
  55. for (int i = 0; i < 9; i++)
  56. {
  57. int r = rnd.Next(maxr / 3, maxr);
  58. var center = new PointF(rnd.Next(r, xx - r) + i % 3 * xx + 1, rnd.Next(r, yy - r) + i / 3 * yy + 1);
  59. var rect = new RectangleF(center.X - r, center.Y - r, r + r, r + r);
  60. var color = colors[i];
  61.  
  62. // Use the blurred background to fill circles:
  63. renderer.BackgroundBitmap = bmpBk;
  64. g.FillEllipse(rect, Color.FromArgb(96, color));
  65.  
  66. // Draw borders normally to hide the artifacts:
  67. renderer.BackgroundBitmap = null;
  68. g.DrawEllipse(rect, new GCDRAW.Pen(color, 2));
  69. }
  70.  
  71. return bmp;
  72. }
  73. }
  74. }
  75.