Metafile.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. //
  2. // System.Drawing.Imaging.Metafile.cs
  3. //
  4. // Authors:
  5. // Christian Meyer, eMail: [email protected]
  6. // Dennis Hayes ([email protected])
  7. // Sebastien Pouliot <[email protected]>
  8. //
  9. // (C) 2002 Ximian, Inc. http://www.ximian.com
  10. // Copyright (C) 2004,2006-2007 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.IO;
  32. using System.Reflection;
  33. using System.ComponentModel;
  34. using System.Runtime.InteropServices;
  35. namespace System.Drawing.Imaging {
  36. [MonoTODO ("Metafiles, both WMF and EMF formats, are only partially supported.")]
  37. [Serializable]
  38. #if ONLY_1_1
  39. [ComVisible (false)]
  40. #endif
  41. [Editor ("System.Drawing.Design.MetafileEditor, " + Consts.AssemblySystem_Drawing_Design, typeof (System.Drawing.Design.UITypeEditor))]
  42. public sealed class Metafile : Image {
  43. // constructors
  44. internal Metafile (IntPtr ptr)
  45. {
  46. nativeObject = ptr;
  47. }
  48. public Metafile (Stream stream)
  49. {
  50. if (stream == null)
  51. throw new ArgumentException ("stream");
  52. Status status;
  53. if (GDIPlus.RunningOnUnix ()) {
  54. // With libgdiplus we use a custom API for this, because there's no easy way
  55. // to get the Stream down to libgdiplus. So, we wrap the stream with a set of delegates.
  56. GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper (stream);
  57. status = GDIPlus.GdipCreateMetafileFromDelegate_linux (sh.GetHeaderDelegate, sh.GetBytesDelegate,
  58. sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, out nativeObject);
  59. } else {
  60. status = GDIPlus.GdipCreateMetafileFromStream (new ComIStreamWrapper (stream), out nativeObject);
  61. }
  62. GDIPlus.CheckStatus (status);
  63. }
  64. public Metafile (string filename)
  65. {
  66. if (filename == null)
  67. throw new ArgumentNullException ("filename");
  68. if (filename.Length == 0)
  69. throw new ArgumentException ("filename");
  70. Status status = GDIPlus.GdipCreateMetafileFromFile (filename, out nativeObject);
  71. if (status == Status.GenericError)
  72. throw new ExternalException ("Couldn't load specified file.");
  73. GDIPlus.CheckStatus (status);
  74. }
  75. public Metafile (IntPtr henhmetafile, bool deleteEmf)
  76. {
  77. Status status = GDIPlus.GdipCreateMetafileFromEmf (henhmetafile, deleteEmf, out nativeObject);
  78. GDIPlus.CheckStatus (status);
  79. }
  80. public Metafile (IntPtr referenceHdc, EmfType emfType) :
  81. this (referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, emfType, null)
  82. {
  83. }
  84. public Metafile (IntPtr referenceHdc, Rectangle frameRect) :
  85. this (referenceHdc, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  86. {
  87. }
  88. public Metafile (IntPtr referenceHdc, RectangleF frameRect) :
  89. this (referenceHdc, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  90. {
  91. }
  92. public Metafile (IntPtr hmetafile, WmfPlaceableFileHeader wmfHeader)
  93. {
  94. Status status = GDIPlus.GdipCreateMetafileFromEmf (hmetafile, false, out nativeObject);
  95. GDIPlus.CheckStatus (status);
  96. }
  97. public Metafile (Stream stream, IntPtr referenceHdc) :
  98. this (stream, referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  99. {
  100. }
  101. public Metafile (string fileName, IntPtr referenceHdc) :
  102. this (fileName, referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual,
  103. null)
  104. {
  105. }
  106. public Metafile (IntPtr referenceHdc, EmfType emfType, string description) :
  107. this (referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, emfType, description)
  108. {
  109. }
  110. public Metafile (IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit) :
  111. this (referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, null)
  112. {
  113. }
  114. public Metafile (IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit) :
  115. this (referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, null)
  116. {
  117. }
  118. public Metafile (IntPtr hmetafile, WmfPlaceableFileHeader wmfHeader, bool deleteWmf)
  119. {
  120. Status status = GDIPlus.GdipCreateMetafileFromEmf (hmetafile, deleteWmf, out nativeObject);
  121. GDIPlus.CheckStatus (status);
  122. }
  123. public Metafile (Stream stream, IntPtr referenceHdc, EmfType type) :
  124. this (stream, referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, type, null)
  125. {
  126. }
  127. public Metafile (Stream stream, IntPtr referenceHdc, Rectangle frameRect) :
  128. this (stream, referenceHdc, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  129. {
  130. }
  131. public Metafile (Stream stream, IntPtr referenceHdc, RectangleF frameRect) :
  132. this (stream, referenceHdc, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  133. {
  134. }
  135. public Metafile (string fileName, IntPtr referenceHdc, EmfType type) :
  136. this (fileName, referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, type, null)
  137. {
  138. }
  139. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect) :
  140. this (fileName, referenceHdc, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  141. {
  142. }
  143. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect) :
  144. this (fileName, referenceHdc, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  145. {
  146. }
  147. public Metafile (IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit, EmfType type) :
  148. this (referenceHdc, frameRect, frameUnit, type, null)
  149. {
  150. }
  151. public Metafile (IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type) :
  152. this (referenceHdc, frameRect, frameUnit, type, null)
  153. {
  154. }
  155. public Metafile (Stream stream, IntPtr referenceHdc, EmfType type, string description) :
  156. this (stream, referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, type, description)
  157. {
  158. }
  159. public Metafile (Stream stream, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit) :
  160. this (stream, referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, null)
  161. {
  162. }
  163. public Metafile (Stream stream, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit) :
  164. this (stream, referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, null)
  165. {
  166. }
  167. public Metafile (string fileName, IntPtr referenceHdc, EmfType type, string description) :
  168. this (fileName, referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, type, description)
  169. {
  170. }
  171. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit) :
  172. this (fileName, referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, null)
  173. {
  174. }
  175. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit) :
  176. this (fileName, referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, null)
  177. {
  178. }
  179. public Metafile (IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit, EmfType type,
  180. string description)
  181. {
  182. Status status = GDIPlus.GdipRecordMetafileI (referenceHdc, type, ref frameRect, frameUnit,
  183. description, out nativeObject);
  184. GDIPlus.CheckStatus (status);
  185. }
  186. public Metafile (IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type,
  187. string description)
  188. {
  189. Status status = GDIPlus.GdipRecordMetafile (referenceHdc, type, ref frameRect, frameUnit,
  190. description, out nativeObject);
  191. GDIPlus.CheckStatus (status);
  192. }
  193. public Metafile (Stream stream, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit,
  194. EmfType type) : this (stream, referenceHdc, frameRect, frameUnit, type, null)
  195. {
  196. }
  197. public Metafile (Stream stream, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit,
  198. EmfType type) : this (stream, referenceHdc, frameRect, frameUnit, type, null)
  199. {
  200. }
  201. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit,
  202. EmfType type) : this (fileName, referenceHdc, frameRect, frameUnit, type, null)
  203. {
  204. }
  205. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit,
  206. string description) : this (fileName, referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, description)
  207. {
  208. }
  209. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit,
  210. EmfType type) : this (fileName, referenceHdc, frameRect, frameUnit, type, null)
  211. {
  212. }
  213. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit,
  214. string description) : this (fileName, referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual,
  215. description)
  216. {
  217. }
  218. public Metafile (Stream stream, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit,
  219. EmfType type, string description)
  220. {
  221. if (stream == null)
  222. throw new NullReferenceException ("stream");
  223. Status status = Status.NotImplemented;
  224. if (GDIPlus.RunningOnUnix ()) {
  225. // With libgdiplus we use a custom API for this, because there's no easy way
  226. // to get the Stream down to libgdiplus. So, we wrap the stream with a set of delegates.
  227. GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper (stream);
  228. status = GDIPlus.GdipRecordMetafileFromDelegateI_linux (sh.GetHeaderDelegate, sh.GetBytesDelegate,
  229. sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, referenceHdc,
  230. type, ref frameRect, frameUnit, description, out nativeObject);
  231. } else {
  232. status = GDIPlus.GdipRecordMetafileStreamI (new ComIStreamWrapper (stream), referenceHdc,
  233. type, ref frameRect, frameUnit, description, out nativeObject);
  234. }
  235. GDIPlus.CheckStatus (status);
  236. }
  237. public Metafile (Stream stream, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit,
  238. EmfType type, string description)
  239. {
  240. if (stream == null)
  241. throw new NullReferenceException ("stream");
  242. Status status = Status.NotImplemented;
  243. if (GDIPlus.RunningOnUnix ()) {
  244. // With libgdiplus we use a custom API for this, because there's no easy way
  245. // to get the Stream down to libgdiplus. So, we wrap the stream with a set of delegates.
  246. GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper (stream);
  247. status = GDIPlus.GdipRecordMetafileFromDelegate_linux (sh.GetHeaderDelegate, sh.GetBytesDelegate,
  248. sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, referenceHdc,
  249. type, ref frameRect, frameUnit, description, out nativeObject);
  250. } else {
  251. status = GDIPlus.GdipRecordMetafileStream (new ComIStreamWrapper (stream), referenceHdc,
  252. type, ref frameRect, frameUnit, description, out nativeObject);
  253. }
  254. GDIPlus.CheckStatus (status);
  255. }
  256. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit,
  257. EmfType type, string description)
  258. {
  259. Status status = GDIPlus.GdipRecordMetafileFileNameI (fileName, referenceHdc, type, ref frameRect,
  260. frameUnit, description, out nativeObject);
  261. GDIPlus.CheckStatus (status);
  262. }
  263. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit,
  264. EmfType type, string description)
  265. {
  266. Status status = GDIPlus.GdipRecordMetafileFileName (fileName, referenceHdc, type, ref frameRect, frameUnit,
  267. description, out nativeObject);
  268. GDIPlus.CheckStatus (status);
  269. }
  270. // methods
  271. public IntPtr GetHenhmetafile ()
  272. {
  273. return nativeObject;
  274. }
  275. [MonoLimitation ("Metafiles aren't only partially supported by libgdiplus.")]
  276. public MetafileHeader GetMetafileHeader ()
  277. {
  278. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  279. try {
  280. Status status = GDIPlus.GdipGetMetafileHeaderFromMetafile (nativeObject, header);
  281. GDIPlus.CheckStatus (status);
  282. return new MetafileHeader (header);
  283. }
  284. finally {
  285. Marshal.FreeHGlobal (header);
  286. }
  287. }
  288. [MonoLimitation ("Metafiles aren't only partially supported by libgdiplus.")]
  289. public static MetafileHeader GetMetafileHeader (IntPtr henhmetafile)
  290. {
  291. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  292. try {
  293. Status status = GDIPlus.GdipGetMetafileHeaderFromEmf (henhmetafile, header);
  294. GDIPlus.CheckStatus (status);
  295. return new MetafileHeader (header);
  296. }
  297. finally {
  298. Marshal.FreeHGlobal (header);
  299. }
  300. }
  301. [MonoLimitation ("Metafiles aren't only partially supported by libgdiplus.")]
  302. public static MetafileHeader GetMetafileHeader (Stream stream)
  303. {
  304. if (stream == null)
  305. throw new NullReferenceException ("stream");
  306. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  307. try {
  308. Status status;
  309. if (GDIPlus.RunningOnUnix ()) {
  310. // With libgdiplus we use a custom API for this, because there's no easy way
  311. // to get the Stream down to libgdiplus. So, we wrap the stream with a set of delegates.
  312. GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper (stream);
  313. status = GDIPlus.GdipGetMetafileHeaderFromDelegate_linux (sh.GetHeaderDelegate,
  314. sh.GetBytesDelegate, sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate,
  315. sh.SizeDelegate, header);
  316. } else {
  317. status = GDIPlus.GdipGetMetafileHeaderFromStream (new ComIStreamWrapper (stream), header);
  318. }
  319. GDIPlus.CheckStatus (status);
  320. return new MetafileHeader (header);
  321. }
  322. finally {
  323. Marshal.FreeHGlobal (header);
  324. }
  325. }
  326. [MonoLimitation ("Metafiles aren't only partially supported by libgdiplus.")]
  327. public static MetafileHeader GetMetafileHeader (string fileName)
  328. {
  329. if (fileName == null)
  330. throw new ArgumentNullException ("fileName");
  331. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  332. try {
  333. Status status = GDIPlus.GdipGetMetafileHeaderFromFile (fileName, header);
  334. GDIPlus.CheckStatus (status);
  335. return new MetafileHeader (header);
  336. }
  337. finally {
  338. Marshal.FreeHGlobal (header);
  339. }
  340. }
  341. [MonoLimitation ("Metafiles aren't only partially supported by libgdiplus.")]
  342. public static MetafileHeader GetMetafileHeader (IntPtr henhmetafile, WmfPlaceableFileHeader wmfHeader)
  343. {
  344. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  345. try {
  346. Status status = GDIPlus.GdipGetMetafileHeaderFromEmf (henhmetafile, header);
  347. GDIPlus.CheckStatus (status);
  348. return new MetafileHeader (header);
  349. }
  350. finally {
  351. Marshal.FreeHGlobal (header);
  352. }
  353. }
  354. [MonoLimitation ("Metafiles aren't only partially supported by libgdiplus.")]
  355. public void PlayRecord (EmfPlusRecordType recordType, int flags, int dataSize, byte[] data)
  356. {
  357. Status status = GDIPlus.GdipPlayMetafileRecord (nativeObject, recordType, flags, dataSize, data);
  358. GDIPlus.CheckStatus (status);
  359. }
  360. }
  361. }