By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,491 Members | 1,186 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

Cropping Images in C#

P: n/a
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");
}

Nov 7 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a


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

He is daBomb for image related stuff.


"jpuopolo" <pu*****@gmail.comwrote in message
news:11*********************@50g2000hsm.googlegrou ps.com...
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");
}

Nov 7 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.