截取保存于本地的某一图片的某一个部分
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace cutPicture { public partial class Form1 : Form { private int sourceW = 39;//从原图片中截取的大小(宽和高):设为39 private int sourceH = 39; private int resultW = 50;//所截取图片放缩后的宽和高:设为50 private int resultH = 50; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height); getPartImage((Bitmap)pictureBox1.Image); } private void getPartImage(Bitmap resultBitmap) //截取指定图片的某部分 { using (Graphics g = Graphics.FromImage(resultBitmap))//生成Graphics对象 { Rectangle resultRect = new Rectangle(50, 50, resultW, resultH);//从原图片中截取的大小并指定位置 Rectangle sourceRect = new Rectangle(0, 0, sourceW, sourceH);//所要截取的图片大小及位置 Bitmap sourcePic = new Bitmap("animal.bmp");//双引号内为所要截取原图片地址 g.DrawImage(sourcePic, resultRect, sourceRect, GraphicsUnit.Pixel);//在指定位置并且按指定大小绘制指定的截取部分 } } } }
如下图所示: