/// <summary> /// Converts this canvas to <see cref="Image"/>. /// </summary> /// <param name="rect">The area to crop from the bitmap.</param> /// <returns>The <see cref="Imaging.Image"/> this method creates.</returns> public Image ToImage(Rectangle rect) { using (System.Drawing.Bitmap bmp = this.ToBitmap()) { if (bmp == null) { return(null); } return(rect.IsEmpty ? BitmapExtensions.FromBitmap(bmp) : BitmapExtensions.FromBitmap(bmp, rect)); } }
/// <summary> /// Creates an <see cref="Image"/> from a specified area of an encapsulated GDI+ bitmap. /// </summary> /// <param name="bitmap">The GDI+ bitmap from which to create the <see cref="Image"/>.</param> /// <param name="rect">Defines the portion of the <paramref name="bitmap"/> to copy. Coordinates are relative to <paramref name="bitmap"/>.</param> /// <returns> /// The <see cref="Image"/> this method creates. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="bitmap"/> is <b>null</b>. /// </exception> public static Image FromBitmap(System.Drawing.Bitmap bitmap, Rectangle rect) { if (bitmap == null) { throw new ArgumentNullException(nameof(bitmap)); } using (System.Drawing.Bitmap clonedBitmap = bitmap.Clone( new System.Drawing.Rectangle(rect.X, rect.Y, rect.Width, rect.Height), bitmap.PixelFormat)) { return(BitmapExtensions.FromBitmap(clonedBitmap)); } }