Metafile.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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, aren't 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 referenceHtc, EmfType emfType)
  81. {
  82. throw new NotImplementedException ();
  83. }
  84. public Metafile (IntPtr referenceHtc, Rectangle frameRect)
  85. {
  86. throw new NotImplementedException ();
  87. }
  88. public Metafile (IntPtr referenceHtc, RectangleF frameRect)
  89. {
  90. throw new NotImplementedException ();
  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 referenceHtc)
  98. {
  99. throw new NotImplementedException ();
  100. }
  101. public Metafile (String fileName, IntPtr referenceHtc)
  102. {
  103. throw new NotImplementedException ();
  104. }
  105. public Metafile (IntPtr referenceHdc, EmfType emfType, string description)
  106. {
  107. throw new NotImplementedException ();
  108. }
  109. public Metafile (IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit)
  110. {
  111. throw new NotImplementedException ();
  112. }
  113. public Metafile (IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit)
  114. {
  115. throw new NotImplementedException ();
  116. }
  117. public Metafile (IntPtr hmetafile, WmfPlaceableFileHeader wmfHeader, bool deleteWmf)
  118. {
  119. Status status = GDIPlus.GdipCreateMetafileFromEmf (hmetafile, deleteWmf, out nativeObject);
  120. GDIPlus.CheckStatus (status);
  121. }
  122. public Metafile (Stream stream, IntPtr referenceHdc, EmfType type)
  123. {
  124. throw new NotImplementedException ();
  125. }
  126. public Metafile (Stream stream, IntPtr referenceHdc, Rectangle frameRect)
  127. {
  128. throw new NotImplementedException ();
  129. }
  130. public Metafile (Stream stream, IntPtr referenceHdc, RectangleF frameRect)
  131. {
  132. throw new NotImplementedException ();
  133. }
  134. public Metafile (string fileName, IntPtr referenceHdc, EmfType type)
  135. {
  136. throw new NotImplementedException ();
  137. }
  138. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect)
  139. {
  140. throw new NotImplementedException ();
  141. }
  142. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect)
  143. {
  144. throw new NotImplementedException ();
  145. }
  146. public Metafile (IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit, EmfType type)
  147. {
  148. throw new NotImplementedException ();
  149. }
  150. public Metafile (IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type)
  151. {
  152. throw new NotImplementedException ();
  153. }
  154. public Metafile (Stream stream, IntPtr referenceHtc, EmfType type, string description)
  155. {
  156. throw new NotImplementedException ();
  157. }
  158. public Metafile (Stream stream, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit)
  159. {
  160. throw new NotImplementedException ();
  161. }
  162. public Metafile (Stream stream, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit)
  163. {
  164. throw new NotImplementedException ();
  165. }
  166. public Metafile (string fileName, IntPtr referenceHdc, EmfType type, string description)
  167. {
  168. throw new NotImplementedException ();
  169. }
  170. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit)
  171. {
  172. throw new NotImplementedException ();
  173. }
  174. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit)
  175. {
  176. throw new NotImplementedException ();
  177. }
  178. public Metafile (IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit, EmfType type, string description)
  179. {
  180. throw new NotImplementedException ();
  181. }
  182. public Metafile (IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type, string description)
  183. {
  184. throw new NotImplementedException ();
  185. }
  186. public Metafile (Stream stream, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit, EmfType type)
  187. {
  188. throw new NotImplementedException ();
  189. }
  190. public Metafile (Stream stream, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type)
  191. {
  192. throw new NotImplementedException ();
  193. }
  194. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit, EmfType type)
  195. {
  196. throw new NotImplementedException ();
  197. }
  198. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit, string description)
  199. {
  200. throw new NotImplementedException ();
  201. }
  202. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type)
  203. {
  204. throw new NotImplementedException ();
  205. }
  206. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, string description)
  207. {
  208. throw new NotImplementedException ();
  209. }
  210. public Metafile (Stream stream, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit, EmfType type,
  211. string description)
  212. {
  213. throw new NotImplementedException ();
  214. }
  215. public Metafile (Stream stream, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type,
  216. string description)
  217. {
  218. throw new NotImplementedException ();
  219. }
  220. public Metafile (string fileName, IntPtr referenceHdc, Rectangle frameRect, MetafileFrameUnit frameUnit, EmfType type,
  221. string description)
  222. {
  223. throw new NotImplementedException ();
  224. }
  225. public Metafile (string fileName, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type,
  226. string description)
  227. {
  228. throw new NotImplementedException ();
  229. }
  230. // methods
  231. public IntPtr GetHenhmetafile ()
  232. {
  233. return nativeObject;
  234. }
  235. [MonoLimitation ("Metafiles aren't supported by libgdiplus.")]
  236. public MetafileHeader GetMetafileHeader ()
  237. {
  238. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  239. try {
  240. Status status = GDIPlus.GdipGetMetafileHeaderFromMetafile (nativeObject, header);
  241. GDIPlus.CheckStatus (status);
  242. return new MetafileHeader (header);
  243. }
  244. finally {
  245. Marshal.FreeHGlobal (header);
  246. }
  247. }
  248. [MonoLimitation ("Metafiles aren't supported by libgdiplus.")]
  249. public static MetafileHeader GetMetafileHeader (IntPtr henhmetafile)
  250. {
  251. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  252. try {
  253. Status status = GDIPlus.GdipGetMetafileHeaderFromEmf (henhmetafile, header);
  254. GDIPlus.CheckStatus (status);
  255. return new MetafileHeader (header);
  256. }
  257. finally {
  258. Marshal.FreeHGlobal (header);
  259. }
  260. }
  261. [MonoLimitation ("Metafiles aren't supported by libgdiplus.")]
  262. public static MetafileHeader GetMetafileHeader (Stream stream)
  263. {
  264. if (stream == null)
  265. throw new NullReferenceException ("stream");
  266. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  267. try {
  268. Status status;
  269. if (GDIPlus.RunningOnUnix ()) {
  270. // With libgdiplus we use a custom API for this, because there's no easy way
  271. // to get the Stream down to libgdiplus. So, we wrap the stream with a set of delegates.
  272. GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper (stream);
  273. status = GDIPlus.GdipGetMetafileHeaderFromDelegate_linux (sh.GetHeaderDelegate,
  274. sh.GetBytesDelegate, sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate,
  275. sh.SizeDelegate, header);
  276. } else {
  277. status = GDIPlus.GdipGetMetafileHeaderFromStream (new ComIStreamWrapper (stream), header);
  278. }
  279. GDIPlus.CheckStatus (status);
  280. return new MetafileHeader (header);
  281. }
  282. finally {
  283. Marshal.FreeHGlobal (header);
  284. }
  285. }
  286. [MonoLimitation ("Metafiles aren't supported by libgdiplus.")]
  287. public static MetafileHeader GetMetafileHeader (string fileName)
  288. {
  289. if (fileName == null)
  290. throw new ArgumentNullException ("fileName");
  291. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  292. try {
  293. Status status = GDIPlus.GdipGetMetafileHeaderFromFile (fileName, header);
  294. GDIPlus.CheckStatus (status);
  295. return new MetafileHeader (header);
  296. }
  297. finally {
  298. Marshal.FreeHGlobal (header);
  299. }
  300. }
  301. [MonoLimitation ("Metafiles aren't supported by libgdiplus.")]
  302. public static MetafileHeader GetMetafileHeader (IntPtr henhmetafile, WmfPlaceableFileHeader wmfHeader)
  303. {
  304. IntPtr header = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (MetafileHeader)));
  305. try {
  306. Status status = GDIPlus.GdipGetMetafileHeaderFromEmf (henhmetafile, header);
  307. GDIPlus.CheckStatus (status);
  308. return new MetafileHeader (header);
  309. }
  310. finally {
  311. Marshal.FreeHGlobal (header);
  312. }
  313. }
  314. [MonoLimitation ("Metafiles aren't supported by libgdiplus.")]
  315. public void PlayRecord (EmfPlusRecordType recordType, int flags, int dataSize, byte[] data)
  316. {
  317. Status status = GDIPlus.GdipPlayMetafileRecord (nativeObject, recordType, flags, dataSize, data);
  318. GDIPlus.CheckStatus (status);
  319. }
  320. }
  321. }