/// <summary> /// Factory for creating a FastBitmap as a destination for the source /// </summary> /// <param name="source">Bitmap to clone</param> /// <param name="pixelFormat">Pixelformat of the cloned bitmap</param> /// <param name="area">Area of the bitmap to access, can be Rectangle.Empty for the whole</param> /// <returns>IFastBitmap</returns> public static IFastBitmap CreateCloneOf(Image source, PixelFormat pixelFormat, Rectangle area) { Bitmap destination = ImageHelper.CloneArea(source, area, pixelFormat); FastBitmap fastBitmap = Create(destination) as FastBitmap; fastBitmap.NeedsDispose = true; fastBitmap.Left = area.Left; fastBitmap.Top = area.Top; return(fastBitmap); }
/// <summary> /// Create a BitmapBuffer from a Bitmap, a Rectangle specifying what part from the Bitmap to take and a flag if we need a clone /// </summary> /// <param name="sourceBmp">Bitmap</param> /// <param name="applyRect">Rectangle</param> /// <param name="clone">bool specifying if the bitmap needs to be cloned</param> public BitmapBuffer(Bitmap sourceBmp, Rectangle applyRect, bool clone) { this.clone = clone; Rectangle sourceRect = new Rectangle(applyRect.X, applyRect.Y, applyRect.Width, applyRect.Height); Rectangle bitmapRect = new Rectangle(0, 0, sourceBmp.Width, sourceBmp.Height); if (sourceRect.IsEmpty) { sourceRect = bitmapRect; } else { sourceRect.Intersect(bitmapRect); } // Does the rect have any pixels? if (sourceRect.Height <= 0 || sourceRect.Width <= 0) { return; } if (clone) { this.bitmap = ImageHelper.CloneArea(sourceBmp, sourceRect, PixelFormat.DontCare); // Set "this" rect to location 0,0 // as the Cloned Bitmap is only the part we want to work with this.rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); } else if (!ImageHelper.SupportsPixelFormat(sourceBmp) && PixelFormat.Format8bppIndexed != sourceBmp.PixelFormat) { throw new ArgumentException("Unsupported pixel format: " + sourceBmp.PixelFormat + " set clone to true!"); } else { this.bitmap = sourceBmp; this.rect = sourceRect; } }