截取图片某一部分

截取保存于本地的某一图片的某一个部分


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);//在指定位置并且按指定大小绘制指定的截取部分
            }
        }
    }
}


如下图所示:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值