AutoLevels.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 GcBitmap.AutoLevel()
  20. // to automatically adjust the output levels of an image.
  21. public class AutoLevels
  22. {
  23. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  24. {
  25. opaque = true;
  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", "ImagesBis", "red-yellow-wall.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 to fit two versions on the resulting bitmap:
  35. int w = pixelSize.Width;
  36. int h = pixelSize.Height / 2;
  37. using (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
  38. {
  39. // Copy the resized original into the upper half of the resulting bitmap:
  40. bmp.BitBlt(sizedBmp, 0, 0);
  41. // Auto adjust levels of the original and copy the result into the lower half:
  42. sizedBmp.AutoLevel();
  43. bmp.BitBlt(sizedBmp, 0, h);
  44. }
  45.  
  46. // Add captions (original and adjusted images):
  47. var lineh = 2;
  48. using (var g = bmp.CreateGraphics(null))
  49. {
  50. var foreColor = Color.Yellow;
  51. var backColor = Color.Blue;
  52. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
  53. g.DrawLine(0, h, w * 2, h, new GCDRAW.Pen(Color.Gray, lineh * 2));
  54. var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
  55. var th = g.MeasureString("QWERTY", tf).Height;
  56. g.DrawString(" Original image ", tf, new PointF(0, h - th + lineh));
  57. g.DrawString(" Auto levels applied ", tf, new PointF(0, h * 2 + lineh - th + lineh));
  58. }
  59. }
  60. return bmp;
  61. }
  62. }
  63. }
  64.