GaussianBlur.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.  
  17. namespace DsImagingWeb.Demos
  18. {
  19. // This sample demonstrates how to use the GaussianBlurEffect.
  20. public class GaussianBlur
  21. {
  22. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  23. {
  24. opaque = false;
  25. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  26. using (var origBmp = new GcBitmap())
  27. {
  28. // Load a sample photo:
  29. var imagePath = Path.Combine("Resources", "ImagesBis", "butterfly.jpg");
  30. using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  31. origBmp.Load(stm);
  32.  
  33. origBmp.SetAlphaTo255();
  34. origBmp.Opaque = false;
  35.  
  36. // Resize the original photo so we can place 4 samples of it
  37. // on the resulting bitmap:
  38. int w = pixelSize.Width / 2;
  39. int h = pixelSize.Height / 2;
  40. using (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
  41. {
  42. // Copy the resized original into 4 quadrants of the resulting bitmap:
  43. bmp.BitBlt(sizedBmp, 0, 0);
  44. bmp.BitBlt(sizedBmp, w, 0);
  45. bmp.BitBlt(sizedBmp, 0, h);
  46. bmp.BitBlt(sizedBmp, w, h);
  47. }
  48.  
  49. // Keep the pixels in top left quadrant intact,
  50. // apply effects to the other 3 quadrants:
  51. var lineh = 2;
  52. bmp.ApplyEffect(GaussianBlurEffect.Get(), new Rectangle(w + lineh, 0, w - lineh, h - lineh));
  53. bmp.ApplyEffect(GaussianBlurEffect.Get(Color.Blue, 4, GaussianBlurBorderMode.Bounce), new Rectangle(0, h + lineh, w - lineh, h - lineh));
  54. bmp.ApplyEffect(GaussianBlurEffect.Get(Color.Blue, 16, GaussianBlurBorderMode.Mirror), new Rectangle(w + lineh, h + lineh, w - lineh, h - lineh));
  55.  
  56. // Add borders between the quadrants, and captions for each:
  57. using (var g = bmp.CreateGraphics(null))
  58. {
  59. var foreColor = Color.Yellow;
  60. var backColor = Color.Blue;
  61. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
  62. g.DrawLine(w, 0, w, h * 2, new GCDRAW.Pen(Color.Gray, lineh * 2));
  63. g.DrawLine(0, h, w * 2, h, new GCDRAW.Pen(Color.Gray, lineh * 2));
  64. var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
  65. var th = g.MeasureString("QWERTY", tf).Height;
  66. g.DrawString(" Original image ", tf, new PointF(0, h - th + lineh));
  67. g.DrawString(" Default Gaussian blur ", tf, new PointF(w + lineh, h - th + lineh));
  68. g.DrawString(" Radius 5px, bounce border ", tf, new PointF(0, h * 2 + lineh - th + lineh));
  69. g.DrawString(" Radius 16px, mirror border ", tf, new PointF(w + lineh, h * 2 + lineh - th + lineh));
  70. }
  71. }
  72. return bmp;
  73. }
  74. }
  75. }
  76.