Resize and Save Multiple Images in C#
We'll develop an application in which user can select multiple images by "OpenFileDialog" and copy those images to specific directory after resizing it.
Now we have to add function "ResizeImage"which will responsible for resizing image and saving it.
Now run the application and test it. It will create a directory named "Files" to your application's "bin/Debug"directory and save resized images in that new created directory.
- Open visual studio and create new "Windows Form Application" project.
- Go to Toolbox and drag a button.
- Change name of button to "FileUploadBtn".
- Change text of button to "Upload & Resize Images".
- Now double click at that button and add following code in its click event
private void FileUploadBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
//Add filter, if you want to allow all extenssions just remove it.
openFileDialog.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + "All files (*.*)|*.*";
// Set multiselect true for slecting multiple files
openFileDialog.Multiselect = true;
// Show open file dialog and check if Open button is pressed.
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Set directory name and create it.
string directory = "Files";
Directory.CreateDirectory(directory);
// Read all files.
foreach (string fileName in openFileDialog.FileNames)
{
// Get file name without its path.
string newFile = directory + "/" + Path.GetFileName(fileName);
ResizeImage(fileName, newFile, 100, 100, true);
}
}
}
{
OpenFileDialog openFileDialog = new OpenFileDialog();
//Add filter, if you want to allow all extenssions just remove it.
openFileDialog.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + "All files (*.*)|*.*";
// Set multiselect true for slecting multiple files
openFileDialog.Multiselect = true;
// Show open file dialog and check if Open button is pressed.
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Set directory name and create it.
string directory = "Files";
Directory.CreateDirectory(directory);
// Read all files.
foreach (string fileName in openFileDialog.FileNames)
{
// Get file name without its path.
string newFile = directory + "/" + Path.GetFileName(fileName);
ResizeImage(fileName, newFile, 100, 100, true);
}
}
}
Now we have to add function "ResizeImage"which will responsible for resizing image and saving it.
public void ResizeImage(string originalFile, string newFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
{
Image OriginalImage = Image.FromFile(originalFile);
if (OnlyResizeIfWider)
{
if (OriginalImage.Width <= NewWidth)
{
NewWidth = OriginalImage.Width;
}
}
int NewHeight = OriginalImage.Height * NewWidth / OriginalImage.Width;
if (NewHeight > MaxHeight)
{
// Resize with height instead
NewWidth = OriginalImage.Width * MaxHeight / OriginalImage.Height;
NewHeight = MaxHeight;
}
Image NewImage = OriginalImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
// Save resized picture
NewImage.Save(newFile);
// Clear handle to original file so that we can overwrite it if necessary
OriginalImage.Dispose();
NewImage.Dispose();
}
{
Image OriginalImage = Image.FromFile(originalFile);
if (OnlyResizeIfWider)
{
if (OriginalImage.Width <= NewWidth)
{
NewWidth = OriginalImage.Width;
}
}
int NewHeight = OriginalImage.Height * NewWidth / OriginalImage.Width;
if (NewHeight > MaxHeight)
{
// Resize with height instead
NewWidth = OriginalImage.Width * MaxHeight / OriginalImage.Height;
NewHeight = MaxHeight;
}
Image NewImage = OriginalImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
// Save resized picture
NewImage.Save(newFile);
// Clear handle to original file so that we can overwrite it if necessary
OriginalImage.Dispose();
NewImage.Dispose();
}
Now run the application and test it. It will create a directory named "Files" to your application's "bin/Debug"directory and save resized images in that new created directory.
"GetThumbnailImage" is the key methodfor generating a thumbnail and "NewImage.Save()" is the key method for saving that thumbnail. Click at links to see their more details
Comments
Post a Comment
Welcome to fc-3