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 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.
40 using System.Drawing.Imaging;
41 using System.Runtime.Serialization;
42 using System.Runtime.InteropServices;
43 using System.ComponentModel;
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)
69 Status s = GDIPlus.GdipCreateBitmapFromGraphics (width, height, g.nativeObject, out bmp);
70 GDIPlus.CheckStatus (s);
71 nativeObject = (IntPtr)bmp;
75 public Bitmap (int width, int height, PixelFormat format)
80 Status s = GDIPlus.GdipCreateBitmapFromScan0 (width, height, 0, format, IntPtr.Zero,
82 GDIPlus.CheckStatus (s);
83 nativeObject = (IntPtr) bmp;
87 public Bitmap (Image original) : this (original.Width, original.Height, PixelFormat.Format32bppArgb)
89 BitmapFromImage(original, original.Size);
92 public Bitmap (Stream stream) : this (stream, false) {}
94 public Bitmap (string filename) : this (filename, false) {}
96 public Bitmap (Image original, Size newSize) : this (newSize.Width, newSize.Height, PixelFormat.Format32bppArgb)
101 g=Graphics.FromImage(this);
103 status = GDIPlus.GdipDrawImageRectRectI(g.nativeObject, original.nativeObject,
104 0, 0, newSize.Width, newSize.Height,
105 0, 0, original.Width, original.Height,
106 GraphicsUnit.Pixel, IntPtr.Zero, null, IntPtr.Zero);
107 GDIPlus.CheckStatus (status);
112 internal Bitmap (int width, int height, PixelFormat pixel, IntPtr bmp)
114 nativeObject = (IntPtr)bmp;
117 internal Bitmap (float width, float height, PixelFormat pixel, IntPtr bmp)
119 nativeObject = (IntPtr)bmp;
123 internal void BitmapFromImage(Image original, Size newSize){
125 if (original is Bitmap) {
127 if (nativeObject!=IntPtr.Zero) Dispose();
132 Bitmap bmpOriginal = (Bitmap) original;
135 Status s = GDIPlus.GdipCloneBitmapAreaI (0, 0, newSize.Width, newSize.Height, bmpOriginal.PixelFormat, bmpOriginal.nativeObject, out bmp);
136 GDIPlus.CheckStatus (s);
137 nativeObject = (IntPtr) bmp;
141 throw new NotImplementedException ();
145 void InitFromFile (string filename)
150 Status st = GDIPlus.GdipLoadImageFromFile (filename, out imagePtr);
151 GDIPlus.CheckStatus (st);
152 nativeObject = imagePtr;
156 public Bitmap (Stream stream, bool useIcm)
158 InitFromStream (stream);
161 public Bitmap (string filename, bool useIcm)
163 InitFromFile (filename);
166 public Bitmap (Type type, string resource)
168 using (Stream s = type.Assembly.GetManifestResourceStream (resource)){
170 throw new FileNotFoundException ("Resource name was not found: `" + resource + "'");
176 public Bitmap (Image original, int width, int heigth)
178 Size newSize = new Size();
179 newSize.Height=heigth;
182 BitmapFromImage(original,newSize);
185 public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
191 Status status = GDIPlus.GdipCreateBitmapFromScan0 (width, height, stride, format, scan0, out bmp);
192 GDIPlus.CheckStatus (status);
193 nativeObject = (IntPtr) bmp;
198 //The below function is not required. Call should resolve to base
199 //Moreover there is a problem with the declaration. Base class function
200 //is not declared as protected to access in descendent class
201 /*private Bitmap (SerializationInfo info, StreamingContext context) : base(info, context)
207 public Color GetPixel (int x, int y) {
211 Status s = GDIPlus.GdipBitmapGetPixel(nativeObject, x, y, out argb);
212 GDIPlus.CheckStatus (s);
214 return Color.FromArgb(argb);
217 public void SetPixel (int x, int y, Color color)
219 Status s = GDIPlus.GdipBitmapSetPixel(nativeObject, x, y, color.ToArgb());
220 GDIPlus.CheckStatus (s);
223 public Bitmap Clone (Rectangle rect,PixelFormat format)
226 Status status = GDIPlus.GdipCloneBitmapAreaI(rect.X, rect.Top, rect.Width, rect.Height,
227 PixelFormat, nativeObject, out bmp);
229 GDIPlus.CheckStatus (status);
231 Bitmap bmpnew = new Bitmap (rect.Width, rect.Height, PixelFormat, (IntPtr) bmp);
235 public Bitmap Clone (RectangleF rect, PixelFormat format)
238 Status status = GDIPlus.GdipCloneBitmapArea (rect.X, rect.Top, rect.Width, rect.Height,
239 PixelFormat, nativeObject, out bmp);
240 GDIPlus.CheckStatus (status);
242 Bitmap bmpnew = new Bitmap (rect.Width, rect.Height, PixelFormat, (IntPtr) bmp);
246 public static Bitmap FromHicon (IntPtr hicon) //TODO: Untested
250 Status status = GDIPlus.GdipCreateBitmapFromHICON (hicon, out bitmap);
251 GDIPlus.CheckStatus (status);
253 return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap); // FIXME
256 public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TODO: Untested
260 Status status = GDIPlus.GdipCreateBitmapFromResource (hinstance, bitmapName, out bitmap);
261 GDIPlus.CheckStatus (status);
263 return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap); // FIXME
266 [EditorBrowsable (EditorBrowsableState.Advanced)]
267 public IntPtr GetHbitmap ()
269 return GetHbitmap(Color.Gray);
272 [EditorBrowsable (EditorBrowsableState.Advanced)]
273 public IntPtr GetHbitmap (Color background)
277 Status status = GDIPlus.GdipCreateHBITMAPFromBitmap (nativeObject, out HandleBmp, background.ToArgb ());
278 GDIPlus.CheckStatus (status);
283 [EditorBrowsable (EditorBrowsableState.Advanced)]
284 public IntPtr GetHicon ()
288 Status status = GDIPlus.GdipCreateHICONFromBitmap (nativeObject, out HandleIcon);
289 GDIPlus.CheckStatus (status);
294 public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format)
296 BitmapData result = new BitmapData();
298 if (nativeObject == (IntPtr) 0)
299 throw new Exception ("nativeObject is null");
301 IntPtr lfBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(result));
302 Marshal.StructureToPtr(result, lfBuffer, false);
304 Status status = GDIPlus.GdipBitmapLockBits (nativeObject, ref rect, flags, format, lfBuffer);
306 result = (BitmapData) Marshal.PtrToStructure(lfBuffer, typeof(BitmapData));
307 Marshal.FreeHGlobal (lfBuffer);
308 //NOTE: scan0 points to piece of memory allocated in the unmanaged space
309 GDIPlus.CheckStatus (status);
314 public void MakeTransparent ()
316 Color clr = GetPixel(0,0);
317 MakeTransparent (clr);
320 public void MakeTransparent (Color transparentColor)
322 Bitmap bmp = new Bitmap(Width, Height, PixelFormat);
323 Graphics gr = Graphics.FromImage(bmp);
324 Rectangle destRect = new Rectangle(0,0, Width, Height);
325 ImageAttributes imageAttr = new ImageAttributes();
327 imageAttr.SetColorKey(transparentColor, transparentColor);
329 gr.DrawImage (this, destRect, 0, 0, Width, Height, GraphicsUnit.Pixel, imageAttr);
331 Size newSize = new Size();
332 newSize.Height=Height;
334 BitmapFromImage(bmp,newSize);
340 public void SetResolution (float xDpi, float yDpi)
342 Status status = GDIPlus.GdipBitmapSetResolution (nativeObject, xDpi, yDpi);
343 GDIPlus.CheckStatus (status);
346 public void UnlockBits (BitmapData bitmap_data)
348 Status status = GDIPlus.GdipBitmapUnlockBits (nativeObject, bitmap_data);
349 GDIPlus.CheckStatus (status);