GetContentRect.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 GrapeCity.Documents.Drawing;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Imaging;
  13.  
  14. namespace DsImagingWeb.Demos
  15. {
  16. // This sample demonstrates the use of GcBitmap.GetContentRect() method
  17. // that allows trimming single colored margins off an image.
  18. public class GetContentRect
  19. {
  20. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  21. {
  22. // The original goldfish image has white background with margins around the actual fish:
  23. var origImagePath = Path.Combine("Resources", "Stock", "goldfish.jpg");
  24.  
  25. // Create the resulting bitmap:
  26. var targetBmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  27. using (var origBmp = new GcBitmap())
  28. {
  29. // Load the goldfish image:
  30. using (var stm = new FileStream(origImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  31. origBmp.Load(stm);
  32. // Create graphics on the resulting bitmap, filling it with blue color:
  33. using (var g = targetBmp.CreateGraphics(Color.FromArgb(unchecked((int)0xff004d99))))
  34. {
  35. // Render the goldfish on white background:
  36. targetBmp.BitBlt(origBmp, 0, 0);
  37. // The GetContentRect() method returns the area of the image without
  38. // margins of the specified color (white in this case):
  39. var rc = origBmp.GetContentRect(Color.White);
  40. // Draw a red border on the content rectangle:
  41. g.DrawRectangle(rc, Color.Red);
  42. }
  43. }
  44. return targetBmp;
  45. }
  46. }
  47. }
  48.