|
Bitmap bm = (Bitmap)Image.FromFile("myimage.jpg");
Bitmap resized = new Bitmap((int)(0.3f*bm.Width),(int)(0.3f*bm.Height));
Graphics g=Graphics.FromImage(resized);
g.DrawImage(bm, new Rectangle(0,0,resized.Width,resized.Height),0,0,bm.Width,bm.Height,GraphicsUnit.Pixel);
g.Dispose();
resized.Save("resizedimage.jpg",ImageFormat.Jpeg);
Listing 2 shows how to crop an image..
Bitmap bm = (Bitmap)Image.FromFile("myimage.jpg"); // assumes a 400 * 300 image from which a 160 * 120 chunk will be taken
Bitmap cropped = new Bitmap(160,120);
Graphics g=Graphics.FromImage(cropped);
g.DrawImage(bm, new Rectangle(0,0,cropped.Width,cropped.Height),100,50,cropped.Width,cropped.Height,GraphicsUnit.Pixel);
g.Dispose();
cropped.Save("croppedimage.jpg",ImageFormat.Jpeg);
Return to the GDI+ FAQ |