PDF解析(2) 输入坐标截取图片 #C#

.net自带的截图功能,输入坐标和图片高度和宽度实现截图

winform形式

代码如下


```csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CutImageTest
{
    public partial class Form1 : Form
    {
        private object openFileDialog1;

        public Form1()
        {
            InitializeComponent();
        }
 

        private void button1_Click(object sender, EventArgs e)
        {

            string imagePath = @"D:\job\A_PDF_TEST\ToBmp-img-1.bmp";
            int x = int.Parse(locate_X_Box.Text);
            int y = int.Parse(locate_Y_Box.Text);
            int width = int.Parse(cut_Width_Box.Text);
            int height = int.Parse(cut_Height_Box.Text);
            cutPicture(imagePath, x, y, width, height);


           //this.GetPartOfImage(imagePath, 10, 10, 0, 0);
           // this.Crop(imagePath, 50, 50, 50, 50);



        }

        public static void cutPicture(String picPath, int x, int y, int width, int height)
        {
            //图片路径
            String oldPath = picPath;
            //新图片路径
            String newPath = Path.GetExtension(oldPath);
            //计算新的文件名,在旧文件名后加_new
            newPath = oldPath.Substring(0, oldPath.Length - newPath.Length) + "_new" + newPath;
            //定义截取矩形
            Rectangle cropArea = new Rectangle(x, y, width, height);
            //要截取的区域大小
            //加载图片
            Image img = Image.FromStream(new MemoryStream(File.ReadAllBytes(oldPath)));
            //判断超出的位置否
            if ((img.Width < x + width) || img.Height < y + height)
            {
                MessageBox.Show("裁剪尺寸超出原有尺寸!");
                img.Dispose();
                return;
            }
            //定义Bitmap对象
            Bitmap bmpImage = new Bitmap(img);
            //进行裁剪
            Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
            //保存成新文件
            bmpCrop.Save(newPath);
            //释放对象
            img.Dispose();
            bmpCrop.Dispose();
        }
        
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值