ImageFormat.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // System.Drawing.Imaging.ImageFormat.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-4 Ximian, Inc. http://www.ximian.com
  12. // Copyright (C) 2004,2006 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.ComponentModel;
  34. namespace System.Drawing.Imaging {
  35. [TypeConverter (typeof (ImageFormatConverter))]
  36. public sealed class ImageFormat {
  37. private Guid guid;
  38. private string name;
  39. private const string BmpGuid = "b96b3cab-0728-11d3-9d7b-0000f81ef32e";
  40. private const string EmfGuid = "b96b3cac-0728-11d3-9d7b-0000f81ef32e";
  41. private const string ExifGuid = "b96b3cb2-0728-11d3-9d7b-0000f81ef32e";
  42. private const string GifGuid = "b96b3cb0-0728-11d3-9d7b-0000f81ef32e";
  43. private const string TiffGuid = "b96b3cb1-0728-11d3-9d7b-0000f81ef32e";
  44. private const string PngGuid = "b96b3caf-0728-11d3-9d7b-0000f81ef32e";
  45. private const string MemoryBmpGuid = "b96b3caa-0728-11d3-9d7b-0000f81ef32e";
  46. private const string IconGuid = "b96b3cb5-0728-11d3-9d7b-0000f81ef32e";
  47. private const string JpegGuid = "b96b3cae-0728-11d3-9d7b-0000f81ef32e";
  48. private const string WmfGuid = "b96b3cad-0728-11d3-9d7b-0000f81ef32e";
  49. // lock(this) is bad
  50. // http://msdn.microsoft.com/library/en-us/dnaskdr/html/askgui06032003.asp?frame=true
  51. private static object locker = new object ();
  52. private static ImageFormat BmpImageFormat;
  53. private static ImageFormat EmfImageFormat;
  54. private static ImageFormat ExifImageFormat;
  55. private static ImageFormat GifImageFormat;
  56. private static ImageFormat TiffImageFormat;
  57. private static ImageFormat PngImageFormat;
  58. private static ImageFormat MemoryBmpImageFormat;
  59. private static ImageFormat IconImageFormat;
  60. private static ImageFormat JpegImageFormat;
  61. private static ImageFormat WmfImageFormat;
  62. // constructors
  63. public ImageFormat (Guid guid)
  64. {
  65. this.name = null;
  66. this.guid = guid;
  67. }
  68. private ImageFormat (string name, string guid)
  69. {
  70. this.name = name;
  71. this.guid = new Guid (guid);
  72. }
  73. // methods
  74. public override bool Equals (object o)
  75. {
  76. ImageFormat f = (o as ImageFormat);
  77. if (f == null)
  78. return false;
  79. return f.Guid.Equals (guid);
  80. }
  81. public override int GetHashCode ()
  82. {
  83. return guid.GetHashCode ();
  84. }
  85. public override string ToString ()
  86. {
  87. if (name != null)
  88. return name;
  89. return ("[ImageFormat: " + guid.ToString () + "]");
  90. }
  91. // properties
  92. public Guid Guid {
  93. get { return guid; }
  94. }
  95. public static ImageFormat Bmp {
  96. get {
  97. lock (locker) {
  98. if (BmpImageFormat == null)
  99. BmpImageFormat = new ImageFormat ("Bmp", BmpGuid);
  100. return BmpImageFormat;
  101. }
  102. }
  103. }
  104. public static ImageFormat Emf {
  105. get {
  106. lock (locker) {
  107. if (EmfImageFormat == null)
  108. EmfImageFormat = new ImageFormat ("Emf", EmfGuid);
  109. return EmfImageFormat;
  110. }
  111. }
  112. }
  113. public static ImageFormat Exif {
  114. get {
  115. lock (locker) {
  116. if (ExifImageFormat == null)
  117. ExifImageFormat = new ImageFormat ("Exif", ExifGuid);
  118. return ExifImageFormat;
  119. }
  120. }
  121. }
  122. public static ImageFormat Gif {
  123. get {
  124. lock (locker) {
  125. if (GifImageFormat == null)
  126. GifImageFormat = new ImageFormat ("Gif", GifGuid);
  127. return GifImageFormat;
  128. }
  129. }
  130. }
  131. public static ImageFormat Icon {
  132. get {
  133. lock (locker) {
  134. if (IconImageFormat == null)
  135. IconImageFormat = new ImageFormat ("Icon", IconGuid);
  136. return IconImageFormat;
  137. }
  138. }
  139. }
  140. public static ImageFormat Jpeg {
  141. get {
  142. lock (locker) {
  143. if (JpegImageFormat == null)
  144. JpegImageFormat = new ImageFormat ("Jpeg", JpegGuid);
  145. return JpegImageFormat;
  146. }
  147. }
  148. }
  149. public static ImageFormat MemoryBmp {
  150. get {
  151. lock (locker) {
  152. if (MemoryBmpImageFormat == null)
  153. MemoryBmpImageFormat = new ImageFormat ("MemoryBMP", MemoryBmpGuid);
  154. return MemoryBmpImageFormat;
  155. }
  156. }
  157. }
  158. public static ImageFormat Png {
  159. get {
  160. lock (locker) {
  161. if (PngImageFormat == null)
  162. PngImageFormat = new ImageFormat ("Png", PngGuid);
  163. return PngImageFormat;
  164. }
  165. }
  166. }
  167. public static ImageFormat Tiff {
  168. get {
  169. lock (locker) {
  170. if (TiffImageFormat == null)
  171. TiffImageFormat = new ImageFormat ("Tiff", TiffGuid);
  172. return TiffImageFormat;
  173. }
  174. }
  175. }
  176. public static ImageFormat Wmf {
  177. get {
  178. lock (locker) {
  179. if (WmfImageFormat == null)
  180. WmfImageFormat = new ImageFormat ("Wmf", WmfGuid);
  181. return WmfImageFormat;
  182. }
  183. }
  184. }
  185. }
  186. }