I have some task - resize image if height or width >500px. I try this code.
But when I choosed image I have error like
NewImage.Save(path);
В GDI+ error of the general form.
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Multiselect = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < fdlg.FileNames.Length; i++)
{
string file = fdlg.FileNames[i];
string path = System.IO.Path.GetFullPath(file);
System.Drawing.Image img = System.Drawing.Image.FromFile(path);
if (img.Width > 500 || img.Height > 500)
{
int currW = img.Width;
int currH = img.Height;
int realWPer = 500 * 100 / currW;
int realHPer = 500 * 100 / currH;
int realW = currW / 100 * realWPer; // new width
int realH = currH / 100 * realHPer; // new height
Image NewImage = resizeImage(img, new Size(realW, realH));
NewImage.Save(path);
}
}
}
}
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}