* Bitmap.cs: Changed argument names to match MS. Changed spaces to
[mono.git] / mcs / class / System.Drawing / System.Drawing / Bitmap.cs
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)
13 //
14
15 //
16 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
17 //
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.
36 //
37
38 using System.IO;
39 using System.Drawing.Imaging;
40 using System.Runtime.Serialization;
41 using System.Runtime.InteropServices;
42 using System.ComponentModel;
43 using System.Security.Permissions;
44
45 namespace System.Drawing
46 {
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
51         {
52                 #region constructors
53                 // constructors
54
55 #if NET_2_0
56                 // required for XmlSerializer (#323246)
57                 private Bitmap ()
58                 {
59                 }
60 #endif
61
62                 internal Bitmap (IntPtr ptr)
63                 {
64                         nativeObject = ptr;
65                 }
66
67                 public Bitmap (int width, int height) : this (width, height, PixelFormat.Format32bppArgb)
68                 {
69                 }
70
71                 public Bitmap (int width, int height, Graphics g)
72                 {
73                         IntPtr bmp;
74                         Status s = GDIPlus.GdipCreateBitmapFromGraphics (width, height, g.nativeObject, out bmp);
75                         GDIPlus.CheckStatus (s);
76                         nativeObject = bmp;
77                 }
78
79                 public Bitmap (int width, int height, PixelFormat format)
80                 {
81                         IntPtr bmp;
82                         Status s = GDIPlus.GdipCreateBitmapFromScan0 (width, height, 0, format, IntPtr.Zero, out bmp);
83                         GDIPlus.CheckStatus (s);
84                         nativeObject = bmp;
85                         
86                 }
87
88                 public Bitmap (Image original) : this (original, original.Width, original.Height) {}
89
90                 public Bitmap (Stream stream)  : this (stream, false) {} 
91
92                 public Bitmap (string filename) : this (filename, false) {}
93
94                 public Bitmap (Image original, Size newSize)  : this(original, newSize.Width, newSize.Height) {}
95                 
96                 public Bitmap (Stream stream, bool useIcm)
97                 {
98                         // false: stream is owned by user code
99                         nativeObject = InitFromStream (stream);
100                 }
101
102                 public Bitmap (string filename, bool useIcm)
103                 {
104                         IntPtr imagePtr;
105                         Status st;
106
107                         if (useIcm)
108                                 st = GDIPlus.GdipCreateBitmapFromFileICM (filename, out imagePtr);
109                         else
110                                 st = GDIPlus.GdipCreateBitmapFromFile (filename, out imagePtr);
111
112                         GDIPlus.CheckStatus (st);
113                         nativeObject = imagePtr;
114                 }
115
116                 public Bitmap (Type type, string resource)
117                 {
118                         Stream s = type.Assembly.GetManifestResourceStream (type, resource);
119                         if (s == null) {
120                                 string msg = Locale.GetText ("Resource '{0}' was not found.", resource);
121                                 throw new FileNotFoundException (msg);
122                         }
123
124                         nativeObject = InitFromStream (s);
125                         // under Win32 stream is owned by SD/GDI+ code
126                         if (GDIPlus.RunningOnWindows ())
127                                 stream = s;
128                 }
129
130                 public Bitmap (Image original, int width, int height)  : this(width, height, PixelFormat.Format32bppArgb)
131                 {
132                         Graphics graphics = Graphics.FromImage(this);
133
134                         graphics.DrawImage(original, 0, 0, width, height);
135                         graphics.Dispose();
136                 }
137
138                 public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
139                 {
140                         IntPtr bmp;
141
142                         Status status = GDIPlus.GdipCreateBitmapFromScan0 (width, height, stride, format, scan0, out bmp);
143                         GDIPlus.CheckStatus (status);
144                         nativeObject = bmp;
145                 }
146
147                 private Bitmap (SerializationInfo info, StreamingContext context)
148                         : base (info, context)
149                 {
150                 }
151
152                 #endregion
153                 // methods
154                 public Color GetPixel (int x, int y) {
155                         
156                         int argb;
157
158                         Status s = GDIPlus.GdipBitmapGetPixel(nativeObject, x, y, out argb);
159                         GDIPlus.CheckStatus (s);
160
161                         return Color.FromArgb(argb);
162                 }
163
164                 public void SetPixel (int x, int y, Color color)
165                 {
166                         Status s = GDIPlus.GdipBitmapSetPixel (nativeObject, x, y, color.ToArgb ());
167                         if (s == Status.InvalidParameter) {
168                                 // check is done in case of an error only to avoid another
169                                 // unmanaged call for normal (successful) calls
170                                 if ((this.PixelFormat & PixelFormat.Indexed) != 0) {
171                                         string msg = Locale.GetText ("SetPixel cannot be called on indexed bitmaps.");
172 #if NET_2_0
173                                         throw new InvalidOperationException (msg);
174 #else
175                                         throw new Exception (msg);
176 #endif
177                                 }
178                         }
179                         GDIPlus.CheckStatus (s);
180                 }
181
182                 public Bitmap Clone (Rectangle rect, PixelFormat format)
183                 {
184                         IntPtr bmp;
185                         Status status = GDIPlus.GdipCloneBitmapAreaI (rect.X, rect.Y, rect.Width, rect.Height,
186                                 format, nativeObject, out bmp);
187                         GDIPlus.CheckStatus (status);
188                         return new Bitmap (bmp);
189                 }
190                 
191                 public Bitmap Clone (RectangleF rect, PixelFormat format)
192                 {
193                         IntPtr bmp;
194                         Status status = GDIPlus.GdipCloneBitmapArea (rect.X, rect.Y, rect.Width, rect.Height,
195                                 format, nativeObject, out bmp);
196                         GDIPlus.CheckStatus (status);
197                         return new Bitmap (bmp);
198                 }
199
200                 public static Bitmap FromHicon (IntPtr hicon)
201                 {       
202                         IntPtr bitmap;
203                         Status status = GDIPlus.GdipCreateBitmapFromHICON (hicon, out bitmap);
204                         GDIPlus.CheckStatus (status);
205                         return new Bitmap (bitmap);
206                 }
207
208                 public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TODO: Untested
209                 {
210                         IntPtr bitmap;
211                         Status status = GDIPlus.GdipCreateBitmapFromResource (hinstance, bitmapName, out bitmap);
212                         GDIPlus.CheckStatus (status);
213                         return new Bitmap (bitmap);
214                 }
215
216                 [EditorBrowsable (EditorBrowsableState.Advanced)]
217                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
218                 public IntPtr GetHbitmap ()
219                 {
220                         return GetHbitmap(Color.Gray);
221                 }
222
223                 [EditorBrowsable (EditorBrowsableState.Advanced)]
224                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
225                 public IntPtr GetHbitmap (Color background)
226                 {
227                         IntPtr HandleBmp;
228                         
229                         Status status = GDIPlus.GdipCreateHBITMAPFromBitmap (nativeObject, out HandleBmp, background.ToArgb ());
230                         GDIPlus.CheckStatus (status);
231
232                         return  HandleBmp;
233                 }
234
235                 [EditorBrowsable (EditorBrowsableState.Advanced)]
236                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
237                 public IntPtr GetHicon ()
238                 {
239                         IntPtr HandleIcon;
240                         
241                         Status status = GDIPlus.GdipCreateHICONFromBitmap (nativeObject, out HandleIcon);
242                         GDIPlus.CheckStatus (status);
243
244                         return  HandleIcon;
245                 }
246
247                 public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format)
248                 {
249                         BitmapData result = new BitmapData();
250                         return LockBits (rect, flags, format, result);
251                 }
252
253 #if NET_2_0
254                 public
255 #endif
256                 BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format, BitmapData bitmapData)
257                 {
258                         Status status = GDIPlus.GdipBitmapLockBits (nativeObject, ref rect, flags, format, bitmapData);
259                         //NOTE: scan0 points to piece of memory allocated in the unmanaged space
260                         GDIPlus.CheckStatus (status);
261
262                         return bitmapData;
263                 }
264
265                 public void MakeTransparent ()
266                 {
267                         Color clr = GetPixel(0,0);
268                         MakeTransparent (clr);
269                 }
270
271                 public void MakeTransparent (Color transparentColor)
272                 {
273                         // We have to draw always over a 32-bitmap surface that supports alpha channel
274                         Bitmap  bmp = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
275                         Graphics gr = Graphics.FromImage(bmp);
276                         Rectangle destRect = new Rectangle(0, 0, Width, Height);
277                         ImageAttributes imageAttr = new ImageAttributes();
278                         
279                         imageAttr.SetColorKey(transparentColor, transparentColor);
280
281                         gr.DrawImage (this, destRect, 0, 0, Width, Height, GraphicsUnit.Pixel, imageAttr);
282                         
283                         IntPtr oldBmp = nativeObject;
284                         nativeObject = bmp.nativeObject;
285                         bmp.nativeObject = oldBmp;
286
287                         gr.Dispose();
288                         bmp.Dispose();
289                         imageAttr.Dispose();
290                 }
291
292                 public void SetResolution (float xDpi, float yDpi)
293                 {
294                         Status status = GDIPlus.GdipBitmapSetResolution (nativeObject, xDpi, yDpi);
295                         GDIPlus.CheckStatus (status);
296                 }
297
298                 public void UnlockBits (BitmapData bitmapdata)
299                 {
300                         Status status = GDIPlus.GdipBitmapUnlockBits (nativeObject, bitmapdata);
301                         GDIPlus.CheckStatus (status);
302                 }
303         }
304 }