Crop Bitmap by Rectangle - CSharp System.Drawing

CSharp examples for System.Drawing:Image Size

Description

Crop Bitmap by Rectangle

Demo Code


using System.Drawing;

public class Main{
        /// <summary>
        /// Recorta uma imagem a partir das coordenadas do retangulo informado
        /// </summary>
        /// <param name="source">Bitmap a ser recortado</param>
        /// <param name="section">Retangulo com as coordenadas do recorte</param>
        /// <returns>byte arrah da imagem recortada</returns>
        public static byte[] Crop(this Bitmap source, Rectangle section)
        {/* w  ww. ja va2 s .  c  om*/
            // An empty bitmap which will hold the cropped image
            Bitmap bmp = new Bitmap(section.Width, section.Height);

            Graphics g = Graphics.FromImage(bmp);

            // Draw the given area (section) of the source image
            // at location 0,0 on the empty bitmap (bmp)
            g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);

            return bmp.ToByteArray();
        }
        /// <summary>
        /// Converte um Bitmap em byte array
        /// </summary>
        /// <param name="instance">Bitmap a ser convertido</param>
        /// <returns>byte array convertido</returns>
        public static byte[] ToByteArray(this Bitmap instance)
        {
            ImageConverter converter = new ImageConverter();
            return (byte[])converter.ConvertTo(instance, typeof(byte[]));
        }
}

Related Tutorials