Indexing.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 convert full color RGB
  20. // to 4 BPP and 8 BPP indexed images.
  21. public class Indexing
  22. {
  23. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  24. {
  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", "Images", "maple.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 = opaque;
  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. using (var ib4bpp = sizedBmp.ToIndexed4bppBitmap(DitheringMethod.JarvisJudiceNinke))
  42. using (var ib8bpp = sizedBmp.ToIndexed8bppBitmap(DitheringMethod.JarvisJudiceNinke))
  43. using (var b4bpp = ib4bpp.ToGcBitmap())
  44. using (var b8bpp = ib8bpp.ToGcBitmap())
  45. {
  46. // Copy the original and indexed images into 4 quadrants of the resulting bitmap:
  47. bmp.BitBlt(sizedBmp, 0, 0);
  48. b4bpp.Opaque = opaque;
  49. bmp.BitBlt(b4bpp, w, 0);
  50. b8bpp.Opaque = opaque;
  51. bmp.BitBlt(b8bpp, 0, h);
  52. bmp.BitBlt(sizedBmp, w, h);
  53. }
  54.  
  55. // Add borders between the quadrants, and captions for each:
  56. var lineh = 2;
  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(foreColor, lineh * 2));
  63. g.DrawLine(0, h, w * 2, h, new GCDRAW.Pen(foreColor, lineh * 2));
  64. var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
  65. g.DrawString(" Original image ", tf, new PointF(0, 0));
  66. g.DrawString(" Indexed 4 bits per pixel ", tf, new PointF(w + lineh, 0));
  67. g.DrawString(" Indexed 8 bits per pixel ", tf, new PointF(0, h + lineh));
  68. g.DrawString(" Original image ", tf, new PointF(w + lineh, h + lineh));
  69. }
  70. }
  71. return bmp;
  72. }
  73. }
  74. }
  75.