Adjust Image - CSharp System.Drawing

CSharp examples for System.Drawing:Image Operation

Description

Adjust Image

Demo Code


using System.Drawing.Drawing2D;
using System.Drawing;
using System.IO;// w  ww  .  j av  a2s  .  com
using System.Collections;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        public static void AdjustImage(int thumbnailSize,string fileFullPath, Stream fileContent)
        {
            Bitmap originalBMP = new Bitmap(fileContent);

            int newWidth, newHeight;
            if (originalBMP.Width > originalBMP.Height)
            {
                newWidth = thumbnailSize;
                newHeight = originalBMP.Height * thumbnailSize / originalBMP.Width;
            }
            else
            {
                newWidth = originalBMP.Width * thumbnailSize / originalBMP.Height;
                newHeight = thumbnailSize;
            }
            Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
            Graphics oGraphics = Graphics.FromImage(newBMP);

            oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
            oGraphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);

            newBMP.Save(fileFullPath, System.Drawing.Imaging.ImageFormat.Jpeg);

            originalBMP.Dispose();
            newBMP.Dispose();
            oGraphics.Dispose();
        }
}

Related Tutorials