Saturday, May 30, 2009

Cut part of the image file

// Cut part of the image file

// --------
// Method 1
// --------

Rectangle rect = new Rectangle(myImage1.Left, myImage1.Top, myImage1.Width - 200, myImage1.Height - 100);
myImage2 = myImage1.Clone(rect, System.Drawing.Imaging.PixelFormat.Format32bppPArg arg);


// --------
// Method 2
// --------

string path = "C:\\test.jpg";
using (Bitmap orignal = new Bitmap(path))
{
using (Bitmap newimage = new Bitmap((int)(orignal.Width * 0.5), (int)(orignal.Height * 0.5)))
{
using (Graphics newgraphics = Graphics.FromImage(newimage))
{
newgraphics.DrawImage(orignal, 0, 0, new Rectangle(newimage.Width, newimage.Height, orignal.Width - newimage.Width, orignal.Height - newimage.Height), GraphicsUnit.Pixel);
newgraphics.Flush();
}

newimage.Save(new System.IO.FileInfo(path).DirectoryName + "out.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}

No comments:

Post a Comment