zoukankan      html  css  js  c++  java
  • c#裁剪图片(根据鼠标画的矩形裁剪图片)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using S10806Class;
    using System.IO;

    namespace GetImage
    {
    public partial class GetScreenImgForm : Form
    {

    /// <summary>
    /// 图像操作类
    /// </summary>
    ImageWork imageWork = new ImageWork();

    /// <summary>
    /// 保存截取到的桌面图像
    /// </summary>
    Image screenImg = null;

    /// <summary>
    /// 保存用户截取的图像区域
    /// </summary>
    Rectangle imgRect = new Rectangle();

    /// <summary>
    /// 窗体的GDI
    /// </summary>
    Graphics g = null;

    /// <summary>
    /// 区域边框画笔
    /// </summary>
    Pen pen = new Pen(Color.Black, 1);

    /// <summary>
    /// 是否应该绘制区域
    /// </summary>
    bool isDraw = false;

    /// <summary>
    /// 用户是否选择了区域
    /// </summary>
    bool isOption = false;

    public GetScreenImgForm()
    {
    InitializeComponent();

    //获取当前桌面截图
    screenImg = imageWork.GetScreenImage();

    g = this.CreateGraphics();
    }

    private void GetScreenImgForm_Load(object sender, EventArgs e)
    {
    this.Size = new Size(screenImg.Width, screenImg.Height);

    this.BackgroundImage = screenImg;
    }

    private void GetScreenImgForm_MouseUp(object sender, MouseEventArgs e)
    {
    //没有截图并且单击右键 - 关闭本窗体
    if (e.Button == MouseButtons.Right && !this.isOption)
    {
    this.Close();
    }
    //截图了并且单击右键 - 取消此次截图
    else if (e.Button == MouseButtons.Right && this.isOption)
    {
    g.DrawImage(screenImg, new Point(0, 0));
    this.isOption = false;
    return;
    }
    //在截图情况下松开左键,选择区域结束,等待双击截取
    else if (e.Button == MouseButtons.Left && this.isOption && this.isDraw)
    {
    this.isDraw = false;

    imgRect.Width = e.X - imgRect.X; //区域长度

    imgRect.Height = e.Y - imgRect.Y; //区域高度
    }

    }

    private void GetScreenImgForm_MouseDown(object sender, MouseEventArgs e)
    {
    if (e.Button != MouseButtons.Left) return;

    if (this.isOption) return;

    imgRect.X = e.X; //起始X轴

    imgRect.Y = e.Y; //起始Y轴

    this.isDraw = true;//允许绘图
    }

    private void GetScreenImgForm_MouseMove(object sender, MouseEventArgs e)
    {
    if (isDraw)
    {
    //刷新图像
    g.DrawImage(screenImg, new Point(0, 0));

    this.isOption = true;

    Point p1 = new Point(imgRect.X, imgRect.Y);
    Point p2 = new Point(e.X, imgRect.Y);
    Point p3 = new Point(imgRect.X, e.Y);
    Point p4 = new Point(e.X, e.Y);

    //画四条边框
    g.DrawLine(pen, p1, p2);
    g.DrawLine(pen, p2, p4);
    g.DrawLine(pen, p4, p3);
    g.DrawLine(pen, p3, p1);
    }
    else
    {
    //确定用户的选择区域
    int top = imgRect.Y;
    int down = imgRect.Y + imgRect.Height;
    int left = imgRect.X;
    int right = imgRect.X + imgRect.Width;

    //鼠标在选择区域中会变个模样
    if (e.X > left && e.X < right && e.Y > top && e.Y < down)
    {
    this.Cursor = Cursors.SizeAll;
    }
    else
    {
    this.Cursor = Cursors.Cross;
    }
    }
    }

    private void GetScreenImgForm_MouseDoubleClick(object sender, MouseEventArgs e)
    {
    try
    {
    //用户双击选择区域时.....
    if (this.Cursor == Cursors.SizeAll)
    {
    Bitmap b = new Bitmap(this.screenImg);

    Bitmap img = b.Clone(this.imgRect, System.Drawing.Imaging.PixelFormat.DontCare);

    SaveFileDialog dlg = new SaveFileDialog();

    dlg.Filter = "(*.jpg)|*.jpg";

    if (dlg.ShowDialog() != DialogResult.OK) return;

    img.Save(dlg.FileName);

    this.Close();
    }
    }
    catch (Exception err)
    {
    MessageBox.Show(err.Message);
    }
    }

    }
    }<收起

    10
  • 相关阅读:
    hdu 2203
    hdu 3081
    hdu 4240 最大流量路径
    b_vj_Fiber Network(floyd思想+状态压缩)
    b_vj_Corn Fields(预处理行的状态、合法状态+枚举当前行与上一行的状态)
    b_vj_Hackers' Crackdown(预处理所有集合+检查合法集合后进行状态转移)
    b_vj_Count Color(线段树+二进制表示颜色)
    b_vj_K-th Number(二分+线段树)
    b_lg_火烧赤壁(讨论完全覆盖/部分覆盖)
    b_hdu_Ping pong(树状数组+乘法原理)
  • 原文地址:https://www.cnblogs.com/xianyin05/p/3081687.html
Copyright © 2011-2022 走看看