Dithering2.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 dithering methods Stucki, Atkinson, Burkes.
  20. // See also MatrixEffects1 and MatrixEffects2.
  21. public class Dithering2
  22. {
  23. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  24. {
  25. opaque = false;
  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", "Stock", "bw-hiking.jpg");
  31. using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  32. origBmp.Load(stm);
  33.  
  34. origBmp.SetAlphaTo255();
  35. origBmp.Opaque = false;
  36.  
  37. // Resize the original photo so we can place 4 samples of it
  38. // on the resulting bitmap:
  39. int w = pixelSize.Width / 2;
  40. int h = pixelSize.Height / 2;
  41. using (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
  42. {
  43. // Copy the resized original into 4 quadrants of the resulting bitmap:
  44. bmp.BitBlt(sizedBmp, 0, 0);
  45. bmp.BitBlt(sizedBmp, w, 0);
  46. bmp.BitBlt(sizedBmp, 0, h);
  47. bmp.BitBlt(sizedBmp, w, h);
  48. }
  49.  
  50. // Keep the pixels in top left quadrant intact,
  51. // apply effects to the other 3 quadrants:
  52. var lineh = 2;
  53. bmp.ApplyEffect(DitheringEffect.Get(DitheringMethod.Stucki), new Rectangle(w + lineh, 0, w - lineh, h - lineh));
  54. bmp.ApplyEffect(DitheringEffect.Get(DitheringMethod.Atkinson), new Rectangle(0, h + lineh, w - lineh, h - lineh));
  55. bmp.ApplyEffect(DitheringEffect.Get(DitheringMethod.Burkes), new Rectangle(w + lineh, h + lineh, w - lineh, h - lineh));
  56.  
  57. // Add borders between the quadrants, and captions for each:
  58. using (var g = bmp.CreateGraphics(null))
  59. {
  60. var foreColor = Color.Yellow;
  61. var backColor = Color.Blue;
  62. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
  63. g.DrawLine(w, 0, w, h * 2, new GCDRAW.Pen(foreColor, lineh * 2));
  64. g.DrawLine(0, h, w * 2, h, new GCDRAW.Pen(foreColor, lineh * 2));
  65. var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
  66. var th = g.MeasureString("QWERTY", tf).Height;
  67. g.DrawString(" Original image ", tf, new PointF(0, h - th - lineh));
  68. g.DrawString(" Stucki ", tf, new PointF(w + lineh, h - th - lineh));
  69. g.DrawString(" Atkinson ", tf, new PointF(0, h * 2 - th));
  70. g.DrawString(" Burkes ", tf, new PointF(w + lineh, h * 2 - th));
  71. }
  72. }
  73. return bmp;
  74. }
  75. }
  76. }
  77.