ImageSelection.ResizeImageFromStream C# (CSharp) Méthode

ResizeImageFromStream() public static méthode

public static ResizeImageFromStream ( int MaxSideSize, Stream Buffer ) : byte[]
MaxSideSize int
Buffer Stream
Résultat byte[]
    public static byte[] ResizeImageFromStream(int MaxSideSize, Stream Buffer)
    {
        byte[] byteArray = null;
        try
        {
            Bitmap bitMap = new Bitmap(Buffer);
            int intOldWidth = bitMap.Width;
            int intOldHeight = bitMap.Height;

            int intNewWidth;
            int intNewHeight;

            int intMaxSide;

            if (intOldWidth >= intOldHeight)
                intMaxSide = intOldWidth;
            else
                intMaxSide = intOldHeight;

            if (intMaxSide > MaxSideSize)
            {
                //set new width and height
                double dblCoef = MaxSideSize / (double)intMaxSide;
                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(oImg, oRectangle);
            MemoryStream ms = new MemoryStream();
            oThumbNail.Save(ms, ImageFormat.Jpeg);
            byteArray = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));

            oGraphic.Dispose();
            oImg.Dispose();
            ms.Close();
            ms.Dispose();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return byteArray;
    }