ImageTransparency.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Pdf
  8. Imports GrapeCity.Documents.Text
  9. Imports GrapeCity.Documents.Drawing
  10. Imports GCTEXT = GrapeCity.Documents.Text
  11. Imports GCDRAW = GrapeCity.Documents.Drawing
  12.  
  13. '' This sample demonstrates the ability of DsPdf to render
  14. '' images Imports a specified transparency (opacity).
  15. Public Class ImageTransparency
  16. Function CreatePDF(ByVal stream As Stream) As Integer
  17. Dim doc = New GcPdfDocument()
  18. Dim page = doc.NewPage()
  19. Dim g = page.Graphics
  20.  
  21. Dim rc = Util.AddNote(
  22. "GcPdfGraphics.DrawImage() method allows rendering images with a specified opacity. " +
  23. "Below is a random text with an image drawn on top of it using opacity 0.2 (almost transparent), " +
  24. "0.5 (medium transparency) and 1 (non-transparent).",
  25. page)
  26.  
  27. Dim tl = g.CreateTextLayout()
  28. tl.DefaultFormat.Font = StandardFonts.Times
  29. tl.DefaultFormat.FontSize = 12
  30. tl.MaxWidth = page.Size.Width
  31. tl.MaxHeight = page.Size.Height
  32. tl.MarginAll = 36
  33. tl.MarginTop += rc.Bottom
  34. tl.Append(Util.LoremIpsum(2))
  35. tl.PerformLayout(True)
  36. g.DrawTextLayout(tl, PointF.Empty)
  37.  
  38. Using img As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "puffins.jpg"))
  39. Dim imageRc = New RectangleF(tl.MarginLeft, tl.MarginTop, 144, 144)
  40. '' Opacity 0.2:
  41. g.DrawImage(img, imageRc, Nothing, ImageAlign.ScaleImage, 0.2F)
  42. imageRc.Offset(imageRc.Width + 36, 0)
  43. '' Opacity 0.5:
  44. g.DrawImage(img, imageRc, Nothing, ImageAlign.ScaleImage, 0.5F)
  45. imageRc.Offset(imageRc.Width + 36, 0)
  46. '' Opacity 1 (default):
  47. g.DrawImage(img, imageRc, Nothing, ImageAlign.ScaleImage)
  48.  
  49. '' NOTE: we must save document BEFORE disposing the image(s) used in it:
  50. doc.Save(stream)
  51. End Using
  52. Return doc.Pages.Count
  53. End Function
  54. End Class
  55.