Resize image in C #

2

I would like to know how to resize images in C #. I will receive images in both JPG, JPEG, GIF and PNG.

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceformCalcularIdade{publicpartialclassForm1:Form{privateint_anoAtual;privateint_anoNasc;privateint_idade;publicForm1(){InitializeComponent();}privatevoidLimpar(objectsender,EventArgse){txtAnoAtual.Clear();txtDateNasc.Clear();}privatevoidCalcular(objectsender,EventArgse){this._anoAtual=Convert.ToInt32(txtAnoAtual.Text);this._anoNasc=Convert.ToInt32(txtDateNasc.Text);this._idade=_anoAtual-_anoNasc;txtDateIdade.Text=Convert.ToString(this._idade);}privatevoiduploadFoto(objectsender,EventArgse){OpenFileDialogdataImage=newOpenFileDialog();dataImage.Filter="Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.BMP;*.JPG;*.JPEG;*.PNG";
            if (dataImage.ShowDialog() == DialogResult.OK)
            {

                pictureBox1.ImageLocation = dataImage.FileName;


            }
        }

        private void txtAnoAtual_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }


    }
}
    
asked by anonymous 25.08.2018 / 16:24

1 answer

4

Hello, you can do it this way:

Then in your code you call this method, for example:

var retImage = resizeImage(800, 600, dataImage.FileName); 

Resize Method:

public Image resizeImage(int newWidth, int newHeight, string stPhotoPath)
{
     Image imgPhoto = Image.FromFile(stPhotoPath); 

     int sourceWidth = imgPhoto.Width;
     int sourceHeight = imgPhoto.Height;

     //Consider vertical pics
    if (sourceWidth < sourceHeight)
    {
        int buff = newWidth;

        newWidth = newHeight;
        newHeight = buff;
    }

    int sourceX = 0, sourceY = 0, destX = 0, destY = 0;
    float nPercent = 0, nPercentW = 0, nPercentH = 0;

    nPercentW = ((float)newWidth / (float)sourceWidth);
    nPercentH = ((float)newHeight / (float)sourceHeight);
    if (nPercentH < nPercentW)
    {
        nPercent = nPercentH;
        destX = System.Convert.ToInt16((newWidth -
                  (sourceWidth * nPercent)) / 2);
    }
    else
    {
        nPercent = nPercentW;
        destY = System.Convert.ToInt16((newHeight -
                  (sourceHeight * nPercent)) / 2);
    }

    int destWidth = (int)(sourceWidth * nPercent);
    int destHeight = (int)(sourceHeight * nPercent);


    Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
                  PixelFormat.Format24bppRgb);

    bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                 imgPhoto.VerticalResolution);

    Graphics grPhoto = Graphics.FromImage(bmPhoto);
    grPhoto.Clear(Color.Black);
    grPhoto.InterpolationMode =
        System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

    grPhoto.DrawImage(imgPhoto,
        new Rectangle(destX, destY, destWidth, destHeight),
        new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
        GraphicsUnit.Pixel);

    grPhoto.Dispose();
    imgPhoto.Dispose();
    return bmPhoto;
}
    
25.08.2018 / 16:50