/// <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; } }