Cropping Images in C#

J

jpuopolo

All:

I looked around and could not find a simple yet complete example of
cropping an image in C#. I had the problem of needing to shave off the
bottom-most 18 lines of a directory full of images. The following code
does this. I hope that this serves as a concise yet complete example
of how to tackle cropping an image.

---
Note that inputDireName and outputDirName have been set in the class
someplace else.

private static void CropImages()
{
// Get all the files from the source directory

DirectoryInfo di = new DirectoryInfo(inputDirName);
FileInfo[] imageFileNames = di.GetFiles();

// Process each file, one by one
foreach (FileInfo fi in imageFileNames) {

// If this is not an image file, skip it
if (!IsImageFile(fi.FullName)) {
continue;
}

// Take a piece of the orginal image and write it to
the
// cropped image.

Image imgOriginal; // Image read from disk
Bitmap imgCropped; // Image created from original
Rectangle rOriginal; // Source rectangle of
original
Rectangle rCropped; // Destination (cropped)
rectangle
Graphics g; // Graphics surface of
destination image
string outFileName; // Name of cropped image file
written out

try {
// Load the original image
imgOriginal = Image.FromFile(fi.FullName);

// Calculate the cropped image dimensions and
create a surface
// on which to draw it
imgCropped = new Bitmap(imgOriginal.Width,
imgOriginal.Height - 18);
g = Graphics.FromImage(imgCropped);
rOriginal = new Rectangle(0, 0, imgCropped.Width,
imgCropped.Height);
rCropped = rOriginal;

// Copy the subset of the original image to the
cropped image
g.DrawImage(imgOriginal, rCropped, rOriginal,
GraphicsUnit.Pixel);

// Save the cropped image to disk
outFileName = outputDirName + @"\" +
Path.GetFileNameWithoutExtension(fi.Name) + "_cropped.jpg";
imgCropped.Save(outFileName,
System.Drawing.Imaging.ImageFormat.Jpeg);

// Clean up resources
g.Dispose();
imgCropped.Dispose();
imgOriginal.Dispose();
}
catch (IOException ex) {
}
}
}

private static bool IsImageFile(string fileName)
{
return fileName.EndsWith(".jpg") ||
fileName.EndsWith(".gif") ||
fileName.EndsWith(".bmp");
}
 
J

jpuopolo

Bookmark this site:http://www.bobpowell.net/faqmain.htm

He is daBomb for image related stuff.


I looked around and could not find a simple yet complete example of
cropping an image in C#. I had the problem of needing to shave off the
bottom-most 18 lines of a directory full of images. The following code
does this. I hope that this serves as a concise yet complete example
of how to tackle cropping an image.
private static void CropImages()
{
// Get all the files from the source directory
DirectoryInfo di = new DirectoryInfo(inputDirName);
FileInfo[] imageFileNames = di.GetFiles();
// Process each file, one by one
foreach (FileInfo fi in imageFileNames) {
// If this is not an image file, skip it
if (!IsImageFile(fi.FullName)) {
continue;
}
// Take a piece of the orginal image and write it to
the
// cropped image.
Image imgOriginal; // Image read from disk
Bitmap imgCropped; // Image created from original
Rectangle rOriginal; // Source rectangle of
original
Rectangle rCropped; // Destination (cropped)
rectangle
Graphics g; // Graphics surface of
destination image
string outFileName; // Name of cropped image file
written out
try {
// Load the original image
imgOriginal = Image.FromFile(fi.FullName);
// Calculate the cropped image dimensions and
create a surface
// on which to draw it
imgCropped = new Bitmap(imgOriginal.Width,
imgOriginal.Height - 18);
g = Graphics.FromImage(imgCropped);
rOriginal = new Rectangle(0, 0, imgCropped.Width,
imgCropped.Height);
rCropped = rOriginal;
// Copy the subset of the original image to the
cropped image
g.DrawImage(imgOriginal, rCropped, rOriginal,
GraphicsUnit.Pixel);
// Save the cropped image to disk
outFileName = outputDirName + @"\" +
Path.GetFileNameWithoutExtension(fi.Name) + "_cropped.jpg";
imgCropped.Save(outFileName,
System.Drawing.Imaging.ImageFormat.Jpeg);
// Clean up resources
g.Dispose();
imgCropped.Dispose();
imgOriginal.Dispose();
}
catch (IOException ex) {
}
}
}
private static bool IsImageFile(string fileName)
{
return fileName.EndsWith(".jpg") ||
fileName.EndsWith(".gif") ||
fileName.EndsWith(".bmp");
}

Thanks!
John
 
Top