ImageCodecInfo.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. public sealed class ImageCodecInfo {
  38. private Guid clsid;
  39. private string codecName;
  40. private string dllName;
  41. private string filenameExtension;
  42. private ImageCodecFlags flags;
  43. private string formatDescription;
  44. private Guid formatID;
  45. private string mimeType;
  46. private byte[][] signatureMasks;
  47. private byte[][] signaturePatterns;
  48. private int version;
  49. internal ImageCodecInfo()
  50. {
  51. }
  52. // methods
  53. public static ImageCodecInfo[] GetImageDecoders()
  54. {
  55. int decoderNums, arraySize, decoder_size;
  56. IntPtr decoders, decoder_ptr;
  57. ImageCodecInfo[] result;
  58. GdipImageCodecInfo gdipdecoder = new GdipImageCodecInfo();
  59. Status status;
  60. status = GDIPlus.GdipGetImageDecodersSize (out decoderNums, out arraySize);
  61. GDIPlus.CheckStatus (status);
  62. result = new ImageCodecInfo [decoderNums];
  63. if (decoderNums == 0)
  64. return result;
  65. /* Get decoders list*/
  66. decoders = Marshal.AllocHGlobal (arraySize);
  67. try {
  68. status = GDIPlus.GdipGetImageDecoders (decoderNums, arraySize, decoders);
  69. GDIPlus.CheckStatus (status);
  70. decoder_size = Marshal.SizeOf (gdipdecoder);
  71. decoder_ptr = decoders;
  72. for (int i = 0; i < decoderNums; i++, decoder_ptr = new IntPtr (decoder_ptr.ToInt64 () + decoder_size)) {
  73. gdipdecoder = (GdipImageCodecInfo) Marshal.PtrToStructure (decoder_ptr, typeof (GdipImageCodecInfo));
  74. result[i] = new ImageCodecInfo ();
  75. GdipImageCodecInfo.MarshalTo (gdipdecoder, result[i]);
  76. }
  77. }
  78. finally {
  79. Marshal.FreeHGlobal (decoders);
  80. }
  81. return result;
  82. }
  83. public static ImageCodecInfo[] GetImageEncoders()
  84. {
  85. int encoderNums, arraySize, encoder_size;
  86. IntPtr encoders, encoder_ptr;
  87. ImageCodecInfo[] result;
  88. GdipImageCodecInfo gdipencoder = new GdipImageCodecInfo();
  89. Status status;
  90. status = GDIPlus.GdipGetImageEncodersSize (out encoderNums, out arraySize);
  91. GDIPlus.CheckStatus (status);
  92. result = new ImageCodecInfo [encoderNums];
  93. if (encoderNums == 0)
  94. return result;
  95. /* Get encoders list*/
  96. encoders = Marshal.AllocHGlobal (arraySize);
  97. try {
  98. status = GDIPlus.GdipGetImageEncoders (encoderNums, arraySize, encoders);
  99. GDIPlus.CheckStatus (status);
  100. encoder_size = Marshal.SizeOf (gdipencoder);
  101. encoder_ptr = encoders;
  102. for (int i = 0; i < encoderNums; i++, encoder_ptr = new IntPtr (encoder_ptr.ToInt64 () + encoder_size)) {
  103. gdipencoder = (GdipImageCodecInfo) Marshal.PtrToStructure (encoder_ptr, typeof (GdipImageCodecInfo));
  104. result[i] = new ImageCodecInfo ();
  105. GdipImageCodecInfo.MarshalTo (gdipencoder, result[i]);
  106. }
  107. }
  108. finally {
  109. Marshal.FreeHGlobal (encoders);
  110. }
  111. return result;
  112. }
  113. // properties
  114. public Guid Clsid
  115. {
  116. get { return clsid; }
  117. set { clsid = value; }
  118. }
  119. public string CodecName
  120. {
  121. get { return codecName; }
  122. set { codecName = value; }
  123. }
  124. public string DllName
  125. {
  126. get { return dllName; }
  127. set { dllName = value; }
  128. }
  129. public string FilenameExtension
  130. {
  131. get { return filenameExtension; }
  132. set { filenameExtension = value; }
  133. }
  134. public ImageCodecFlags Flags
  135. {
  136. get { return flags; }
  137. set { flags = value; }
  138. }
  139. public string FormatDescription
  140. {
  141. get { return formatDescription; }
  142. set { formatDescription = value; }
  143. }
  144. public Guid FormatID
  145. {
  146. get { return formatID; }
  147. set { formatID = value; }
  148. }
  149. public string MimeType
  150. {
  151. get { return mimeType; }
  152. set { mimeType = value; }
  153. }
  154. [CLSCompliant(false)]
  155. public byte[][] SignatureMasks
  156. {
  157. get { return signatureMasks; }
  158. set { signatureMasks = value; }
  159. }
  160. [CLSCompliant(false)]
  161. public byte[][] SignaturePatterns
  162. {
  163. get { return signaturePatterns; }
  164. set { signaturePatterns = value; }
  165. }
  166. public int Version
  167. {
  168. get { return version; }
  169. set { version = value; }
  170. }
  171. }
  172. }