2 // System.Drawing.Bitmap.cs
4 // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
5 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
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-2005 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:
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
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.
39 using System.Drawing.Imaging;
40 using System.Runtime.Serialization;
41 using System.Runtime.InteropServices;
42 using System.ComponentModel;
43 using System.Security.Permissions;
45 namespace System.Drawing
49 [Editor ("System.Drawing.Design.BitmapEditor, " + Consts.AssemblySystem_Drawing_Design, typeof (System.Drawing.Design.UITypeEditor))]
50 public sealed class Bitmap : Image
56 // required for XmlSerializer (#323246)
62 internal Bitmap (IntPtr ptr)
67 public Bitmap (int width, int height) : this (width, height, PixelFormat.Format32bppArgb)
71 public Bitmap (int width, int height, Graphics g)
74 Status s = GDIPlus.GdipCreateBitmapFromGraphics (width, height, g.nativeObject, out bmp);
75 GDIPlus.CheckStatus (s);
79 public Bitmap (int width, int height, PixelFormat format)
82 Status s = GDIPlus.GdipCreateBitmapFromScan0 (width, height, 0, format, IntPtr.Zero, out bmp);
83 GDIPlus.CheckStatus (s);
88 public Bitmap (Image original) : this (original, original.Width, original.Height) {}
90 public Bitmap (Stream stream) : this (stream, false) {}
92 public Bitmap (string filename) : this (filename, false) {}
94 public Bitmap (Image original, Size newSize) : this(original, newSize.Width, newSize.Height) {}
96 public Bitmap (Stream stream, bool useIcm)
98 // false: stream is owned by user code
99 nativeObject = InitFromStream (stream);
102 public Bitmap (string filename, bool useIcm)
108 st = GDIPlus.GdipCreateBitmapFromFileICM (filename, out imagePtr);
110 st = GDIPlus.GdipCreateBitmapFromFile (filename, out imagePtr);
112 GDIPlus.CheckStatus (st);
113 nativeObject = imagePtr;
116 public Bitmap (Type type, string resource)
118 Stream s = type.Assembly.GetManifestResourceStream (type, resource);
120 string msg = Locale.GetText ("Resource '{0}' was not found.", resource);
121 throw new FileNotFoundException (msg);
124 nativeObject = InitFromStream (s);
125 // under Win32 stream is owned by SD/GDI+ code
126 if (GDIPlus.RunningOnWindows ())
130 public Bitmap (Image original, int width, int height) : this(width, height, PixelFormat.Format32bppArgb)
132 Graphics graphics = Graphics.FromImage(this);
134 graphics.DrawImage(original, 0, 0, width, height);
138 public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
142 Status status = GDIPlus.GdipCreateBitmapFromScan0 (width, height, stride, format, scan0, out bmp);
143 GDIPlus.CheckStatus (status);
147 private Bitmap (SerializationInfo info, StreamingContext context)
148 : base (info, context)
154 public Color GetPixel (int x, int y) {
158 Status s = GDIPlus.GdipBitmapGetPixel(nativeObject, x, y, out argb);
159 GDIPlus.CheckStatus (s);
161 return Color.FromArgb(argb);
164 public void SetPixel (int x, int y, Color color)
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.");
173 throw new InvalidOperationException (msg);
175 throw new Exception (msg);
179 GDIPlus.CheckStatus (s);
182 public Bitmap Clone (Rectangle rect, PixelFormat format)
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);
191 public Bitmap Clone (RectangleF rect, PixelFormat format)
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);
200 public static Bitmap FromHicon (IntPtr hicon)
203 Status status = GDIPlus.GdipCreateBitmapFromHICON (hicon, out bitmap);
204 GDIPlus.CheckStatus (status);
205 return new Bitmap (bitmap);
208 public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TODO: Untested
211 Status status = GDIPlus.GdipCreateBitmapFromResource (hinstance, bitmapName, out bitmap);
212 GDIPlus.CheckStatus (status);
213 return new Bitmap (bitmap);
216 [EditorBrowsable (EditorBrowsableState.Advanced)]
217 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
218 public IntPtr GetHbitmap ()
220 return GetHbitmap(Color.Gray);
223 [EditorBrowsable (EditorBrowsableState.Advanced)]
224 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
225 public IntPtr GetHbitmap (Color background)
229 Status status = GDIPlus.GdipCreateHBITMAPFromBitmap (nativeObject, out HandleBmp, background.ToArgb ());
230 GDIPlus.CheckStatus (status);
235 [EditorBrowsable (EditorBrowsableState.Advanced)]
236 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
237 public IntPtr GetHicon ()
241 Status status = GDIPlus.GdipCreateHICONFromBitmap (nativeObject, out HandleIcon);
242 GDIPlus.CheckStatus (status);
247 public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format)
249 BitmapData result = new BitmapData();
250 return LockBits (rect, flags, format, result);
256 BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format, BitmapData bitmapData)
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);
265 public void MakeTransparent ()
267 Color clr = GetPixel(0,0);
268 MakeTransparent (clr);
271 public void MakeTransparent (Color transparentColor)
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();
279 imageAttr.SetColorKey(transparentColor, transparentColor);
281 gr.DrawImage (this, destRect, 0, 0, Width, Height, GraphicsUnit.Pixel, imageAttr);
283 IntPtr oldBmp = nativeObject;
284 nativeObject = bmp.nativeObject;
285 bmp.nativeObject = oldBmp;
292 public void SetResolution (float xDpi, float yDpi)
294 Status status = GDIPlus.GdipBitmapSetResolution (nativeObject, xDpi, yDpi);
295 GDIPlus.CheckStatus (status);
298 public void UnlockBits (BitmapData bitmapdata)
300 Status status = GDIPlus.GdipBitmapUnlockBits (nativeObject, bitmapdata);
301 GDIPlus.CheckStatus (status);