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
54 internal Bitmap (IntPtr ptr)
59 public Bitmap (int width, int height) : this (width, height, PixelFormat.Format32bppArgb)
64 public Bitmap (int width, int height, Graphics g)
67 Status s = GDIPlus.GdipCreateBitmapFromGraphics (width, height, g.nativeObject, out bmp);
68 GDIPlus.CheckStatus (s);
72 public Bitmap (int width, int height, PixelFormat format)
75 Status s = GDIPlus.GdipCreateBitmapFromScan0 (width, height, 0, format, IntPtr.Zero, out bmp);
76 GDIPlus.CheckStatus (s);
81 public Bitmap (Image original) : this (original, original.Width, original.Height) {}
83 public Bitmap (Stream stream) : this (stream, false) {}
85 public Bitmap (string filename) : this (filename, false) {}
87 public Bitmap (Image original, Size newSize) : this(original, newSize.Width, newSize.Height) {}
89 internal Bitmap (int width, int height, PixelFormat pixel, IntPtr bmp)
94 internal Bitmap (float width, float height, PixelFormat pixel, IntPtr bmp)
100 public Bitmap (Stream stream, bool useIcm)
103 throw new ArgumentNullException ("stream");
105 InitFromStream (stream);
108 public Bitmap (string filename, bool useIcm)
114 st = GDIPlus.GdipCreateBitmapFromFileICM (filename, out imagePtr);
116 st = GDIPlus.GdipCreateBitmapFromFile (filename, out imagePtr);
118 GDIPlus.CheckStatus (st);
119 nativeObject = imagePtr;
122 public Bitmap (Type type, string resource)
124 using (Stream s = type.Assembly.GetManifestResourceStream (type, resource)){
126 throw new FileNotFoundException ("Resource name was not found: `" + resource + "'");
132 public Bitmap (Image original, int width, int height) : this(width, height, PixelFormat.Format32bppArgb)
134 Graphics graphics = Graphics.FromImage(this);
136 graphics.DrawImage(original, 0, 0, width, height);
140 public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
144 Status status = GDIPlus.GdipCreateBitmapFromScan0 (width, height, stride, format, scan0, out bmp);
145 GDIPlus.CheckStatus (status);
149 private Bitmap (SerializationInfo info, StreamingContext context)
151 foreach (SerializationEntry serEnum in info) {
152 if (String.Compare(serEnum.Name, "Data", true) == 0) {
153 byte[] bytes = (byte[]) serEnum.Value;
156 InitFromStream(new MemoryStream(bytes));
161 //The below function is not required. Call should resolve to base
162 //Moreover there is a problem with the declaration. Base class function
163 //is not declared as protected to access in descendent class
164 /*private Bitmap (SerializationInfo info, StreamingContext context) : base(info, context)
170 public Color GetPixel (int x, int y) {
174 Status s = GDIPlus.GdipBitmapGetPixel(nativeObject, x, y, out argb);
175 GDIPlus.CheckStatus (s);
177 return Color.FromArgb(argb);
180 public void SetPixel (int x, int y, Color color)
182 Status s = GDIPlus.GdipBitmapSetPixel(nativeObject, x, y, color.ToArgb());
183 GDIPlus.CheckStatus (s);
186 public Bitmap Clone (Rectangle rect,PixelFormat format)
189 Status status = GDIPlus.GdipCloneBitmapAreaI(rect.X, rect.Top, rect.Width, rect.Height,
190 format, nativeObject, out bmp);
192 GDIPlus.CheckStatus (status);
194 Bitmap bmpnew = new Bitmap (rect.Width, rect.Height, format, bmp);
198 public Bitmap Clone (RectangleF rect, PixelFormat format)
201 Status status = GDIPlus.GdipCloneBitmapArea (rect.X, rect.Top, rect.Width, rect.Height,
202 format, nativeObject, out bmp);
203 GDIPlus.CheckStatus (status);
205 Bitmap bmpnew = new Bitmap (rect.Width, rect.Height, format, bmp);
209 public static Bitmap FromHicon (IntPtr hicon) //TODO: Untested
213 Status status = GDIPlus.GdipCreateBitmapFromHICON (hicon, out bitmap);
214 GDIPlus.CheckStatus (status);
216 return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap); // FIXME
219 public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TODO: Untested
223 Status status = GDIPlus.GdipCreateBitmapFromResource (hinstance, bitmapName, out bitmap);
224 GDIPlus.CheckStatus (status);
226 return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap); // FIXME
229 [EditorBrowsable (EditorBrowsableState.Advanced)]
230 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
231 public IntPtr GetHbitmap ()
233 return GetHbitmap(Color.Gray);
236 [EditorBrowsable (EditorBrowsableState.Advanced)]
237 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
238 public IntPtr GetHbitmap (Color background)
242 Status status = GDIPlus.GdipCreateHBITMAPFromBitmap (nativeObject, out HandleBmp, background.ToArgb ());
243 GDIPlus.CheckStatus (status);
248 [EditorBrowsable (EditorBrowsableState.Advanced)]
249 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
250 public IntPtr GetHicon ()
254 Status status = GDIPlus.GdipCreateHICONFromBitmap (nativeObject, out HandleIcon);
255 GDIPlus.CheckStatus (status);
260 public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format)
262 BitmapData result = new BitmapData();
263 return LockBits (rect, flags, format, result);
269 BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format, BitmapData bitmapData)
271 if (bitmapData == null)
272 throw new ArgumentException ("bitmapData");
274 int img_format = (int) PixelFormat;
275 if ((int) format != img_format && (img_format & (int) PixelFormat.Indexed) != 0 &&
276 (flags == ImageLockMode.WriteOnly || flags == ImageLockMode.ReadWrite)) {
277 throw new ArgumentException ("Parameter is not valid.");
280 Status status = GDIPlus.GdipBitmapLockBits (nativeObject, ref rect, flags, format, bitmapData);
281 //NOTE: scan0 points to piece of memory allocated in the unmanaged space
282 GDIPlus.CheckStatus (status);
287 public void MakeTransparent ()
289 Color clr = GetPixel(0,0);
290 MakeTransparent (clr);
293 public void MakeTransparent (Color transparentColor)
295 // We have to draw always over a 32-bitmap surface that supports alpha channel
296 Bitmap bmp = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
297 Graphics gr = Graphics.FromImage(bmp);
298 Rectangle destRect = new Rectangle(0, 0, Width, Height);
299 ImageAttributes imageAttr = new ImageAttributes();
301 imageAttr.SetColorKey(transparentColor, transparentColor);
303 gr.DrawImage (this, destRect, 0, 0, Width, Height, GraphicsUnit.Pixel, imageAttr);
305 IntPtr oldBmp = nativeObject;
306 nativeObject = bmp.nativeObject;
307 bmp.nativeObject = oldBmp;
314 public void SetResolution (float xDpi, float yDpi)
316 Status status = GDIPlus.GdipBitmapSetResolution (nativeObject, xDpi, yDpi);
317 GDIPlus.CheckStatus (status);
320 public void UnlockBits (BitmapData bitmap_data)
322 Status status = GDIPlus.GdipBitmapUnlockBits (nativeObject, bitmap_data);
323 GDIPlus.CheckStatus (status);