CefImage.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Container for a single image represented at different scale factors. All
  10. /// image representations should be the same size in density independent pixel
  11. /// (DIP) units. For example, if the image at scale factor 1.0 is 100x100 pixels
  12. /// then the image at scale factor 2.0 should be 200x200 pixels -- both images
  13. /// will display with a DIP size of 100x100 units. The methods of this class can
  14. /// be called on any browser process thread.
  15. /// </summary>
  16. public sealed unsafe partial class CefImage
  17. {
  18. /// <summary>
  19. /// Create a new CefImage. It will initially be empty. Use the Add*() methods
  20. /// to add representations at different scale factors.
  21. /// </summary>
  22. public static CefImage CreateImage()
  23. {
  24. return CefImage.FromNative(
  25. cef_image_t.create()
  26. );
  27. }
  28. /// <summary>
  29. /// Returns true if this Image is empty.
  30. /// </summary>
  31. public bool IsEmpty
  32. {
  33. get { return cef_image_t.is_empty(_self) != 0; }
  34. }
  35. /// <summary>
  36. /// Returns true if this Image and |that| Image share the same underlying
  37. /// storage. Will also return true if both images are empty.
  38. /// </summary>
  39. public bool IsSame(CefImage that)
  40. {
  41. if (that == null) return false;
  42. return cef_image_t.is_same(_self, that.ToNative()) != 0;
  43. }
  44. /// <summary>
  45. /// Add a bitmap image representation for |scale_factor|. Only 32-bit RGBA/BGRA
  46. /// formats are supported. |pixel_width| and |pixel_height| are the bitmap
  47. /// representation size in pixel coordinates. |pixel_data| is the array of
  48. /// pixel data and should be |pixel_width| x |pixel_height| x 4 bytes in size.
  49. /// |color_type| and |alpha_type| values specify the pixel format.
  50. /// </summary>
  51. public bool AddBitmap(float scaleFactor, int pixelWidth, int pixelHeight, CefColorType colorType, CefAlphaType alphaType, IntPtr pixelData, int pixelDataSize)
  52. {
  53. if (pixelData == IntPtr.Zero) throw new ArgumentNullException(nameof(pixelData));
  54. if (pixelDataSize < 0) throw new ArgumentOutOfRangeException(nameof(pixelDataSize));
  55. var n_result = cef_image_t.add_bitmap(_self, scaleFactor, pixelWidth, pixelHeight, colorType, alphaType, (void*)pixelData, (UIntPtr)pixelDataSize);
  56. return n_result != 0;
  57. }
  58. /// <summary>
  59. /// Add a PNG image representation for |scale_factor|. |png_data| is the image
  60. /// data of size |png_data_size|. Any alpha transparency in the PNG data will
  61. /// be maintained.
  62. /// </summary>
  63. public bool AddPng(float scaleFactor, IntPtr pngData, int pngDataSize)
  64. {
  65. if (pngData == IntPtr.Zero) throw new ArgumentNullException(nameof(pngData));
  66. if (pngDataSize < 0) throw new ArgumentOutOfRangeException(nameof(pngDataSize));
  67. var n_result = cef_image_t.add_png(_self, scaleFactor, (void*)pngData, (UIntPtr)pngDataSize);
  68. return n_result != 0;
  69. }
  70. /// <summary>
  71. /// Create a JPEG image representation for |scale_factor|. |jpeg_data| is the
  72. /// image data of size |jpeg_data_size|. The JPEG format does not support
  73. /// transparency so the alpha byte will be set to 0xFF for all pixels.
  74. /// </summary>
  75. public bool AddJpeg(float scaleFactor, IntPtr jpegData, int jpegDataSize)
  76. {
  77. if (jpegData == IntPtr.Zero) throw new ArgumentNullException(nameof(jpegData));
  78. if (jpegDataSize < 0) throw new ArgumentOutOfRangeException(nameof(jpegDataSize));
  79. var n_result = cef_image_t.add_png(_self, scaleFactor, (void*)jpegData, (UIntPtr)jpegDataSize);
  80. return n_result != 0;
  81. }
  82. /// <summary>
  83. /// Returns the image width in density independent pixel (DIP) units.
  84. /// </summary>
  85. public int Width
  86. {
  87. get
  88. {
  89. var n_result = cef_image_t.get_width(_self);
  90. return checked((int)n_result);
  91. }
  92. }
  93. /// <summary>
  94. /// Returns the image height in density independent pixel (DIP) units.
  95. /// </summary>
  96. public int Height
  97. {
  98. get
  99. {
  100. var n_result = cef_image_t.get_height(_self);
  101. return checked((int)n_result);
  102. }
  103. }
  104. /// <summary>
  105. /// Returns true if this image contains a representation for |scale_factor|.
  106. /// </summary>
  107. public bool HasRepresentation(float scaleFactor)
  108. {
  109. var n_result = cef_image_t.has_representation(_self, scaleFactor);
  110. return n_result != 0;
  111. }
  112. /// <summary>
  113. /// Removes the representation for |scale_factor|. Returns true on success.
  114. /// </summary>
  115. public bool RemoveRepresentation(float scaleFactor)
  116. {
  117. var n_result = cef_image_t.remove_representation(_self, scaleFactor);
  118. return n_result != 0;
  119. }
  120. /// <summary>
  121. /// Returns information for the representation that most closely matches
  122. /// |scale_factor|. |actual_scale_factor| is the actual scale factor for the
  123. /// representation. |pixel_width| and |pixel_height| are the representation
  124. /// size in pixel coordinates. Returns true on success.
  125. /// </summary>
  126. public bool GetRepresentationInfo(float scaleFactor, out float actualScaleFactor, out int pixelWidth, out int pixelHeight)
  127. {
  128. float n_actualScaleFactor;
  129. int n_pixelWidth;
  130. int n_pixelHeight;
  131. var n_result = cef_image_t.get_representation_info(_self, scaleFactor, &n_actualScaleFactor, &n_pixelWidth, &n_pixelHeight);
  132. if (n_result != 0)
  133. {
  134. actualScaleFactor = n_actualScaleFactor;
  135. pixelWidth = n_pixelWidth;
  136. pixelHeight = n_pixelHeight;
  137. return true;
  138. }
  139. else
  140. {
  141. actualScaleFactor = 0;
  142. pixelWidth = 0;
  143. pixelHeight = 0;
  144. return false;
  145. }
  146. }
  147. /// <summary>
  148. /// Returns the bitmap representation that most closely matches |scale_factor|.
  149. /// Only 32-bit RGBA/BGRA formats are supported. |color_type| and |alpha_type|
  150. /// values specify the desired output pixel format. |pixel_width| and
  151. /// |pixel_height| are the output representation size in pixel coordinates.
  152. /// Returns a CefBinaryValue containing the pixel data on success or NULL on
  153. /// failure.
  154. /// </summary>
  155. public CefBinaryValue GetAsBitmap(float scaleFactor, CefColorType colorType, CefAlphaType alphaType, out int pixelWidth, out int pixelHeight)
  156. {
  157. int n_pixelWidth;
  158. int n_pixelHeight;
  159. var n_result = cef_image_t.get_as_bitmap(_self, scaleFactor, colorType, alphaType, &n_pixelWidth, &n_pixelHeight);
  160. if (n_result != null)
  161. {
  162. pixelWidth = n_pixelWidth;
  163. pixelHeight = n_pixelHeight;
  164. return CefBinaryValue.FromNative(n_result);
  165. }
  166. else
  167. {
  168. pixelWidth = 0;
  169. pixelHeight = 0;
  170. return null;
  171. }
  172. }
  173. /// <summary>
  174. /// Returns the PNG representation that most closely matches |scale_factor|. If
  175. /// |with_transparency| is true any alpha transparency in the image will be
  176. /// represented in the resulting PNG data. |pixel_width| and |pixel_height| are
  177. /// the output representation size in pixel coordinates. Returns a
  178. /// CefBinaryValue containing the PNG image data on success or NULL on failure.
  179. /// </summary>
  180. public CefBinaryValue GetAsPng(float scaleFactor, bool withTransparency, out int pixelWidth, out int pixelHeight)
  181. {
  182. int n_pixelWidth;
  183. int n_pixelHeight;
  184. var n_result = cef_image_t.get_as_png(_self, scaleFactor, withTransparency ? 1 : 0, &n_pixelWidth, &n_pixelHeight);
  185. if (n_result != null)
  186. {
  187. pixelWidth = n_pixelWidth;
  188. pixelHeight = n_pixelHeight;
  189. return CefBinaryValue.FromNative(n_result);
  190. }
  191. else
  192. {
  193. pixelWidth = 0;
  194. pixelHeight = 0;
  195. return null;
  196. }
  197. }
  198. /// <summary>
  199. /// Returns the JPEG representation that most closely matches |scale_factor|.
  200. /// |quality| determines the compression level with 0 == lowest and 100 ==
  201. /// highest. The JPEG format does not support alpha transparency and the alpha
  202. /// channel, if any, will be discarded. |pixel_width| and |pixel_height| are
  203. /// the output representation size in pixel coordinates. Returns a
  204. /// CefBinaryValue containing the JPEG image data on success or NULL on
  205. /// failure.
  206. /// </summary>
  207. public CefBinaryValue GetAsJpeg(float scaleFactor, int quality, out int pixelWidth, out int pixelHeight)
  208. {
  209. int n_pixelWidth;
  210. int n_pixelHeight;
  211. var n_result = cef_image_t.get_as_jpeg(_self, scaleFactor, quality, &n_pixelWidth, &n_pixelHeight);
  212. if (n_result != null)
  213. {
  214. pixelWidth = n_pixelWidth;
  215. pixelHeight = n_pixelHeight;
  216. return CefBinaryValue.FromNative(n_result);
  217. }
  218. else
  219. {
  220. pixelWidth = 0;
  221. pixelHeight = 0;
  222. return null;
  223. }
  224. }
  225. }
  226. }