Resize image in c#

Asked

Viewed 214 times

2

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

inserir a descrição da imagem aqui

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace formCalcularIdade
{
    public partial class Form1 : Form
    {
        private int _anoAtual; 
        private int _anoNasc; 
        private int _idade; 

        public Form1()
        {
            InitializeComponent();
        }

        private void Limpar(object sender, EventArgs e)
        {
            txtAnoAtual.Clear();
            txtDateNasc.Clear();

        }

        private void Calcular(object sender, EventArgs e)
        {
            this._anoAtual = Convert.ToInt32(txtAnoAtual.Text);
            this._anoNasc = Convert.ToInt32(txtDateNasc.Text);

            this._idade = _anoAtual - _anoNasc;
            txtDateIdade.Text = Convert.ToString(this._idade);
        }

        private void uploadFoto(object sender, EventArgs e)
        {

            OpenFileDialog dataImage = new OpenFileDialog();
            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)
        {

        }


    }
}

1 answer

4


Hello, you can do it this way:

There 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.