Utilities.Media.SwiftBitmap.Crop C# (CSharp) Method

Crop() public method

Crops the image by the specified width/height
public Crop ( int Width, int Height, Align VAlignment, Align HAlignment ) : SwiftBitmap
Width int The width.
Height int The height.
VAlignment Align The v alignment.
HAlignment Align The h alignment.
return SwiftBitmap
        public SwiftBitmap Crop(int Width, int Height, Align VAlignment, Align HAlignment)
        {
            Contract.Requires<NullReferenceException>(InternalBitmap != null);
            Unlock();
            var TempRectangle = new System.Drawing.Rectangle();
            TempRectangle.Height = Height;
            TempRectangle.Width = Width;
            TempRectangle.Y = VAlignment == Align.Top ? 0 : this.Height - Height;
            if (TempRectangle.Y < 0)
                TempRectangle.Y = 0;
            TempRectangle.X = HAlignment == Align.Left ? 0 : this.Width - Width;
            if (TempRectangle.X < 0)
                TempRectangle.X = 0;
            var TempHolder = InternalBitmap.Clone(TempRectangle, InternalBitmap.PixelFormat);
            InternalBitmap.Dispose();
            InternalBitmap = TempHolder;
            this.Width = Width;
            this.Height = Height;
            return this;
        }

Usage Example

 public void Crop()
 {
     using (Utilities.Media.SwiftBitmap TestObject = new Utilities.Media.SwiftBitmap(@"..\..\Data\Image\Lenna.jpg"))
     {
         TestObject.Crop(100, 100, Align.Bottom, Align.Right).Save(@".\Testing\LennaCrop.jpg");
         Assert.True(new Utilities.IO.FileInfo(@".\Testing\LennaCrop.jpg").ReadBinary().SequenceEqual(new Utilities.IO.FileInfo(@"..\..\BitmapResults\LennaCrop.jpg").ReadBinary()));
     }
 }