GetContentRect.vb
  1. ''
  2. '' This code is part of Document Solutions for Imaging demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Collections.Generic
  8. Imports System.Linq
  9. Imports GrapeCity.Documents.Drawing
  10. Imports GrapeCity.Documents.Text
  11. Imports GrapeCity.Documents.Imaging
  12.  
  13. '' This sample demonstrates the use of GcBitmap.GetContentRect() method
  14. '' that allows you to trim single colored margins off an image.
  15. Public Class GetContentRect
  16. Function GenerateImage(
  17. ByVal pixelSize As Size,
  18. ByVal dpi As Single,
  19. ByVal opaque As Boolean,
  20. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  21.  
  22. '' The original goldfish image has white background with margins around the actual fish:
  23. Dim origImagePath = Path.Combine("Resources", "Stock", "goldfish.jpg")
  24.  
  25. '' Create the resulting bitmap:
  26. Dim targetBmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
  27. Using origBmp = New GcBitmap()
  28. '' Load the goldfish image:
  29. Using stm = New FileStream(origImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
  30. origBmp.Load(stm)
  31. End Using
  32. '' Create graphics on the resulting bitmap, filling it with blue color:
  33. Using g = targetBmp.CreateGraphics(Color.FromArgb(&HFF004D99))
  34. '' Render the goldfish on white background:
  35. targetBmp.BitBlt(origBmp, 0, 0)
  36. '' The GetContentRect() method returns the area of the image without
  37. '' margins of the specified color (white in this case):
  38. Dim rc = origBmp.GetContentRect(Color.White)
  39. '' Draw a red border on the content rectangle:
  40. g.DrawRectangle(rc, Color.Red)
  41. End Using
  42. End Using
  43. Return targetBmp
  44. End Function
  45. End Class
  46.