(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Drawing / System.Drawing / Bitmap.cs
blob24fd41626907cd86c24e3bb9854962a3062b4502
1 //
2 // System.Drawing.Bitmap.cs
3 //
4 // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
5 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
6 //
7 // Authors:
8 // Alexandre Pigolkine (pigolkine@gmx.de)
9 // Christian Meyer (Christian.Meyer@cs.tum.edu)
10 // Miguel de Icaza (miguel@ximian.com)
11 // Jordi Mas i Hernandez (jmas@softcatala.org)
12 // Ravindra (rkumar@novell.com)
16 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
18 // Permission is hereby granted, free of charge, to any person obtaining
19 // a copy of this software and associated documentation files (the
20 // "Software"), to deal in the Software without restriction, including
21 // without limitation the rights to use, copy, modify, merge, publish,
22 // distribute, sublicense, and/or sell copies of the Software, and to
23 // permit persons to whom the Software is furnished to do so, subject to
24 // the following conditions:
25 //
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
28 //
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System;
39 using System.IO;
40 using System.Drawing.Imaging;
41 using System.Runtime.Serialization;
42 using System.Runtime.InteropServices;
43 using System.ComponentModel;
45 namespace System.Drawing
47 [Serializable]
48 [ComVisible (true)]
49 [Editor ("System.Drawing.Design.BitmapEditor, " + Consts.AssemblySystem_Drawing_Design, typeof (System.Drawing.Design.UITypeEditor))]
50 public sealed class Bitmap : Image
52 #region constructors
53 // constructors
54 internal Bitmap (IntPtr ptr)
56 nativeObject = ptr;
59 public Bitmap (int width, int height) : this (width, height, PixelFormat.Format32bppArgb)
64 public Bitmap (int width, int height, Graphics g)
66 IntPtr bmp;
67 Status s = GDIPlus.GdipCreateBitmapFromGraphics (width, height, g.nativeObject, out bmp);
68 GDIPlus.CheckStatus (s);
69 nativeObject = (IntPtr)bmp;
72 public Bitmap (int width, int height, PixelFormat format)
74 IntPtr bmp;
75 Status s = GDIPlus.GdipCreateBitmapFromScan0 (width, height, 0, format, IntPtr.Zero, out bmp);
76 GDIPlus.CheckStatus (s);
77 nativeObject = (IntPtr) bmp;
81 public Bitmap (Image original) : this (original.Width, original.Height, PixelFormat.Format32bppArgb)
83 BitmapFromImage(original, original.Size);
86 public Bitmap (Stream stream) : this (stream, false) {}
88 public Bitmap (string filename) : this (filename, false) {}
90 public Bitmap (Image original, Size newSize) : this (newSize.Width, newSize.Height, PixelFormat.Format32bppArgb)
92 Status status;
93 Graphics g;
95 g=Graphics.FromImage(this);
97 status = GDIPlus.GdipDrawImageRectRectI(g.nativeObject, original.nativeObject,
98 0, 0, newSize.Width, newSize.Height,
99 0, 0, original.Width, original.Height,
100 GraphicsUnit.Pixel, IntPtr.Zero, null, IntPtr.Zero);
101 GDIPlus.CheckStatus (status);
103 g.Dispose();
106 internal Bitmap (int width, int height, PixelFormat pixel, IntPtr bmp)
108 nativeObject = (IntPtr)bmp;
111 internal Bitmap (float width, float height, PixelFormat pixel, IntPtr bmp)
113 nativeObject = (IntPtr)bmp;
117 internal void BitmapFromImage(Image original, Size newSize){
119 if (original is Bitmap) {
121 if (nativeObject!=IntPtr.Zero)
122 Dispose();
124 Bitmap bmpOriginal = (Bitmap) original;
126 IntPtr bmp;
127 Status s = GDIPlus.GdipCloneBitmapAreaI (0, 0, newSize.Width, newSize.Height, bmpOriginal.PixelFormat, bmpOriginal.nativeObject, out bmp);
128 GDIPlus.CheckStatus (s);
129 nativeObject = (IntPtr) bmp;
132 else {
133 throw new NotImplementedException ();
137 void InitFromFile (string filename)
139 IntPtr imagePtr;
140 Status st = GDIPlus.GdipLoadImageFromFile (filename, out imagePtr);
141 GDIPlus.CheckStatus (st);
142 nativeObject = imagePtr;
145 public Bitmap (Stream stream, bool useIcm)
147 InitFromStream (stream);
150 public Bitmap (string filename, bool useIcm)
152 InitFromFile (filename);
155 public Bitmap (Type type, string resource)
157 using (Stream s = type.Assembly.GetManifestResourceStream (resource)){
158 if (s == null)
159 throw new FileNotFoundException ("Resource name was not found: `" + resource + "'");
161 InitFromStream (s);
165 public Bitmap (Image original, int width, int heigth)
167 Size newSize = new Size();
168 newSize.Height=heigth;
169 newSize.Width=width;
171 BitmapFromImage(original,newSize);
174 public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
176 IntPtr bmp;
178 Status status = GDIPlus.GdipCreateBitmapFromScan0 (width, height, stride, format, scan0, out bmp);
179 GDIPlus.CheckStatus (status);
180 nativeObject = (IntPtr) bmp;
183 //The below function is not required. Call should resolve to base
184 //Moreover there is a problem with the declaration. Base class function
185 //is not declared as protected to access in descendent class
186 /*private Bitmap (SerializationInfo info, StreamingContext context) : base(info, context)
190 #endregion
191 // methods
192 public Color GetPixel (int x, int y) {
194 int argb;
196 Status s = GDIPlus.GdipBitmapGetPixel(nativeObject, x, y, out argb);
197 GDIPlus.CheckStatus (s);
199 return Color.FromArgb(argb);
202 public void SetPixel (int x, int y, Color color)
204 Status s = GDIPlus.GdipBitmapSetPixel(nativeObject, x, y, color.ToArgb());
205 GDIPlus.CheckStatus (s);
208 public Bitmap Clone (Rectangle rect,PixelFormat format)
210 IntPtr bmp;
211 Status status = GDIPlus.GdipCloneBitmapAreaI(rect.X, rect.Top, rect.Width, rect.Height,
212 PixelFormat, nativeObject, out bmp);
214 GDIPlus.CheckStatus (status);
216 Bitmap bmpnew = new Bitmap (rect.Width, rect.Height, PixelFormat, (IntPtr) bmp);
217 return bmpnew;
220 public Bitmap Clone (RectangleF rect, PixelFormat format)
222 IntPtr bmp;
223 Status status = GDIPlus.GdipCloneBitmapArea (rect.X, rect.Top, rect.Width, rect.Height,
224 PixelFormat, nativeObject, out bmp);
225 GDIPlus.CheckStatus (status);
227 Bitmap bmpnew = new Bitmap (rect.Width, rect.Height, PixelFormat, (IntPtr) bmp);
228 return bmpnew;
231 public static Bitmap FromHicon (IntPtr hicon) //TODO: Untested
233 IntPtr bitmap;
235 Status status = GDIPlus.GdipCreateBitmapFromHICON (hicon, out bitmap);
236 GDIPlus.CheckStatus (status);
238 return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap); // FIXME
241 public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TODO: Untested
243 IntPtr bitmap;
245 Status status = GDIPlus.GdipCreateBitmapFromResource (hinstance, bitmapName, out bitmap);
246 GDIPlus.CheckStatus (status);
248 return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap); // FIXME
251 [EditorBrowsable (EditorBrowsableState.Advanced)]
252 public IntPtr GetHbitmap ()
254 return GetHbitmap(Color.Gray);
257 [EditorBrowsable (EditorBrowsableState.Advanced)]
258 public IntPtr GetHbitmap (Color background)
260 IntPtr HandleBmp;
262 Status status = GDIPlus.GdipCreateHBITMAPFromBitmap (nativeObject, out HandleBmp, background.ToArgb ());
263 GDIPlus.CheckStatus (status);
265 return HandleBmp;
268 [EditorBrowsable (EditorBrowsableState.Advanced)]
269 public IntPtr GetHicon ()
271 IntPtr HandleIcon;
273 Status status = GDIPlus.GdipCreateHICONFromBitmap (nativeObject, out HandleIcon);
274 GDIPlus.CheckStatus (status);
276 return HandleIcon;
279 public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format)
281 BitmapData result = new BitmapData();
283 if (nativeObject == (IntPtr) 0)
284 throw new Exception ("nativeObject is null");
286 IntPtr lfBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(result));
287 Marshal.StructureToPtr(result, lfBuffer, false);
289 Status status = GDIPlus.GdipBitmapLockBits (nativeObject, ref rect, flags, format, lfBuffer);
291 result = (BitmapData) Marshal.PtrToStructure(lfBuffer, typeof(BitmapData));
292 Marshal.FreeHGlobal (lfBuffer);
293 //NOTE: scan0 points to piece of memory allocated in the unmanaged space
294 GDIPlus.CheckStatus (status);
296 return result;
299 public void MakeTransparent ()
301 Color clr = GetPixel(0,0);
302 MakeTransparent (clr);
305 public void MakeTransparent (Color transparentColor)
307 // We have to draw always over a 32-bitmap surface that supports alpha channel
308 Bitmap bmp = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
309 Graphics gr = Graphics.FromImage(bmp);
310 Rectangle destRect = new Rectangle(0,0, Width, Height);
311 ImageAttributes imageAttr = new ImageAttributes();
313 imageAttr.SetColorKey(transparentColor, transparentColor);
315 gr.DrawImage (this, destRect, 0, 0, Width, Height, GraphicsUnit.Pixel, imageAttr);
317 Size newSize = new Size();
318 newSize.Height=Height;
319 newSize.Width=Width;
320 BitmapFromImage(bmp,newSize);
322 gr.Dispose();
323 bmp.Dispose();
324 imageAttr.Dispose();
327 public void SetResolution (float xDpi, float yDpi)
329 Status status = GDIPlus.GdipBitmapSetResolution (nativeObject, xDpi, yDpi);
330 GDIPlus.CheckStatus (status);
333 public void UnlockBits (BitmapData bitmap_data)
335 Status status = GDIPlus.GdipBitmapUnlockBits (nativeObject, bitmap_data);
336 GDIPlus.CheckStatus (status);