Metafile.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. [Editor ("System.Drawing.Design.MetafileEditor, " + Consts.AssemblySystem_Drawing_Design, typeof (System.Drawing.Design.UITypeEditor))]
  39. public sealed class Metafile : Image {
  40. // constructors
  41. internal Metafile (IntPtr ptr)
  42. {
  43. nativeObject = ptr;
  44. }
  45. // Usually called when cloning images that need to have
  46. // not only the handle saved, but also the underlying stream
  47. // (when using MS GDI+ and IStream we must ensure the stream stays alive for all the life of the Image)
  48. internal Metafile (IntPtr ptr, Stream stream)
  49. {
  50. // under Win32 stream is owned by SD/GDI+ code
  51. if (GDIPlus.RunningOnWindows ())
  52. this.stream = stream;
  53. nativeObject = ptr;
  54. }
  55. public Metafile (Stream stream)
  56. {
  57. if (stream == null)
  58. throw new ArgumentException ("stream");
  59. Status status;
  60. if (GDIPlus.RunningOnUnix ()) {
  61. // With libgdiplus we use a custom API for this, because there's no easy way
  62. // to get the Stream down to libgdiplus. So, we wrap the stream with a set of delegates.
  63. GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper (stream, false);
  64. status = GDIPlus.GdipCreateMetafileFromDelegate_linux (sh.GetHeaderDelegate, sh.GetBytesDelegate,
  65. sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, out nativeObject);
  66. } else {
  67. status = GDIPlus.GdipCreateMetafileFromStream (new ComIStreamWrapper (stream), out nativeObject);
  68. }
  69. GDIPlus.CheckStatus (status);
  70. }
  71. public Metafile (string filename)
  72. {
  73. if (filename == null)
  74. throw new ArgumentNullException ("filename");
  75. if (filename.Length == 0)
  76. throw new ArgumentException ("filename");
  77. Status status = GDIPlus.GdipCreateMetafileFromFile (filename, out nativeObject);
  78. if (status == Status.GenericError)
  79. throw new ExternalException ("Couldn't load specified file.");
  80. GDIPlus.CheckStatus (status);
  81. }
  82. public Metafile (IntPtr henhmetafile, bool deleteEmf)
  83. {
  84. Status status = GDIPlus.GdipCreateMetafileFromEmf (henhmetafile, deleteEmf, out nativeObject);
  85. GDIPlus.CheckStatus (status);
  86. }
  87. public Metafile (IntPtr referenceHdc, EmfType emfType) :
  88. this (referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, emfType, null)
  89. {
  90. }
  91. public Metafile (IntPtr referenceHdc, Rectangle frameRect) :
  92. this (referenceHdc, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  93. {
  94. }
  95. public Metafile (IntPtr referenceHdc, RectangleF frameRect) :
  96. this (referenceHdc, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  97. {
  98. }
  99. public Metafile (IntPtr hmetafile, WmfPlaceableFileHeader wmfHeader)
  100. {
  101. Status status = GDIPlus.GdipCreateMetafileFromEmf (hmetafile, false, out nativeObject);
  102. GDIPlus.CheckStatus (status);
  103. }
  104. public Metafile (Stream stream, IntPtr referenceHdc) :
  105. this (stream, referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  106. {
  107. }
  108. public Metafile (string fileName, IntPtr referenceHdc) :
  109. this (fileName, referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual,
  110. null)
  111. {
  112. }
  113. public Metafile (IntPtr referenceHdc, EmfType emfType, string description) :
  114. this (referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, emfType, description)
  115. {
  116. }
  117. public Metafile (IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit) :
  118. this (referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, null)
  119. {
  120. }
  121. public Metafile (IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit) :
  122. this (referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, null)
  123. {
  124. }
  125. public Metafile (IntPtr hmetafile, WmfPlaceableFileHeader wmfHeader, bool deleteWmf)
  126. {
  127. Status status = GDIPlus.GdipCreateMetafileFromEmf (hmetafile, deleteWmf, out nativeObject);
  128. GDIPlus.CheckStatus (status);
  129. }
  130. public Metafile (Stream stream, IntPtr referenceHdc, EmfType type) :
  131. this (stream, referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, type, null)
  132. {
  133. }
  134. public Metafile (Stream stream, IntPtr referenceHdc, Rectangle frameRect) :
  135. this (stream, referenceHdc, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  136. {
  137. }
  138. public Metafile (Stream stream, IntPtr referenceHdc, RectangleF frameRect) :
  139. this (stream, referenceHdc, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  140. {
  141. }
  142. public Metafile (string fileName, IntPtr referenceHdc, EmfType type) :
  143. this (fileName, referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, type, null)
  144. {
  145. }
  146. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect) :
  147. this (fileName, referenceHdc, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  148. {
  149. }
  150. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect) :
  151. this (fileName, referenceHdc, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusDual, null)
  152. {
  153. }
  154. public Metafile (IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit, EmfType type) :
  155. this (referenceHdc, frameRect, frameUnit, type, null)
  156. {
  157. }
  158. public Metafile (IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type) :
  159. this (referenceHdc, frameRect, frameUnit, type, null)
  160. {
  161. }
  162. public Metafile (Stream stream, IntPtr referenceHdc, EmfType type, string description) :
  163. this (stream, referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, type, description)
  164. {
  165. }
  166. public Metafile (Stream stream, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit) :
  167. this (stream, referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, null)
  168. {
  169. }
  170. public Metafile (Stream stream, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit) :
  171. this (stream, referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, null)
  172. {
  173. }
  174. public Metafile (string fileName, IntPtr referenceHdc, EmfType type, string description) :
  175. this (fileName, referenceHdc, new RectangleF (), MetafileFrameUnit.GdiCompatible, type, description)
  176. {
  177. }
  178. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit) :
  179. this (fileName, referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, null)
  180. {
  181. }
  182. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit) :
  183. this (fileName, referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, null)
  184. {
  185. }
  186. public Metafile (IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit, EmfType type,
  187. string description)
  188. {
  189. Status status = GDIPlus.GdipRecordMetafileI (referenceHdc, type, ref frameRect, frameUnit,
  190. description, out nativeObject);
  191. GDIPlus.CheckStatus (status);
  192. }
  193. public Metafile (IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type,
  194. string description)
  195. {
  196. Status status = GDIPlus.GdipRecordMetafile (referenceHdc, type, ref frameRect, frameUnit,
  197. description, out nativeObject);
  198. GDIPlus.CheckStatus (status);
  199. }
  200. public Metafile (Stream stream, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit,
  201. EmfType type) : this (stream, referenceHdc, frameRect, frameUnit, type, null)
  202. {
  203. }
  204. public Metafile (Stream stream, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit,
  205. EmfType type) : this (stream, referenceHdc, frameRect, frameUnit, type, null)
  206. {
  207. }
  208. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit,
  209. EmfType type) : this (fileName, referenceHdc, frameRect, frameUnit, type, null)
  210. {
  211. }
  212. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit,
  213. string description) : this (fileName, referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual, description)
  214. {
  215. }
  216. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit,
  217. EmfType type) : this (fileName, referenceHdc, frameRect, frameUnit, type, null)
  218. {
  219. }
  220. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit,
  221. string description) : this (fileName, referenceHdc, frameRect, frameUnit, EmfType.EmfPlusDual,
  222. description)
  223. {
  224. }
  225. public Metafile (Stream stream, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit,
  226. EmfType type, string description)
  227. {
  228. if (stream == null)
  229. throw new NullReferenceException ("stream");
  230. Status status = Status.NotImplemented;
  231. if (GDIPlus.RunningOnUnix ()) {
  232. // With libgdiplus we use a custom API for this, because there's no easy way
  233. // to get the Stream down to libgdiplus. So, we wrap the stream with a set of delegates.
  234. GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper (stream, false);
  235. status = GDIPlus.GdipRecordMetafileFromDelegateI_linux (sh.GetHeaderDelegate, sh.GetBytesDelegate,
  236. sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, referenceHdc,
  237. type, ref frameRect, frameUnit, description, out nativeObject);
  238. } else {
  239. status = GDIPlus.GdipRecordMetafileStreamI (new ComIStreamWrapper (stream), referenceHdc,
  240. type, ref frameRect, frameUnit, description, out nativeObject);
  241. }
  242. GDIPlus.CheckStatus (status);
  243. }
  244. public Metafile (Stream stream, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit,
  245. EmfType type, string description)
  246. {
  247. if (stream == null)
  248. throw new NullReferenceException ("stream");
  249. Status status = Status.NotImplemented;
  250. if (GDIPlus.RunningOnUnix ()) {
  251. // With libgdiplus we use a custom API for this, because there's no easy way
  252. // to get the Stream down to libgdiplus. So, we wrap the stream with a set of delegates.
  253. GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper (stream, false);
  254. status = GDIPlus.GdipRecordMetafileFromDelegate_linux (sh.GetHeaderDelegate, sh.GetBytesDelegate,
  255. sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, referenceHdc,
  256. type, ref frameRect, frameUnit, description, out nativeObject);
  257. } else {
  258. status = GDIPlus.GdipRecordMetafileStream (new ComIStreamWrapper (stream), referenceHdc,
  259. type, ref frameRect, frameUnit, description, out nativeObject);
  260. }
  261. GDIPlus.CheckStatus (status);
  262. }
  263. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit,
  264. EmfType type, string description)
  265. {
  266. Status status = GDIPlus.GdipRecordMetafileFileNameI (fileName, referenceHdc, type, ref frameRect,
  267. frameUnit, description, out nativeObject);
  268. GDIPlus.CheckStatus (status);
  269. }
  270. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit,
  271. EmfType type, string description)
  272. {
  273. Status status = GDIPlus.GdipRecordMetafileFileName (fileName, referenceHdc, type, ref frameRect, frameUnit,
  274. description, out nativeObject);
  275. GDIPlus.CheckStatus (status);
  276. }
  277. // methods
  278. public IntPtr GetHenhmetafile ()
  279. {
  280. return nativeObject;
  281. }
  282. [MonoLimitation ("Metafiles aren't only partially supported by libgdiplus.")]
  283. public MetafileHeader GetMetafileHeader ()
  284. {
  285. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  286. try {
  287. Status status = GDIPlus.GdipGetMetafileHeaderFromMetafile (nativeObject, header);
  288. GDIPlus.CheckStatus (status);
  289. return new MetafileHeader (header);
  290. }
  291. finally {
  292. Marshal.FreeHGlobal (header);
  293. }
  294. }
  295. [MonoLimitation ("Metafiles aren't only partially supported by libgdiplus.")]
  296. public static MetafileHeader GetMetafileHeader (IntPtr henhmetafile)
  297. {
  298. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  299. try {
  300. Status status = GDIPlus.GdipGetMetafileHeaderFromEmf (henhmetafile, header);
  301. GDIPlus.CheckStatus (status);
  302. return new MetafileHeader (header);
  303. }
  304. finally {
  305. Marshal.FreeHGlobal (header);
  306. }
  307. }
  308. [MonoLimitation ("Metafiles aren't only partially supported by libgdiplus.")]
  309. public static MetafileHeader GetMetafileHeader (Stream stream)
  310. {
  311. if (stream == null)
  312. throw new NullReferenceException ("stream");
  313. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  314. try {
  315. Status status;
  316. if (GDIPlus.RunningOnUnix ()) {
  317. // With libgdiplus we use a custom API for this, because there's no easy way
  318. // to get the Stream down to libgdiplus. So, we wrap the stream with a set of delegates.
  319. GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper (stream, false);
  320. status = GDIPlus.GdipGetMetafileHeaderFromDelegate_linux (sh.GetHeaderDelegate,
  321. sh.GetBytesDelegate, sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate,
  322. sh.SizeDelegate, header);
  323. } else {
  324. status = GDIPlus.GdipGetMetafileHeaderFromStream (new ComIStreamWrapper (stream), header);
  325. }
  326. GDIPlus.CheckStatus (status);
  327. return new MetafileHeader (header);
  328. }
  329. finally {
  330. Marshal.FreeHGlobal (header);
  331. }
  332. }
  333. [MonoLimitation ("Metafiles aren't only partially supported by libgdiplus.")]
  334. public static MetafileHeader GetMetafileHeader (string fileName)
  335. {
  336. if (fileName == null)
  337. throw new ArgumentNullException ("fileName");
  338. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  339. try {
  340. Status status = GDIPlus.GdipGetMetafileHeaderFromFile (fileName, header);
  341. GDIPlus.CheckStatus (status);
  342. return new MetafileHeader (header);
  343. }
  344. finally {
  345. Marshal.FreeHGlobal (header);
  346. }
  347. }
  348. [MonoLimitation ("Metafiles aren't only partially supported by libgdiplus.")]
  349. public static MetafileHeader GetMetafileHeader (IntPtr henhmetafile, WmfPlaceableFileHeader wmfHeader)
  350. {
  351. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  352. try {
  353. Status status = GDIPlus.GdipGetMetafileHeaderFromEmf (henhmetafile, header);
  354. GDIPlus.CheckStatus (status);
  355. return new MetafileHeader (header);
  356. }
  357. finally {
  358. Marshal.FreeHGlobal (header);
  359. }
  360. }
  361. [MonoLimitation ("Metafiles aren't only partially supported by libgdiplus.")]
  362. public void PlayRecord (EmfPlusRecordType recordType, int flags, int dataSize, byte[] data)
  363. {
  364. Status status = GDIPlus.GdipPlayMetafileRecord (nativeObject, recordType, flags, dataSize, data);
  365. GDIPlus.CheckStatus (status);
  366. }
  367. }
  368. }