Resize hình ảnh

 

 

public void ResizeImage(string filepath, int imageSize)
{
    var newImage = new Bitmap(Server.MapPath(filepath));
    int thumbnailSize = imageSize;
    int newWidth, newHeight;

    if (newImage.Width > newImage.Height)
    {
        newWidth = thumbnailSize;
        newHeight = newImage.Height * thumbnailSize / newImage.Width;
    }
    else
    {
        newWidth = newImage.Width * thumbnailSize / newImage.Height;
        newHeight = thumbnailSize;
    }

    var thumbnailBitmap = new Bitmap(newWidth, newHeight);

    var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
    thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
    thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
    thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

    var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
    thumbnailGraph.DrawImage(newImage, imageRectangle);

    filepath = filepath.Replace("/StoredImages/Images/", "/StoredImages/Thumbs/");
    thumbnailBitmap.Save(Server.MapPath(filepath));
    thumbnailGraph.Dispose();
    thumbnailBitmap.Dispose();
    newImage.Dispose();
}