/// <summary> /// Crops the image by the specified width/height /// </summary> /// <param name="Width">The width.</param> /// <param name="Height">The height.</param> /// <param name="VAlignment">The v alignment.</param> /// <param name="HAlignment">The h alignment.</param> /// <returns></returns> 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); }
protected override void CommitBuffer() { InternalBitmap.Mutate(c => c.Dither().BlackWhite()); var luminanceSource = InternalBitmap .CloneAs <L8>(); var pages = Height / 8; for (var pageAddress = 0; pageAddress < pages; pageAddress++) { var scan = new byte[Width]; // each byte represents 8 pixels in column for (var y = 0; y < 8; y++) { // row scan inside page var luminance = luminanceSource.GetPixelRowMemory(y + pageAddress * 8).ToArray(); for (var x = 0; x < Width; x++) { if (y == 0) { scan[x] = 0; } if (luminance[x].PackedValue >= 128) { scan[x] |= (byte)(0x01 << y); } } } _device.WritePage(pageAddress, scan); } }
/// <summary> /// Rotates and/or flips the image /// </summary> /// <param name="flipType">Type of flip/rotation to do</param> /// <returns>This</returns> public SwiftBitmap Rotate(RotateFlipType flipType) { Contract.Requires <NullReferenceException>(InternalBitmap != null); Unlock(); InternalBitmap.RotateFlip(flipType); return(this); }
protected override void CommitBuffer() { var luminanceSource = InternalBitmap.CloneAs <L8>(); for (var page = 0; page < Height / 8; page++) { var scan = new byte[Width]; // each byte represents 8 pixels in column for (var y = 0; y < 8; y++) { var luminance = luminanceSource.GetPixelRowMemory(y + page * 8).ToArray(); for (var x = 0; x < Width; x++) { if (y == 0) { scan[x] = 0; // initialize on the first pixel row of the scan } if (luminance[x].PackedValue >= 128) { scan[x] |= (byte)(0x80 >> y); } } } _device.WritePage(page, scan); } }
/// <summary> /// Resizes an SwiftBitmap to a certain height /// </summary> /// <param name="Width">New width for the final image</param> /// <param name="Height">New height for the final image</param> /// <param name="Quality">Quality of the resizing</param> /// <returns>This</returns> public SwiftBitmap Resize(int Width, int Height, Quality Quality = Quality.Low) { Contract.Requires <NullReferenceException>(InternalBitmap != null); Unlock(); var TempBitmap = new Bitmap(Width, Height); using (Graphics NewGraphics = Graphics.FromImage(TempBitmap)) { if (Quality == Quality.High) { NewGraphics.CompositingQuality = CompositingQuality.HighQuality; NewGraphics.SmoothingMode = SmoothingMode.HighQuality; NewGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; } else { NewGraphics.CompositingQuality = CompositingQuality.HighSpeed; NewGraphics.SmoothingMode = SmoothingMode.HighSpeed; NewGraphics.InterpolationMode = InterpolationMode.NearestNeighbor; } NewGraphics.DrawImage(InternalBitmap, new System.Drawing.Rectangle(0, 0, Width, Height)); } InternalBitmap.Dispose(); InternalBitmap = TempBitmap; this.Width = Width; this.Height = Height; return(this); }
/// <summary> /// Saves to the specified file name. /// </summary> /// <param name="fileName">Name of the file.</param> /// <returns>This</returns> public SwiftBitmap Save(string fileName) { Contract.Requires <ArgumentNullException>(!string.IsNullOrEmpty(fileName)); Contract.Requires <NullReferenceException>(InternalBitmap != null); Unlock(); InternalBitmap.Save(fileName, GetImageFormat(fileName)); return(this); }
protected override void CommitBuffer() { var dst = InternalBitmap.Clone(i => { i.ApplyProcessor(new BlackWhiteProcessor()); }); dst.Save(_imagePath); }
/// <summary> /// Converts an SwiftBitmap to a base64 string and returns it /// </summary> /// <param name="desiredFormat">Desired SwiftBitmap format (defaults to Jpeg)</param> /// <returns>The SwiftBitmap in base64 string format</returns> public string ToString(ImageFormat desiredFormat) { Contract.Requires <NullReferenceException>(InternalBitmap != null); desiredFormat = desiredFormat.Check(ImageFormat.Jpeg); using (MemoryStream Stream = new MemoryStream()) { InternalBitmap.Save(Stream, desiredFormat); return(Stream.ToArray().ToString(Base64FormattingOptions.None)); } }
/// <summary> /// Unlocks the bitmap to return the original bitmap /// </summary> /// <returns>The bitmap after finishing the edit</returns> public unsafe FastBitmap Unlock() { if (Data == null) { return(this); } InternalBitmap.UnlockBits(Data); Data = null; DataPointer = null; return(this); }
private void AcquireDevice() { var dst = InternalBitmap.Clone(i => { i.ApplyProcessor(new BlackWhiteProcessor()); }); dst.Save(_imagePath); }
/// <summary> /// Function to override in order to dispose objects /// </summary> /// <param name="Managed"> /// If true, managed and unmanaged objects should be disposed. Otherwise unmanaged objects only. /// </param> protected override void Dispose(bool Managed) { if (Data != null) { Unlock(); } if (InternalBitmap != null) { InternalBitmap.Dispose(); InternalBitmap = null; } }
/// <summary> /// Unlocks this SwiftBitmap /// </summary> /// <returns>This</returns> public unsafe SwiftBitmap Unlock() { Contract.Requires <NullReferenceException>(InternalBitmap != null); if (Data == null) { return(this); } InternalBitmap.UnlockBits(Data); Data = null; DataPointer = null; return(this); }
/// <summary> /// Copies the image from one image to this one. /// </summary> /// <param name="SwiftBitmap">The SwiftBitmap to copy from.</param> /// <returns>This</returns> public unsafe SwiftBitmap Copy(SwiftBitmap SwiftBitmap) { Contract.Requires <NullReferenceException>(InternalBitmap != null); if (SwiftBitmap == null) { return(this); } Unlock(); InternalBitmap.Dispose(); InternalBitmap = (Bitmap)SwiftBitmap.InternalBitmap.Clone(); return(this); }
/// <summary> /// Lock the bitmap to edit it through memory /// </summary> public unsafe void Lock() { if (Data != null) { return; } Data = InternalBitmap.LockBits(new Rectangle(0, 0, InternalBitmap.Width, InternalBitmap.Height), ImageLockMode.ReadWrite, InternalBitmap.PixelFormat); PixelSize = GetPixelSize(); DataPointer = (byte *)Data.Scan0; }
/// <summary> /// Locks this instance. /// </summary> /// <returns>This</returns> public unsafe SwiftBitmap Lock() { Contract.Requires <NullReferenceException>(InternalBitmap != null); if (Data != null) { return(this); } Data = InternalBitmap.LockBits(new Rectangle(0, 0, InternalBitmap.Width, InternalBitmap.Height), ImageLockMode.ReadWrite, InternalBitmap.PixelFormat); PixelSize = GetPixelSize(); DataPointer = (byte *)Data.Scan0; return(this); }
/// <summary> /// Rotates an image /// </summary> /// <param name="DegreesToRotate">Degrees to rotate the image</param> /// <returns>This</returns> public SwiftBitmap Rotate(float DegreesToRotate) { Contract.Requires <NullReferenceException>(InternalBitmap != null); Unlock(); var TempBitmap = new Bitmap(Width, Height); using (Graphics NewGraphics = Graphics.FromImage(TempBitmap)) { NewGraphics.TranslateTransform((float)Width / 2.0f, (float)Height / 2.0f); NewGraphics.RotateTransform(DegreesToRotate); NewGraphics.TranslateTransform(-(float)Width / 2.0f, -(float)Height / 2.0f); NewGraphics.DrawImage(InternalBitmap, new System.Drawing.Rectangle(0, 0, Width, Height), new System.Drawing.Rectangle(0, 0, Width, Height), GraphicsUnit.Pixel); } InternalBitmap.Dispose(); InternalBitmap = TempBitmap; return(this); }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// <returns>A new object that is a copy of this instance.</returns> public object Clone() { Unlock(); return(new SwiftBitmap((Bitmap)InternalBitmap.Clone())); }