MatrixEffects1.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 HueRotationEffect
  20. // to change the hue of an image based on the rotation angle.
  21. // See also MatrixEffects2.
  22. public class MatrixEffects1
  23. {
  24. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  25. {
  26. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  27. using (var origBmp = new GcBitmap())
  28. {
  29. // Load a sample photo:
  30. var imagePath = Path.Combine("Resources", "Images", "lavender.jpg");
  31. using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  32. origBmp.Load(stm);
  33.  
  34. // Resize the original photo so we can place 4 samples of it
  35. // on the resulting bitmap:
  36. int w = pixelSize.Width / 2;
  37. int h = pixelSize.Height / 2;
  38. using (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
  39. {
  40. sizedBmp.Opaque = opaque;
  41. // Copy the resized original into 4 quadrants of the resulting bitmap:
  42. bmp.BitBlt(sizedBmp, 0, 0);
  43. bmp.BitBlt(sizedBmp, w, 0);
  44. bmp.BitBlt(sizedBmp, 0, h);
  45. bmp.BitBlt(sizedBmp, w, h);
  46. }
  47.  
  48. // Moving the 3 lines with the "ApplyEffect" comment from below to here
  49. // will apply the effects only to the photos but not to the texts.
  50.  
  51. // Add borders between the quadrants, and captions for each:
  52. var lineh = 2;
  53. using (var g = bmp.CreateGraphics(null))
  54. {
  55. var foreColor = Color.Yellow;
  56. var backColor = Color.Blue;
  57. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
  58. g.DrawLine(w, 0, w, h * 2, new GCDRAW.Pen(foreColor, lineh * 2));
  59. g.DrawLine(0, h, w * 2, h, new GCDRAW.Pen(foreColor, lineh * 2));
  60. var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
  61. g.DrawString(" Original image ", tf, new PointF(0, 0));
  62. g.DrawString(" HueRotationEffect.Get(120) ", tf, new PointF(w + lineh, 0));
  63. g.DrawString(" HueRotationEffect.Get(180) ", tf, new PointF(0, h + lineh));
  64. g.DrawString(" HueRotationEffect.Get(270) ", tf, new PointF(w + lineh, h + lineh));
  65. }
  66. // ApplyEffect (move this code up to before drawing texts
  67. // to limit it to photos only and not affect the captions).
  68. //
  69. // Keep the pixels in top left quadrant intact,
  70. // apply effects to the other 3 quadrants:
  71. bmp.ApplyEffect(HueRotationEffect.Get(120), new Rectangle(w + lineh, 0, w - lineh, h - lineh));
  72. bmp.ApplyEffect(HueRotationEffect.Get(180), new Rectangle(0, h + lineh, w - lineh, h - lineh));
  73. bmp.ApplyEffect(HueRotationEffect.Get(270), new Rectangle(w + lineh, h + lineh, w - lineh, h - lineh));
  74. }
  75. return bmp;
  76. }
  77. }
  78. }
  79.