C#实现鼠标裁剪图像功能

本文实例为大家分享了C#实现鼠标裁剪图像的具体代码,供大家参考,具体内容如下

C#的图像裁剪很容易操作,这里给个实现的例子。

关键是需要处理鼠标的事件和一些更新

实现鼠标移动的代码.注意更新不要全部重画,只有选择矩形部分重画

private void Form1_MouseMove(object sender, MouseEventArgs e)
 {

  if (Track_move)
  endpoint = new Point(e.X, e.Y);
  else
  {
  return;
  }
  rect1 = new Rectangle(stpoint.X, stpoint.Y, endpoint.X - stpoint.X, endpoint.Y - stpoint.Y);

  Rectangle tempr = new Rectangle(rect1.X, rect1.Y, rect1.Width + 2, rect1.Height + 2);
  this.Invalidate(tempr);
 }

选择结束的处理代码.

private void Form1_MouseUp(object sender, MouseEventArgs e)
 {
  if (e.Button == MouseButtons.Left && Track_move==true )
  {
  Track_move = false;
  endpoint = new Point(e.X, e.Y);
  rect1 = new Rectangle(stpoint.X, stpoint.Y, endpoint.X - stpoint.X, endpoint.Y - stpoint.Y);
  Rectangle rectorg = new Rectangle(borg.X, borg.Y, image1.Width, image1.Height);
  if (rect1.Width <= 0)
   return;
  if (rect1.Height <= 0)
   return;
  if (rectorg.Contains(rect1))
  {
   Rectangle rectadj = new Rectangle(rect1.X - borg.X, rect1.Y - borg.Y, rect1.Width, rect1.Height);
   Bitmap cropimge = image1.Clone(rectadj, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
   pictureBox2.Image = cropimge;
  }
  else
  {
   pictureBox2.Image = null;
  }
  this.Invalidate();
 }
}

程序的整个代码

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;
using System.Runtime.InteropServices;

namespace imageForms
{
 static class Program
 {
 /// <summary>
 /// 应用程序的主入口点。
 /// </summary>
 [STAThread]
 static void Main()
 {
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
 }
 }
 public partial class Form1 : Form
 {
 private System.Windows.Forms.PictureBox pictureBox2;
 private System.Windows.Forms.Label label1;
 public Form1()
 {
  InitializeComponent();
 }

 private void pictureBox1_Click(object sender, EventArgs e)
 {

 }

 private void Form1_Load(object sender, EventArgs e)
 {
  showimg();

 }
 Bitmap image1;
 private void showimg()
 {
  int wd = 400;
  int hg = 200;
  int len = wd * hg * 3;
  byte[] pdata = new byte[len];
  for (int i = 0; i < len; i++)
  {
  if (i > 3 * wd * (hg / 2))
  {
   pdata[i] = 255;
  }
  else
  {
   pdata[i] = 0;
  }
  }

  try
  {
  image1 = new Bitmap(wd, hg, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  for (int y = 0; y < hg; y++)
  {
   for (int x = 0; x < wd; x++)
   {
   Color crr = Color.FromArgb(pdata[3 * wd * y + x], pdata[3 * wd * y + x], pdata[3 * wd * y + x]);
   image1.SetPixel(x, y, crr);
   }
  }
  // Set the PictureBox to display the image.
  // pictureBox1.Image = image1;

  }
  catch (ArgumentException)
  {
  MessageBox.Show("There was an error check data.");
  }
 }
 Point stpoint,endpoint;
 Rectangle rect1;
 Point borg = new Point(20, 20);
 protected override void OnPaint(PaintEventArgs e)
 {

  base.OnPaint(e);
  e.Graphics.DrawImage(image1, borg);
  if (rect1 != null )
  {
   e.Graphics.DrawRectangle(new Pen(Color.Red, 1), rect1);
  }

 }

 private void Form1_MouseDown(object sender, MouseEventArgs e)
 {
  if (e.Button == MouseButtons.Left)
  {
  stpoint = new Point(e.X, e.Y);
  Track_move = true;
  return;
  }
  Track_move = false;
 }

 private void Form1_MouseUp(object sender, MouseEventArgs e)
 {
  if (e.Button == MouseButtons.Left && Track_move==true )
  {
  Track_move = false;
  endpoint = new Point(e.X, e.Y);
  rect1 = new Rectangle(stpoint.X, stpoint.Y, endpoint.X - stpoint.X, endpoint.Y - stpoint.Y);
  Rectangle rectorg = new Rectangle(borg.X, borg.Y, image1.Width, image1.Height);
  if (rect1.Width <= 0)
   return;
  if (rect1.Height <= 0)
   return;
  if (rectorg.Contains(rect1))
  {
   Rectangle rectadj = new Rectangle(rect1.X - borg.X, rect1.Y - borg.Y, rect1.Width, rect1.Height);
   Bitmap cropimge = image1.Clone(rectadj, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
   pictureBox2.Image = cropimge;
  }
  else
  {
   pictureBox2.Image = null;
  }
  this.Invalidate();
  }
 }
 bool Track_move=false ;
 private void Form1_MouseMove(object sender, MouseEventArgs e)
 {

  if (Track_move)
  endpoint = new Point(e.X, e.Y);
  else
  {
  return;
  }
  rect1 = new Rectangle(stpoint.X, stpoint.Y, endpoint.X - stpoint.X, endpoint.Y - stpoint.Y);

  Rectangle tempr = new Rectangle(rect1.X, rect1.Y, rect1.Width + 2, rect1.Height + 2);
  this.Invalidate(tempr);
 }
 private System.ComponentModel.IContainer components = null;

 private void InitializeComponent()
 {
  this.pictureBox2 = new System.Windows.Forms.PictureBox();
  this.label1 = new System.Windows.Forms.Label();
  ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
  this.SuspendLayout();
  //
  // pictureBox2
  //
  this.pictureBox2.Location = new System.Drawing.Point(605, 103);
  this.pictureBox2.Name = "pictureBox2";
  this.pictureBox2.Size = new System.Drawing.Size(227, 173);
  this.pictureBox2.TabIndex = 1;
  this.pictureBox2.TabStop = false;
  //
  // label1
  //
  this.label1.AutoSize = true;
  this.label1.Location = new System.Drawing.Point(602, 58);
  this.label1.Name = "label1";
  this.label1.Size = new System.Drawing.Size(127, 15);
  this.label1.TabIndex = 2;
  this.label1.Text = "鼠标左键选择裁剪";
  //
  // Form1
  //
  this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  this.ClientSize = new System.Drawing.Size(844, 558);
  this.Controls.Add(this.label1);
  this.Controls.Add(this.pictureBox2);
  this.Name = "Form1";
  this.Text = "Form1";
  this.Load += new System.EventHandler(this.Form1_Load);
  this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
  this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
  this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
  ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
  this.ResumeLayout(false);
  this.PerformLayout();

 }
 protected override void Dispose(bool disposing)
 {
  if (disposing && (components != null))
  {
  components.Dispose();
  }
  base.Dispose(disposing);
 }

 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • c#图片处理之图片裁剪成不规则图形

    为了让大家知道下面内容是否是自己想要的,我先发效果图. 好了,那就开始贴代码了 以下为一个按钮的事件,为裁剪准备图片.裁剪路径.保存路径 复制代码 代码如下: private void button1_Click(object sender, EventArgs e)        {            GraphicsPath path = new GraphicsPath();            Point[] p = {                            new

  • C#裁剪,缩放,清晰度,水印处理操作示例

    前言 需求源自项目中的一些应用,比如相册功能,通常用户上传相片后我们都会针对该相片再生成一张缩略图,用于其它页面上的列表显示.随便看一下,大部分网站基本都是将原图等比缩放来生成缩略图.但完美主义者会发现一些问题,比如显示排版时想让相片缩略图列表非常统一.整齐.和美观,比如要求每张缩略图大小固定为120 x 90且不拉伸变形怎么办?再比如用户头像如何让缩略图比原图更清晰?或是如何在上传的图片下加一个半透明的LOGO水印? OK,本文根据自己的项目代码描述以上问题的解决方案,全部基于.Net Fra

  • C#实现在服务器端裁剪图片的方法

    本文实例讲述了C#实现在服务器端裁剪图片的方法.分享给大家供大家参考.具体实现方法如下: //图片路径 String oldPath = Server.MapPath("~/62223231.jpg"); //新图片路径 String newPath = System.IO.Path.GetExtension(oldPath); //设置截取的坐标和大小 int x = 0, y = 20, width = 200, height = 2400; //计算新的文件名,在旧文件名后加_n

  • C#实现对图片文件的压缩、裁剪操作实例

    本文实例讲述了C#对图片文件的压缩.裁剪操作方法,在C#项目开发中非常有实用价值.分享给大家供大家参考.具体如下: 一般在做项目时,对图片的处理,以前都采用在上传时,限制其大小的方式,这样带来诸多不便.毕竟网站运维人员不一定会对图片做处理,经常超出大小限制,即使会使用图片处理软件的,也由于个人水平方面原因,处理效果差强人意. 于是采用C#为我们提供的图像编辑功能,实现一站式上传,通过程序生成所需大小.尺寸的目标图片. 具体步骤如下: 先说图片压缩: 第一步:需要读取一个图片文件,读取方法: //

  • c#裁剪图片后使用zxing生成二维码示例分享

    复制代码 代码如下: /// <summary>/// 生成二维码/// </summary>/// <param name="fileName">生成二维码路径</param>/// <param name="url">生成的内容</param>/// <param name="width">二维码宽</param>/// <param nam

  • c#利用Grahics进行图片裁剪

    最开始用了 复制代码 代码如下: /// <summary>        /// 裁剪图片        /// </summary>        /// <param name="imagePath"/>        /// <param name="savePath">"c:\images\"</param>        private List<string>

  • C#实现鼠标裁剪图像功能

    本文实例为大家分享了C#实现鼠标裁剪图像的具体代码,供大家参考,具体内容如下 C#的图像裁剪很容易操作,这里给个实现的例子. 关键是需要处理鼠标的事件和一些更新 实现鼠标移动的代码.注意更新不要全部重画,只有选择矩形部分重画 private void Form1_MouseMove(object sender, MouseEventArgs e) { if (Track_move) endpoint = new Point(e.X, e.Y); else { return; } rect1 =

  • 用Vue.js在浏览器中实现裁剪图像功能

    你是否写了一个需要接受用户上传图片的 Web 应用,后来才意识到用户总是提供各种形状和大小的图像来破坏你的网站主题?在网络上处理图像很容易成为一种痛苦 -- 当然,除非你使用了正确的工具. 在本教程中,我们将探讨如何在浏览器中使用 JavaScript 库来操作图片,为服务器上的存储做准备,并在 Web 程序中使用.我们将使用 Vue.js 而不是原生 JavaScript来完成此操作. 要了本文想要完成的任务,请查看上面的图片.左侧是原始图像,右侧是新图像预览.我们可以移动裁剪框并调整其大小,

  • OpenCV使用鼠标响应裁剪图像

    给定一幅图像,将其中的某一部分兴趣区域裁剪出来,这在PS中很好实现,但是使用openCV如何实现呢?因此本文主要介绍openCV使用鼠标响应来裁剪图像: 一.代码部分: #include "stdafx.h" #include "cv.h" #include <highgui.h> #include <stdio.h> IplImage* org = 0; IplImage* img = 0; IplImage* tmp = 0; IplIm

  • cropperjs实现裁剪图片功能

    本文实例为大家分享了cropperjs实现裁剪图片功能的具体代码,供大家参考,具体内容如下 cropperjs (裁剪图片) vue版本 // 下载 // npm install cropperjs -save // 使用 //1.0 引入  import Cropper from 'cropperjs' // 2.0初始化裁剪框     data(){         return{             croppable: false // 控制上传后的显隐         }     

  • Android裁剪图像实现方法示例

    本文实例讲述了Android裁剪图像实现方法.分享给大家供大家参考,具体如下: package com.xiaoma.piccut.demo; import java.io.File; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.B

  • React鼠标多选功能的配置方法

    一般列表都有选择功能,单选复选多选都很常见.在自定义循环的列表,图像中,实现鼠标单选,多选,反选功能. # React mousemultiples # React 鼠标多选组件 React 鼠标多选组件 局限性 > 主要实现鼠标多选的效果, 在不破坏原有的列表情况下,嵌入组件拥有鼠标多选功能. npm包地址 [链接](https://www.npmjs.com/package/mousemultiples) 安装 npm i mousemultiples 使用配置项 /**  * wrappe

  • 网页禁止鼠标左右键功能的简单代码

    网页禁止鼠标左右键功能的简单代码   这个代码以前在网上找的,试用效果不错,放这里留备份,我想这个也能算是常用的代码之一了.当然啦,查看源码时鼠标肯定可以用啦,不过,有时不用想那么复杂.要求那么强大,况且源码中的代码比文本多. document.oncontextmenu=new Function("event.returnValue=false;"); document.onselectstart=new Function("event.returnValue=false;

  • BootStrap实现鼠标悬停下拉列表功能

    BootStrap实现鼠标悬停下拉列表功能,具体内容详情如下所示: //最简单的鼠标悬停,实现下拉功能,应用bootstrap框架的知识,不会bootstrap自己百度怎么使用 <html><head> <meta charset="UTF-8"> <!-- 引入 Bootstrap --> <link href="../css/bootstrap.min.css" rel="external nofo

  • JS实现禁止鼠标右键的功能

    遇到网页上有精美图片或者精彩文字想保存时,通常大家都是选中目标后按鼠标右键,在弹出菜单中选择"图片另存为"或"复制"来达到我们的目的.但是,目前有许多网页都屏蔽了鼠标右键,那么用js如何实现禁止鼠标右键的功能呢? 1.与禁止鼠标右键相关的JS说明 <script type="text/javascript"> document.oncontextmenu=new Function("event.returnValue=fal

  • WEB前端实现裁剪上传图片功能

    最后的效果如下: 这里面有几个功能,第一个是支持拖拽,第二个压缩,第三个是裁剪编辑,第四个是上传和上传进度显示,下面依次介绍每个功能的实现: 1. 拖拽显示图片 拖拽读取的功能主要是要兼听html5的drag事件,这个没什么好说的,查查api就知道怎么做了,主要在于怎么读取用户拖过来的图片并把它转成base64以在本地显示. var handler = { init: function($container){ //需要把dragover的默认行为禁掉,不然会跳页 $container.on("

随机推荐