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
)
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
)
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
)
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
);
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
)
124 Bitmap bmpOriginal
= (Bitmap
) original
;
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
;
133 throw new NotImplementedException ();
137 void InitFromFile (string filename
)
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
)){
159 throw new FileNotFoundException ("Resource name was not found: `" + resource
+ "'");
165 public Bitmap (Image original
, int width
, int heigth
)
167 Size newSize
= new Size();
168 newSize
.Height
=heigth
;
171 BitmapFromImage(original
,newSize
);
174 public Bitmap (int width
, int height
, int stride
, PixelFormat format
, IntPtr scan0
)
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)
192 public Color
GetPixel (int x
, int y
) {
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
)
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
);
220 public Bitmap
Clone (RectangleF rect
, PixelFormat format
)
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
);
231 public static Bitmap
FromHicon (IntPtr hicon
) //TODO: Untested
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
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
)
262 Status status
= GDIPlus
.GdipCreateHBITMAPFromBitmap (nativeObject
, out HandleBmp
, background
.ToArgb ());
263 GDIPlus
.CheckStatus (status
);
268 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
269 public IntPtr
GetHicon ()
273 Status status
= GDIPlus
.GdipCreateHICONFromBitmap (nativeObject
, out HandleIcon
);
274 GDIPlus
.CheckStatus (status
);
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
);
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
;
320 BitmapFromImage(bmp
,newSize
);
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
);