Image Resize İşlemleri

Image Resize İşlemleri

Image Resize İşlemleri

17.9.2015

Sizler için projelerimizde kullandığımız Image Resize Class'ını blogumda paylaşmak istedim. Class C# dilindedir ancak her türlü dile uyarlanabilir. Yardımcı olacağını umuyorum ;)

 
public class ImageResize
    {
        public static byte[] ResizeFromByteArray(string fileName, int MaxSideSize, Byte[] byteArrayIn)
        {
            byte[] byteArray = null;  // really make this an error gif
            MemoryStream ms = new MemoryStream(byteArrayIn);
            byteArray = ResizeFromStream(fileName, MaxSideSize, ms);
            return byteArray;
        }
        public static byte[] ResizeFromStream(string fileName, int MaxSideSize, Stream Buffer)
        {
            byte[] byteArray = null;  // really make this an error gif

            try
            {
                Bitmap bitMap = new Bitmap(Buffer);
                int bigSideSize = 0;

                if (bitMap.Height > bitMap.Width)
                {
                    bigSideSize = bitMap.Width;
                }
                else if (bitMap.Height < bitMap.Width)
                {
                    bigSideSize = bitMap.Height;
                }
                else if (bitMap.Height == bitMap.Width)
                {
                    bigSideSize = bitMap.Height;
                }

                int intOldWidth = bitMap.Width;
                int intOldHeight = bitMap.Height;

                int intNewWidth;
                int intNewHeight;

                if (bigSideSize > MaxSideSize)
                {
                    //set new width and height
                    double dblCoef = MaxSideSize / (double)bigSideSize;
                    intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
                    intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
                }
                else
                {
                    intNewWidth = intOldWidth;
                    intNewHeight = intOldHeight;
                }

                Size ThumbNailSize = new Size(intNewWidth, intNewHeight);
                // System.Drawing.Image oImg = System.Drawing.Image.FromStream(Buffer);
                System.Drawing.Image oThumbNail = new Bitmap
                    (ThumbNailSize.Width, ThumbNailSize.Height);
                Graphics oGraphic = Graphics.FromImage(oThumbNail);
                oGraphic.CompositingQuality = CompositingQuality.HighQuality;
                oGraphic.SmoothingMode = SmoothingMode.HighQuality;
                oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                Rectangle oRectangle = new Rectangle
                    (0, 0, ThumbNailSize.Width, ThumbNailSize.Height);

                oGraphic.DrawImage(System.Drawing.Image.FromStream(Buffer), oRectangle);

                MemoryStream ms = new MemoryStream();
                ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
                System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
                EncoderParameters myEncoderParameters = new EncoderParameters(1);
                EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 80L);
                //
                // This is where you set the image quality -> 100L = no compression, 0L = max compression
                //
                myEncoderParameters.Param[0] = myEncoderParameter;

                oThumbNail.Save(ms, jgpEncoder, myEncoderParameters);
                byteArray = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));

                bitMap.Dispose();
                oGraphic.Dispose();
                //oImg.Dispose();
                ms.Close();
                ms.Dispose();
            }
            catch (Exception ex)
            {
                int newSize = MaxSideSize - 20;
                Bitmap bitMap = new Bitmap(newSize, newSize);
                Graphics g = Graphics.FromImage(bitMap);
                g.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(0, 0, newSize, newSize));

                Font font = new Font("Courier", 8);
                SolidBrush solidBrush = new SolidBrush(Color.Red);
                g.DrawString("Failed File", font, solidBrush, 10, 5);
                g.DrawString(fileName, font, solidBrush, 10, 50);

                MemoryStream ms = new MemoryStream();

                bitMap.Save(ms, ImageFormat.Jpeg);
                byteArray = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));

                ms.Close();
                ms.Dispose();
                bitMap.Dispose();
                solidBrush.Dispose();
                g.Dispose();
                font.Dispose();
                
            }
            return byteArray;
        }

        public static byte[] cropImage(byte[] imga, int left, int top, int width = 100, int height = 100)
        {

            try
            {
                MemoryStream ms = new MemoryStream(imga);
                Image img = Image.FromStream(ms);
                Bitmap bmp = new Bitmap(100, 100, PixelFormat.Format24bppRgb);
                bmp.SetResolution(80, 80);
                Graphics graphics = Graphics.FromImage(bmp);
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                graphics.DrawImage(img, new Rectangle(0, 0, width, height), left, top, width, height, GraphicsUnit.Pixel);

                img.Dispose();
                graphics.Dispose();

                using (MemoryStream ms2 = new MemoryStream())
                {
                    // Convert Image to byte[]

                    ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
                    System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
                    EncoderParameters myEncoderParameters = new EncoderParameters(1);
                    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
                    myEncoderParameters.Param[0] = myEncoderParameter;

                    bmp.Save(ms2, jgpEncoder, myEncoderParameters);
                    bmp.Dispose();

                    return ms2.ToArray();
                }

                bmp.Dispose();
                return new byte[0];
            }

            catch (Exception ex)
            {
                return new byte[0];
            }
        }
        private static ImageCodecInfo GetEncoder(ImageFormat format)
        {

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }

    }

İLETİŞİM

İletişim Bilgileri

Adres
Kızlarpınarı Mh. Spor Cd. Can Apt. No:28/A Alanya, Antalya
Telefon
(+90) 242-966-2119
Email
info@tulparyazilim.com

İletişime Geç