2 // System.Drawing.Image.cs
4 // (C) 2002 Ximian, Inc. http://www.ximian.com
5 // Author: Christian Meyer
6 // eMail: Christian.Meyer@cs.tum.edu
8 // Alexandre Pigolkine (pigolkine@gmx.de)
11 namespace System.Drawing {
14 using System.Runtime.Remoting;
15 using System.Runtime.Serialization;
16 using System.Runtime.InteropServices;
17 using System.ComponentModel;
18 using System.Drawing.Imaging;
23 [Editor ("System.Drawing.Design.ImageEditor, " + Consts.AssemblySystem_Drawing_Design, typeof (System.Drawing.Design.UITypeEditor))]
24 [TypeConverter (typeof(ImageConverter))]
25 [ImmutableObject (true)]
26 public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISerializable
28 public delegate bool GetThumbnailImageAbort ();
30 internal IntPtr nativeObject = IntPtr.Zero;
31 protected Size image_size;
32 protected PixelFormat pixel_format;
33 protected ColorPalette colorPalette;
35 protected ImageFormat raw_format;
40 pixel_format = PixelFormat.Format32bppArgb;
41 colorPalette = new ColorPalette();
44 private Image (SerializationInfo info, StreamingContext context)
50 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
56 public static Image FromFile (string filename)
58 return new Bitmap (filename);
61 public static Image FromFile (string filename, bool useEmbeddedColorManagement)
63 return new Bitmap (filename, useEmbeddedColorManagement);
66 public static Bitmap FromHbitmap (IntPtr hbitmap)
68 // Fixme: implement me
69 throw new NotImplementedException ();
72 public static Bitmap FromHbitmap (IntPtr hbitmap, IntPtr hpalette)
74 // Fixme: implement me
75 throw new NotImplementedException ();
78 public static Image FromStream (Stream stream)
80 return new Bitmap (stream);
83 public static Image FromStream (Stream stream, bool useECM)
85 return new Bitmap (stream, useECM);
88 internal BitmapData Decode (Stream streamIn)
90 Stream stream = streamIn;
91 BitmapData result = new BitmapData ();
92 if (!stream.CanSeek) {
93 // FIXME: if stream.CanSeek == false, copy to a MemoryStream and read nicely
95 ImageCodecInfo[] availableDecoders = ImageCodecInfo.GetImageDecoders();
96 long pos = stream.Position;
97 ImageCodecInfo codecToUse = null;
98 foreach (ImageCodecInfo info in availableDecoders) {
99 for (int i = 0; i < info.SignaturePatterns.Length; i++) {
100 stream.Seek(pos, SeekOrigin.Begin);
101 bool codecFound = true;
102 for (int iPattern = 0; iPattern < info.SignaturePatterns[i].Length; iPattern++) {
103 byte pattern = (byte)stream.ReadByte();
104 pattern &= info.SignatureMasks[i][iPattern];
105 if (pattern != info.SignaturePatterns[i][iPattern]) {
116 stream.Seek (pos, SeekOrigin.Begin);
117 if (codecToUse != null && codecToUse.decode != null) {
118 codecToUse.decode (this, stream, result);
123 public static int GetPixelFormatSize (PixelFormat pixfmt)
127 case PixelFormat.Format16bppArgb1555:
128 case PixelFormat.Format16bppGrayScale:
129 case PixelFormat.Format16bppRgb555:
130 case PixelFormat.Format16bppRgb565:
133 case PixelFormat.Format1bppIndexed:
136 case PixelFormat.Format24bppRgb:
139 case PixelFormat.Format32bppArgb:
140 case PixelFormat.Format32bppPArgb:
141 case PixelFormat.Format32bppRgb:
144 case PixelFormat.Format48bppRgb:
147 case PixelFormat.Format4bppIndexed:
150 case PixelFormat.Format64bppArgb:
151 case PixelFormat.Format64bppPArgb:
154 case PixelFormat.Format8bppIndexed:
161 public static bool IsAlphaPixelFormat (PixelFormat pixfmt)
165 case PixelFormat.Format16bppArgb1555:
166 case PixelFormat.Format32bppArgb:
167 case PixelFormat.Format32bppPArgb:
168 case PixelFormat.Format64bppArgb:
169 case PixelFormat.Format64bppPArgb:
172 case PixelFormat.Format16bppGrayScale:
173 case PixelFormat.Format16bppRgb555:
174 case PixelFormat.Format16bppRgb565:
175 case PixelFormat.Format1bppIndexed:
176 case PixelFormat.Format24bppRgb:
177 case PixelFormat.Format32bppRgb:
178 case PixelFormat.Format48bppRgb:
179 case PixelFormat.Format4bppIndexed:
180 case PixelFormat.Format8bppIndexed:
187 // public static bool IsCanonicalPixelFormat (PixelFormat pixfmt)
189 // // Fixme: implement me
190 // throw new NotImplementedException ();
193 // public static bool IsExtendedPixelFormat (PixelFormat pixfmt)
195 // // Fixme: implement me
196 // throw new NotImplementedException ();
200 public RectangleF GetBounds (ref GraphicsUnit pageUnit)
202 // Fixme: implement me
203 throw new NotImplementedException ();
206 //public EncoderParameters GetEncoderParameterList(Guid encoder);
207 //public int GetFrameCount(FrameDimension dimension);
208 //public PropertyItem GetPropertyItem(int propid);
210 public Image GetThumbnailImage(int thumbWidth, int thumbHeight,
211 Image.GetThumbnailImageAbort callback,
212 IntPtr callbackData);
215 public void RemovePropertyItem (int propid)
217 // Fixme: implement me
218 throw new NotImplementedException ();
221 public void RotateFlip (RotateFlipType rotateFlipType)
223 // Fixme: implement me
224 throw new NotImplementedException ();
227 public void Save (string filename)
229 Save (filename, RawFormat);
232 public void Save (Stream stream, ImageFormat format)
234 ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
236 foreach (ImageCodecInfo encoder in encoders) {
237 if (encoder.FormatID != format.Guid)
240 if (encoder.encode == null)
242 if (!(this is Bitmap))
244 encoder.encode(this, stream);
249 public void Save(string filename, ImageFormat format)
251 FileStream fs = new FileStream (filename, FileMode.Create);
257 //public void Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams);
258 //public void Save(string filename, ImageCodecInfo encoder, EncoderParameters encoderParams);
259 //public void SaveAdd(EncoderParameters_ encoderParams);
260 //public void SaveAdd(Image image, EncoderParameters_ encoderParams);
261 //public int SelectActiveFrame(FrameDimension dimension, int frameIndex);
262 //public void SetPropertyItem(PropertyItem propitem);
267 throw new NotImplementedException ();
271 public Guid[] FrameDimensionsList {
273 throw new NotImplementedException ();
279 return image_size.Height;
283 public float HorizontalResolution {
285 throw new NotImplementedException ();
289 public ColorPalette Palette {
294 colorPalette = value;
298 public SizeF PhysicalDimension {
300 throw new NotImplementedException ();
304 public PixelFormat PixelFormat {
310 public int[] PropertyIdList {
312 throw new NotImplementedException ();
316 public PropertyItem[] PropertyItems {
318 throw new NotImplementedException ();
322 public ImageFormat RawFormat {
328 internal void SetRawFormat (ImageFormat format)
339 public float VerticalResolution {
341 throw new NotImplementedException ();
347 return image_size.Width;
351 internal IntPtr NativeObject{
356 nativeObject = value;
360 public void Dispose ()
370 protected virtual void DisposeResources ()
372 GDIPlus.GdipDisposeImage (nativeObject);
375 protected virtual void Dispose (bool disposing)
377 if (nativeObject != (IntPtr) 0){
379 nativeObject=IntPtr.Zero;
384 object ICloneable.Clone()
386 throw new NotImplementedException ();