locked
shrink an image RRS feed

  • Question

  • User-125499312 posted

    if <g class="gr_ gr_30 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="30" data-gr-id="30">i</g> have an image (jpg, png, etc..) <g class="gr_ gr_66 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="66" data-gr-id="66">i</g> would like to shrink to maintain its proportions

    <g class="gr_ gr_149 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="149" data-gr-id="149">i</g> would like to take the height or the width - whichever is bigger and shrink it down to a specific size and maintain the proportions of the image.

    how would <g class="gr_ gr_362 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="362" data-gr-id="362">i</g> do this?

    thx for ur help

    Tuesday, January 29, 2019 10:49 PM

All replies

  • User-943250815 posted

    To make changes on images I use ImageProcessor you can get it on Nuget
    For resize you get a sample here http://imageprocessor.org/imageprocessor/imagefactory/

    Or you can work with System.Drawing

    Wednesday, January 30, 2019 12:01 AM
  • User-1174608757 posted

    Hi yzidell,

    According to your description, I have made a sample here.I suggest you to use a class to shrink your image.

    You could firstly get the image in the folder by entering the path, then you could resize your image depend on its width or height ,or just the whole picture.

    Here is the demo, I hope it could help you.

    class1: 

      public void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
            {
                System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);// get the image in the path you enter
    
                int towidth = width;
                int toheight = height;
    
                int x = 0;
                int y = 0;
                int ow = originalImage.Width;
                int oh = originalImage.Height;
    
                switch (mode)
                {
                    case "HW": //"HW"  is to  resize the image according the width and height specified which may cause distortion
                        break;
                    case "W": //"W" is to resize the image according to the Width
                        toheight = originalImage.Height * width / originalImage.Width;
                        break;
                    case "H"://"H" is to resize the image according to the height
                        towidth = originalImage.Width * height / originalImage.Height;
                        break;
                    case "Cut"://"Cut" is to resize the image according to the width and height specified which doesn’t cause distortion.
                        if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                        {
                            oh = originalImage.Height;
                            ow = originalImage.Height * towidth / toheight;
                            y = 0;
                            x = (originalImage.Width - ow) / 2;
                        }
                        else
                        {
                            ow = originalImage.Width;
                            oh = originalImage.Width * height / towidth;
                            x = 0;
                            y = (originalImage.Height - oh) / 2;
                        }
                        break;
                    default:
                        break;
                }
    
    
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
    
    
                Graphics g = System.Drawing.Graphics.FromImage(bitmap);
    
    
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    
    
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    
    
                g.Clear(Color.Transparent);
    
    
                g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
                 new Rectangle(x, y, ow, oh),
                 GraphicsUnit.Pixel);
                try
                {
    
                    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    
                }
                catch (System.Exception e)
                {
                    throw e;
                }
                finally
                {
                    originalImage.Dispose();
                    bitmap.Dispose();
                    g.Dispose();
                }
    
    
            }

    In code behind , you could write as below:

     public partial class image : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string path = Server.MapPath("~/images");
                // get all the absolute pathes  of files in the original folder
                string[] images = Directory.GetFiles(path);
                //get the absolute path of the targeted folder
                string target = Server.MapPath("~/NewFolder2");    
                Class1 resizePicture = new Class1();           
                foreach (var item in images)
                {
    //choose the way you would like to resize the picture resizePicture.MakeThumbnail(item, target + "/" + item.Substring(item.LastIndexOf("\\")), 300, 300, "Cut"); // delete the original images File.Delete(item); } } }

     you could now find the picture in folder has been shrinked as below

    Best Regards

    Wei Zhang

    Wednesday, January 30, 2019 5:18 AM