nativeObject = ptr;
}
+ // Usually called when cloning images that need to have
+ // not only the handle saved, but also the underlying stream
+ // (when using MS GDI+ and IStream we must ensure the stream stays alive for all the life of the Image)
+ internal Bitmap(IntPtr ptr, Stream stream)
+ {
+ // under Win32 stream is owned by SD/GDI+ code
+ if (GDIPlus.RunningOnWindows ())
+ this.stream = stream;
+ nativeObject = ptr;
+ }
+
public Bitmap (int width, int height) : this (width, height, PixelFormat.Format32bppArgb)
{
}
public Bitmap (int width, int height, Graphics g)
{
+ if (g == null)
+ throw new ArgumentNullException ("g");
+
IntPtr bmp;
Status s = GDIPlus.GdipCreateBitmapFromGraphics (width, height, g.nativeObject, out bmp);
GDIPlus.CheckStatus (s);
- nativeObject = bmp;
+ nativeObject = bmp;
}
public Bitmap (int width, int height, PixelFormat format)
- {
+ {
IntPtr bmp;
Status s = GDIPlus.GdipCreateBitmapFromScan0 (width, height, 0, format, IntPtr.Zero, out bmp);
GDIPlus.CheckStatus (s);
public Bitmap (string filename, bool useIcm)
{
+ if (filename == null)
+ throw new ArgumentNullException ("filename");
+
IntPtr imagePtr;
Status st;
public Bitmap (Type type, string resource)
{
+ if (resource == null)
+ throw new ArgumentException ("resource");
+
Stream s = type.Assembly.GetManifestResourceStream (type, resource);
if (s == null) {
string msg = Locale.GetText ("Resource '{0}' was not found.", resource);
}
public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
- {
+ {
IntPtr bmp;
-
+
Status status = GDIPlus.GdipCreateBitmapFromScan0 (width, height, stride, format, scan0, out bmp);
- GDIPlus.CheckStatus (status);
- nativeObject = bmp;
+ GDIPlus.CheckStatus (status);
+ nativeObject = bmp;
}
private Bitmap (SerializationInfo info, StreamingContext context)
// methods
public Color GetPixel (int x, int y) {
- int argb;
-
+ int argb;
+
Status s = GDIPlus.GdipBitmapGetPixel(nativeObject, x, y, out argb);
GDIPlus.CheckStatus (s);
- return Color.FromArgb(argb);
+ return Color.FromArgb(argb);
}
public void SetPixel (int x, int y, Color color)
}
public Bitmap Clone (Rectangle rect, PixelFormat format)
- {
- IntPtr bmp;
+ {
+ IntPtr bmp;
Status status = GDIPlus.GdipCloneBitmapAreaI (rect.X, rect.Y, rect.Width, rect.Height,
format, nativeObject, out bmp);
GDIPlus.CheckStatus (status);
return new Bitmap (bmp);
- }
+ }
public Bitmap Clone (RectangleF rect, PixelFormat format)
{
- IntPtr bmp;
+ IntPtr bmp;
Status status = GDIPlus.GdipCloneBitmapArea (rect.X, rect.Y, rect.Width, rect.Height,
format, nativeObject, out bmp);
GDIPlus.CheckStatus (status);
public static Bitmap FromHicon (IntPtr hicon)
{
- IntPtr bitmap;
+ IntPtr bitmap;
Status status = GDIPlus.GdipCreateBitmapFromHICON (hicon, out bitmap);
GDIPlus.CheckStatus (status);
return new Bitmap (bitmap);
public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TODO: Untested
{
- IntPtr bitmap;
+ IntPtr bitmap;
Status status = GDIPlus.GdipCreateBitmapFromResource (hinstance, bitmapName, out bitmap);
GDIPlus.CheckStatus (status);
return new Bitmap (bitmap);
Status status = GDIPlus.GdipCreateHICONFromBitmap (nativeObject, out HandleIcon);
GDIPlus.CheckStatus (status);
- return HandleIcon;
+ return HandleIcon;
}
public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format)
public void MakeTransparent ()
{
- Color clr = GetPixel(0,0);
+ Color clr = GetPixel(0,0);
MakeTransparent (clr);
}
public void MakeTransparent (Color transparentColor)
- {
+ {
// We have to draw always over a 32-bitmap surface that supports alpha channel
Bitmap bmp = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
Graphics gr = Graphics.FromImage(bmp);
imageAttr.SetColorKey(transparentColor, transparentColor);
- gr.DrawImage (this, destRect, 0, 0, Width, Height, GraphicsUnit.Pixel, imageAttr);
+ gr.DrawImage (this, destRect, 0, 0, Width, Height, GraphicsUnit.Pixel, imageAttr);
IntPtr oldBmp = nativeObject;
nativeObject = bmp.nativeObject;