I just wrote a function that takes a directory, creates a new "resizedDirectory" (if it does not exist), resizes every .bmp, and saves it to "resizedDirectory". This is the first time I've written a function like this (used the internet for some parts), here is the code:
protected void directorySelected(object sender, EventArgs e) {
// Make sure the directory exists
if (Directory.Exists(inputDirectory.Text)) {
string[] filePaths = Directory.GetFiles(inputDirectory.Text);
// Determine if there are any files in inputDirectory
if (filePaths.Length == 0) messageLog.InnerHtml = "<strong>No files found in:</strong> "+inputDirectory.Text;
else {
// Determine if "resizedDirectory" exists, create it if it does not exist
string resizedDirectory = inputDirectory.Text+"\\"+"resizedDirectory";
if (!Directory.Exists(resizedDirectory)) {
Directory.CreateDirectory(resizedDirectory);
messageLog.InnerHtml = "<strong>Created:</strong> "+resizedDirectory+"<br/><br/>";
}
// For each file in inputDirectory...
for (var i = 0; i < filePaths.Length; i++) {
string[] extensionSplit = filePaths[i].Split('.');
// Make sure filePath[i] ends with the appropriate extension
if (extensionSplit.Length == 2 && extensionSplit[1].Equals("bmp")) {
Bitmap currImage = new Bitmap(filePaths[i]);
messageLog.InnerHtml += "<strong>"+i.ToString()+":</strong> "+filePaths[i]+"<br/><div class='indent'>";
// Calculate new dimensions
int newWidth = maxWidth;
int newHeight = maxHeight;
if (currImage.Width > currImage.Height) newHeight = (int)(((float)maxWidth)/((float)currImage.Width)*currImage.Height);
else newWidth = (int)(((float)maxHeight)/((float)currImage.Height)*currImage.Width);
messageLog.InnerHtml += "<strong>Old dimensions:</strong> ("+currImage.Width+","+currImage.Height+")<br/><strong>New dimensions:</strong> ("+newWidth+", "+newHeight+")<br/>";
// Settings before saving
Bitmap targetImage = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(targetImage)) {
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawImage(currImage, 0, 0, newWidth, newHeight);
}
ImageCodecInfo ici = this.GetEncoderInfo(ImageFormat.Bmp);
Encoder eq = Encoder.Quality;
EncoderParameters eps = new EncoderParameters(1);
EncoderParameter ep = new EncoderParameter(eq, 100L); // 100L is the higest quality (goes from 0 - 100)
eps.Param[0] = ep;
// Save image
string targetPath = resizedDirectory+"\\foo"+i.ToString()+".bmp";
targetImage.Save(targetPath, ici, eps);
messageLog.InnerHtml += "<strong>Saved:</strong> "+targetPath+"</div><br/>";
}
else messageLog.InnerHtml += "<strong>IGNORED "+i.ToString()+":</strong> "+filePaths[i]+"<br/>";
}
}
}
else if (inputDirectory.Text.Equals("")) messageLog.InnerHtml = "<strong>No directory specified</strong>";
else messageLog.InnerHtml = "<strong>Cannot find:</strong> "+inputDirectory.Text;
}
protected ImageCodecInfo GetEncoderInfo(ImageFormat format) {
return ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.FormatID == format.Guid);
}
This worked fine with small .bmps. The problem is that the directories of .bmps I need to convert are huge, an example .bmp is 16073 x 13231 (or 608MB). I tried the code with a little .bmp and one of the huge .bmps and it made "resizedDirectory", created a new resized .bmp of the little one there, and then crashed with the message "Out of memory".
How should I go about resizing the huge .bmps?
Edit: It fails on one huge .bmp (but not on small ones)