PDFBitmap.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. http://www.cgsoso.com/forum-211-1.html
  3. CG搜搜 Unity3d 每日Unity3d插件免费更新 更有VIP资源!
  4. CGSOSO 主打游戏开发,影视设计等CG资源素材。
  5. 插件如若商用,请务必官网购买!
  6. daily assets update for try.
  7. U should buy the asset from home store if u use it in your project!
  8. */
  9. using System;
  10. using System.Runtime.InteropServices;
  11. namespace Paroxe.PdfRenderer.Internal
  12. {
  13. public class PDFBitmap : IDisposable
  14. {
  15. private bool m_Disposed;
  16. private IntPtr m_NativePointer;
  17. private readonly int m_Width;
  18. private readonly int m_Height;
  19. private readonly bool m_UseAlphaChannel;
  20. public PDFBitmap(int width, int height, bool useAlphaChannel)
  21. {
  22. PDFLibrary.AddRef("PDFBitmap");
  23. m_Width = width;
  24. m_Height = height;
  25. m_UseAlphaChannel = useAlphaChannel;
  26. m_NativePointer = FPDFBitmap_Create(m_Width, m_Height, useAlphaChannel);
  27. }
  28. ~PDFBitmap()
  29. {
  30. Dispose(false);
  31. }
  32. public void Dispose()
  33. {
  34. Dispose(true);
  35. GC.SuppressFinalize(this);
  36. }
  37. protected virtual void Dispose(bool disposing)
  38. {
  39. if (!m_Disposed)
  40. {
  41. lock (PDFLibrary.nativeLock)
  42. {
  43. if (m_NativePointer != IntPtr.Zero)
  44. FPDFBitmap_Destroy(m_NativePointer);
  45. m_NativePointer = IntPtr.Zero;
  46. }
  47. PDFLibrary.RemoveRef("PDFBitmap");
  48. m_Disposed = true;
  49. }
  50. }
  51. public int Width
  52. {
  53. get { return m_Width; }
  54. }
  55. public int Height
  56. {
  57. get { return m_Height; }
  58. }
  59. public bool UseAlphaChannel
  60. {
  61. get { return m_UseAlphaChannel; }
  62. }
  63. public IntPtr NativePointer
  64. {
  65. get { return m_NativePointer; }
  66. }
  67. public bool HasSameSize(PDFBitmap other)
  68. {
  69. return (m_Width == other.m_Width && m_Height == other.m_Height);
  70. }
  71. public bool HasSameSize(int width, int height)
  72. {
  73. return (m_Width == width && m_Height == height);
  74. }
  75. public void FillRect(int left, int top, int width, int height, int color)
  76. {
  77. FPDFBitmap_FillRect(m_NativePointer, left, top, width, height, color);
  78. }
  79. public IntPtr GetBuffer()
  80. {
  81. return FPDFBitmap_GetBuffer(m_NativePointer);
  82. }
  83. public int GetStride()
  84. {
  85. return FPDFBitmap_GetStride(m_NativePointer);
  86. }
  87. #region NATIVE
  88. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  89. private static extern IntPtr FPDFBitmap_Create(int width, int height, bool alpha);
  90. //[DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  91. //private static extern IntPtr FPDFBitmap_CreateEx(int width, int height, int format, IntPtr firstScan, int stride);
  92. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  93. private static extern void FPDFBitmap_Destroy(IntPtr bitmap);
  94. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  95. private static extern void FPDFBitmap_FillRect(IntPtr bitmap, int left, int top, int width, int height, int color);
  96. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  97. private static extern IntPtr FPDFBitmap_GetBuffer(IntPtr bitmap);
  98. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  99. private static extern int FPDFBitmap_GetStride(IntPtr bitmap);
  100. #endregion
  101. }
  102. }