{
#region constructors
// constructors
+
+#if NET_2_0
+ // required for XmlSerializer (#323246)
+ private Bitmap ()
+ {
+ }
+#endif
+
internal Bitmap (IntPtr ptr)
{
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 = (IntPtr)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);
- nativeObject = (IntPtr) bmp;
+ nativeObject = bmp;
}
public Bitmap (Image original, Size newSize) : this(original, newSize.Width, newSize.Height) {}
- internal Bitmap (int width, int height, PixelFormat pixel, IntPtr bmp)
- {
- nativeObject = (IntPtr)bmp;
- }
-
- internal Bitmap (float width, float height, PixelFormat pixel, IntPtr bmp)
- {
- nativeObject = (IntPtr)bmp;
-
- }
-
public Bitmap (Stream stream, bool useIcm)
{
- if (stream == null)
- throw new ArgumentNullException ("stream");
-
- InitFromStream (stream);
+ // false: stream is owned by user code
+ nativeObject = InitFromStream (stream);
}
public Bitmap (string filename, bool useIcm)
{
+ if (filename == null)
+ throw new ArgumentNullException ("filename");
+
IntPtr imagePtr;
Status st;
public Bitmap (Type type, string resource)
{
- using (Stream s = type.Assembly.GetManifestResourceStream (type, resource)){
- if (s == null)
- throw new FileNotFoundException ("Resource name was not found: `" + resource + "'");
+ if (resource == null)
+ throw new ArgumentException ("resource");
- InitFromStream (s);
+ Stream s = type.Assembly.GetManifestResourceStream (type, resource);
+ if (s == null) {
+ string msg = Locale.GetText ("Resource '{0}' was not found.", resource);
+ throw new FileNotFoundException (msg);
}
+
+ nativeObject = InitFromStream (s);
+ // under Win32 stream is owned by SD/GDI+ code
+ if (GDIPlus.RunningOnWindows ())
+ stream = s;
}
public Bitmap (Image original, int width, int height) : this(width, height, PixelFormat.Format32bppArgb)
Status status = GDIPlus.GdipCreateBitmapFromScan0 (width, height, stride, format, scan0, out bmp);
GDIPlus.CheckStatus (status);
- nativeObject = (IntPtr) bmp;
+ nativeObject = bmp;
}
private Bitmap (SerializationInfo info, StreamingContext context)
+ : base (info, context)
{
- foreach (SerializationEntry serEnum in info) {
- if (String.Compare(serEnum.Name, "Data", true) == 0) {
- byte[] bytes = (byte[]) serEnum.Value;
-
- if (bytes != null) {
- InitFromStream(new MemoryStream(bytes));
- }
- }
- }
}
- //The below function is not required. Call should resolve to base
- //Moreover there is a problem with the declaration. Base class function
- //is not declared as protected to access in descendent class
- /*private Bitmap (SerializationInfo info, StreamingContext context) : base(info, context)
- {
- }*/
#endregion
// methods
}
public void SetPixel (int x, int y, Color color)
- {
- Status s = GDIPlus.GdipBitmapSetPixel(nativeObject, x, y, color.ToArgb());
+ {
+ Status s = GDIPlus.GdipBitmapSetPixel (nativeObject, x, y, color.ToArgb ());
+ if (s == Status.InvalidParameter) {
+ // check is done in case of an error only to avoid another
+ // unmanaged call for normal (successful) calls
+ if ((this.PixelFormat & PixelFormat.Indexed) != 0) {
+ string msg = Locale.GetText ("SetPixel cannot be called on indexed bitmaps.");
+#if NET_2_0
+ throw new InvalidOperationException (msg);
+#else
+ throw new Exception (msg);
+#endif
+ }
+ }
GDIPlus.CheckStatus (s);
}
- public Bitmap Clone (Rectangle rect,PixelFormat format)
+ public Bitmap Clone (Rectangle rect, PixelFormat format)
{
IntPtr bmp;
- Status status = GDIPlus.GdipCloneBitmapAreaI(rect.X, rect.Top, rect.Width, rect.Height,
- PixelFormat, nativeObject, out bmp);
-
+ Status status = GDIPlus.GdipCloneBitmapAreaI (rect.X, rect.Y, rect.Width, rect.Height,
+ format, nativeObject, out bmp);
GDIPlus.CheckStatus (status);
-
- Bitmap bmpnew = new Bitmap (rect.Width, rect.Height, PixelFormat, (IntPtr) bmp);
- return bmpnew;
+ return new Bitmap (bmp);
}
public Bitmap Clone (RectangleF rect, PixelFormat format)
{
IntPtr bmp;
- Status status = GDIPlus.GdipCloneBitmapArea (rect.X, rect.Top, rect.Width, rect.Height,
- PixelFormat, nativeObject, out bmp);
+ Status status = GDIPlus.GdipCloneBitmapArea (rect.X, rect.Y, rect.Width, rect.Height,
+ format, nativeObject, out bmp);
GDIPlus.CheckStatus (status);
-
- Bitmap bmpnew = new Bitmap (rect.Width, rect.Height, PixelFormat, (IntPtr) bmp);
- return bmpnew;
+ return new Bitmap (bmp);
}
- public static Bitmap FromHicon (IntPtr hicon) //TODO: Untested
+ public static Bitmap FromHicon (IntPtr hicon)
{
IntPtr bitmap;
-
Status status = GDIPlus.GdipCreateBitmapFromHICON (hicon, out bitmap);
GDIPlus.CheckStatus (status);
-
- return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap); // FIXME
+ return new Bitmap (bitmap);
}
public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TODO: Untested
{
IntPtr bitmap;
-
Status status = GDIPlus.GdipCreateBitmapFromResource (hinstance, bitmapName, out bitmap);
GDIPlus.CheckStatus (status);
-
- return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap); // FIXME
+ return new Bitmap (bitmap);
}
[EditorBrowsable (EditorBrowsableState.Advanced)]
public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format)
{
BitmapData result = new BitmapData();
-
- if (nativeObject == (IntPtr) 0)
- throw new Exception ("nativeObject is null");
-
- Status status = GDIPlus.GdipBitmapLockBits (nativeObject, ref rect, flags, format, result);
-
- //NOTE: scan0 points to piece of memory allocated in the unmanaged space
- GDIPlus.CheckStatus (status);
-
- return result;
+ return LockBits (rect, flags, format, result);
}
#if NET_2_0
- public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format, BitmapData bitmapData)
+ public
+#endif
+ BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format, BitmapData bitmapData)
{
- Status status = GDIPlus.GdipBitmapLockBits (nativeObject, ref rect, flags, format, bitmapData);
+ Status status = GDIPlus.GdipBitmapLockBits (nativeObject, ref rect, flags, format, bitmapData);
+ //NOTE: scan0 points to piece of memory allocated in the unmanaged space
GDIPlus.CheckStatus (status);
- return bitmapData;\r
- }\r
-#endif
+ return bitmapData;
+ }
public void MakeTransparent ()
{
GDIPlus.CheckStatus (status);
}
- public void UnlockBits (BitmapData bitmap_data)
+ public void UnlockBits (BitmapData bitmapdata)
{
- Status status = GDIPlus.GdipBitmapUnlockBits (nativeObject, bitmap_data);
+ Status status = GDIPlus.GdipBitmapUnlockBits (nativeObject, bitmapdata);
GDIPlus.CheckStatus (status);
}
}