MatrixEffects2.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.IO;
  6. using System.Drawing;
  7. using GrapeCity.Documents.Drawing;
  8. using GrapeCity.Documents.Text;
  9. using GrapeCity.Documents.Imaging;
  10. using GCTEXT = GrapeCity.Documents.Text;
  11. using GCDRAW = GrapeCity.Documents.Drawing;
  12.  
  13. namespace DsImagingWeb.Demos
  14. {
  15. // This sample demonstrates how to use LuminanceToAlphaEffect,
  16. // OpacityEffect and SepiaEffect on an image.
  17. // See also MatrixEffects1.
  18. public class MatrixEffects2
  19. {
  20. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  21. {
  22. opaque = false;
  23. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  24. using (var origBmp = new GcBitmap())
  25. {
  26. // Load a sample photo:
  27. var imagePath = Path.Combine("Resources", "Images", "lavender.jpg");
  28. using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  29. origBmp.Load(stm);
  30.  
  31. origBmp.SetAlphaTo255();
  32. origBmp.Opaque = false;
  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(" LuminanceToAlphaEffect.Get() ", tf, new PointF(w + lineh, 0));
  63. g.DrawString(" OpacityEffect.Get(0.5f) ", tf, new PointF(0, h + lineh));
  64. g.DrawString(" SepiaEffect.Get() ", 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(LuminanceToAlphaEffect.Get(), new Rectangle(w + lineh, 0, w - lineh, h - lineh));
  72. bmp.ApplyEffect(OpacityEffect.Get(0.5f), new Rectangle(0, h + lineh, w - lineh, h - lineh));
  73. bmp.ApplyEffect(SepiaEffect.Get(), new Rectangle(w + lineh, h + lineh, w - lineh, h - lineh));
  74. }
  75. return bmp;
  76. }
  77. }
  78. }
  79.