locked
Using C# how can I resize a jpeg image in c#? RRS feed

  • Question

  • User1140724383 posted

    Hi there,

    I need resizing an Image file in C#, from those wich are common used at least (bmp, jpg, jpeg, png, etc etc.)

    it seems to me that I have to use a class .cs file

    I found many snippets but not a really complete one.

    So I'm gonna ask so who comes here might use a complete file.

    I've tried using this on code-behind without success

    how should I use the ImageHelper class .cs on an .aspx.cs file?

    .aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Default7 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
    
            }
        }
    }

    .cs

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Web;
    
    public static class ImageHelper
    {
        /// <summary>
        /// Resize the image to the specified width and height.
        /// </summary>
        /// <param name="image">The image to resize.</param>
        /// <param name="width">The width to resize to.</param>
        /// <param name="height">The height to resize to.</param>
        /// <returns>The resized image.</returns>
        public static Bitmap ResizeImage(Image image, int width, int height)
        {
            var destRect = new Rectangle(0, 0, width, height);
            var destImage = new Bitmap(width, height);
    
            destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
    
            using (var graphics = Graphics.FromImage(destImage))
            {
                graphics.CompositingMode = CompositingMode.SourceCopy;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
    
                using (var wrapMode = new ImageAttributes())
                {
                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                    graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
                }
            }
    
            return destImage;
        }
    
        public static Bitmap ResizeImage(Image image, decimal percentage)
        {
            int width = (int)Math.Round(image.Width * percentage, MidpointRounding.AwayFromZero);
            int height = (int)Math.Round(image.Height * percentage, MidpointRounding.AwayFromZero);
            return ResizeImage(image, width, height);
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @"C:\Images\MyImage.jpg";
            FileInfo info = new FileInfo(fileName);
            using (Image image = Image.FromFile(fileName))
            {
                using (Bitmap resizedImage = ImageHelper.ResizeImage(image, 0.25m))
                {
                    resizedImage.Save(
                        info.DirectoryName + "\\"
                            + info.Name.Substring(0, info.Name.LastIndexOf(info.Extension))
                            + "_" + resizedImage.Width + "_" + resizedImage.Height
                            + info.Extension,
                        ImageFormat.Jpeg);
                }
            }
        }
    }

    Friday, May 29, 2020 1:11 PM

Answers

  • User-2004582644 posted

    Hi,

    Try:

            string fileName = @"C:\Images\MyImage.jpg";
            FileInfo info = new FileInfo(fileName);
            using (Image image = Image.FromFile(fileName))
            {
                using (Bitmap resizedImage = ImageHelper.ResizeImage(image, 0.25m))
                {
                    resizedImage.Save(
                        info.DirectoryName + "\\"
                            + info.Name.Substring(0, info.Name.LastIndexOf(info.Extension))
                            + "_" + resizedImage.Width + "_" + resizedImage.Height
                            + info.Extension,
                        ImageFormat.Jpeg);
                }
            }

    Hope this help

    Chevy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 30, 2020 6:48 AM