SwapColors.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 System.Numerics
  10. Imports GrapeCity.Documents.Drawing
  11. Imports GrapeCity.Documents.Text
  12. Imports GrapeCity.Documents.Imaging
  13.  
  14. '' This sample demonstrates how to swap color channels on an image
  15. '' using pixel access. Here we swap red and blue channels to
  16. '' change the way the image looks.
  17. Public Class SwapColors
  18. Function GenerateImage(
  19. ByVal pixelSize As Size,
  20. ByVal dpi As Single,
  21. ByVal opaque As Boolean,
  22. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  23.  
  24. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
  25. Using bmpSrc = New GcBitmap(Path.Combine("Resources", "ImagesBis", "alpamayo-sq.jpg"))
  26. '' BitBlt requires the opacity of both images to be the same:
  27. bmpSrc.Opaque = opaque
  28. '' Render source image onto the target bitmap
  29. '' (generally we might want to resize the source image first,
  30. '' but in this case we just know that the source image has
  31. '' the same size as the target, so skip this step):
  32. bmp.BitBlt(bmpSrc, 0, 0)
  33.  
  34. Using g = bmp.CreateGraphics()
  35. For i = 0 To bmp.PixelWidth - 1
  36. For j = 0 To bmp.PixelHeight - 1
  37. Dim px = bmp(i, j)
  38. px = ((px And &HFFUI) << 16) Or ((px And &HFF0000UI) >> 16) Or (px And &HFF00FF00UI)
  39. bmp(i, j) = px
  40. Next
  41. Next
  42. '' Draw the original image in the bottom right corner for reference:
  43. Dim sx = pixelSize.Width / 3, sy = pixelSize.Height / 3
  44. g.DrawImage(bmpSrc, New RectangleF(sx * 2, sy * 2, sx, sy), Nothing, ImageAlign.StretchImage)
  45. End Using
  46. End Using
  47. Return bmp
  48. End Function
  49. End Class
  50.