ImageCodecInfo.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // System.Drawing.Imaging.ImageCodecInfo.cs
  3. //
  4. // Authors:
  5. // Everaldo Canuto ([email protected])
  6. // Andreas Nahr ([email protected])
  7. // Dennis Hayes ([email protected])
  8. // Jordi Mas i Hernandez ([email protected])
  9. // Sebastien Pouliot <[email protected]>
  10. //
  11. // (C) 2002 Ximian, Inc. http://www.ximian.com
  12. // Copyright (C) 2004,2006,2007 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Runtime.InteropServices;
  34. using System.Collections;
  35. using System.IO;
  36. namespace System.Drawing.Imaging {
  37. #if ONLY_1_1
  38. [ComVisible (false)]
  39. #endif
  40. public sealed class ImageCodecInfo {
  41. private Guid clsid;
  42. private string codecName;
  43. private string dllName;
  44. private string filenameExtension;
  45. private ImageCodecFlags flags;
  46. private string formatDescription;
  47. private Guid formatID;
  48. private string mimeType;
  49. private byte[][] signatureMasks;
  50. private byte[][] signaturePatterns;
  51. private int version;
  52. internal ImageCodecInfo()
  53. {
  54. }
  55. // methods
  56. public static ImageCodecInfo[] GetImageDecoders()
  57. {
  58. int decoderNums, arraySize, decoder_size;
  59. IntPtr decoders, decoder_ptr;
  60. ImageCodecInfo[] result;
  61. GdipImageCodecInfo gdipdecoder = new GdipImageCodecInfo();
  62. Status status;
  63. status = GDIPlus.GdipGetImageDecodersSize (out decoderNums, out arraySize);
  64. GDIPlus.CheckStatus (status);
  65. result = new ImageCodecInfo [decoderNums];
  66. if (decoderNums == 0)
  67. return result;
  68. /* Get decoders list*/
  69. decoders = Marshal.AllocHGlobal (arraySize);
  70. try {
  71. status = GDIPlus.GdipGetImageDecoders (decoderNums, arraySize, decoders);
  72. GDIPlus.CheckStatus (status);
  73. decoder_size = Marshal.SizeOf (gdipdecoder);
  74. decoder_ptr = decoders;
  75. for (int i = 0; i < decoderNums; i++, decoder_ptr = new IntPtr (decoder_ptr.ToInt64 () + decoder_size)) {
  76. gdipdecoder = (GdipImageCodecInfo) Marshal.PtrToStructure (decoder_ptr, typeof (GdipImageCodecInfo));
  77. result[i] = new ImageCodecInfo ();
  78. GdipImageCodecInfo.MarshalTo (gdipdecoder, result[i]);
  79. }
  80. }
  81. finally {
  82. Marshal.FreeHGlobal (decoders);
  83. }
  84. return result;
  85. }
  86. public static ImageCodecInfo[] GetImageEncoders()
  87. {
  88. int encoderNums, arraySize, encoder_size;
  89. IntPtr encoders, encoder_ptr;
  90. ImageCodecInfo[] result;
  91. GdipImageCodecInfo gdipencoder = new GdipImageCodecInfo();
  92. Status status;
  93. status = GDIPlus.GdipGetImageEncodersSize (out encoderNums, out arraySize);
  94. GDIPlus.CheckStatus (status);
  95. result = new ImageCodecInfo [encoderNums];
  96. if (encoderNums == 0)
  97. return result;
  98. /* Get encoders list*/
  99. encoders = Marshal.AllocHGlobal (arraySize);
  100. try {
  101. status = GDIPlus.GdipGetImageEncoders (encoderNums, arraySize, encoders);
  102. GDIPlus.CheckStatus (status);
  103. encoder_size = Marshal.SizeOf (gdipencoder);
  104. encoder_ptr = encoders;
  105. for (int i = 0; i < encoderNums; i++, encoder_ptr = new IntPtr (encoder_ptr.ToInt64 () + encoder_size)) {
  106. gdipencoder = (GdipImageCodecInfo) Marshal.PtrToStructure (encoder_ptr, typeof (GdipImageCodecInfo));
  107. result[i] = new ImageCodecInfo ();
  108. GdipImageCodecInfo.MarshalTo (gdipencoder, result[i]);
  109. }
  110. }
  111. finally {
  112. Marshal.FreeHGlobal (encoders);
  113. }
  114. return result;
  115. }
  116. // properties
  117. public Guid Clsid
  118. {
  119. get { return clsid; }
  120. set { clsid = value; }
  121. }
  122. public string CodecName
  123. {
  124. get { return codecName; }
  125. set { codecName = value; }
  126. }
  127. public string DllName
  128. {
  129. get { return dllName; }
  130. set { dllName = value; }
  131. }
  132. public string FilenameExtension
  133. {
  134. get { return filenameExtension; }
  135. set { filenameExtension = value; }
  136. }
  137. public ImageCodecFlags Flags
  138. {
  139. get { return flags; }
  140. set { flags = value; }
  141. }
  142. public string FormatDescription
  143. {
  144. get { return formatDescription; }
  145. set { formatDescription = value; }
  146. }
  147. public Guid FormatID
  148. {
  149. get { return formatID; }
  150. set { formatID = value; }
  151. }
  152. public string MimeType
  153. {
  154. get { return mimeType; }
  155. set { mimeType = value; }
  156. }
  157. [CLSCompliant(false)]
  158. public byte[][] SignatureMasks
  159. {
  160. get { return signatureMasks; }
  161. set { signatureMasks = value; }
  162. }
  163. [CLSCompliant(false)]
  164. public byte[][] SignaturePatterns
  165. {
  166. get { return signaturePatterns; }
  167. set { signaturePatterns = value; }
  168. }
  169. public int Version
  170. {
  171. get { return version; }
  172. set { version = value; }
  173. }
  174. }
  175. }