C# 實(shí)現(xiàn)QQ式截圖功能實(shí)例代碼
這個(gè)功能一共有兩部分組成,第一部分是窗體代碼,另外的一部分是一個(gè)輔助方法。直接貼出代碼,以供大家參考:
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.Drawing.Drawing2D;
using System.Drawing.Imaging;
using ImageClassLib;
namespace ImageShear
{
public partial class Demo: Form
{
public Demo()
{
InitializeComponent();
}
#region 點(diǎn)擊打開圖像
public string strHeadImagePath; //打開圖片的路徑
Bitmap Bi; //定義位圖對(duì)像
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.gif|*.jpg|*.JPEG|*.JPEG|*.bmp|*.bmp"; //設(shè)置讀取圖片類型
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
strHeadImagePath = openFileDialog1.FileName;
//this.Show(strHeadImagePath);
Bi = new Bitmap(strHeadImagePath); //使用打開的圖片路徑創(chuàng)建位圖對(duì)像
ImageCut1 IC = new ImageCut1(40, 112, this.pictureBox1.Width, this.pictureBox1.Height); //實(shí)例化ImageCut類,四個(gè)參數(shù)據(jù)分別表示為:x、y、width、heigth,(40、112)表示pictureBox1的Lcation的坐標(biāo),(120、144)表示pictureBox1控件的寬度和高度
this.pictureBox1.Image = IC.KiCut1((Bitmap)(this.GetSelectImage(this.pictureBox1.Width, this.pictureBox1.Height))); //(120、144)表示pictureBox1控件的寬度和高度
//this.pictureBox1.Image = (this.GetSelectImage(120, 144));
}
catch (Exception ex)
{
MessageBox.Show("格式不對(duì)");
ex.ToString();
}
}
}
#endregion
#region 定義顯示圖像方法,即將打開的圖像在pictureBox1控件顯示
public void Show(string strHeadImagePath)
{
this.pictureBox1.Load(@strHeadImagePath); //
}
#endregion
#region 獲取圖像
/// <summary>
/// 獲取指定寬度和高度的圖像即使圖片和pictureBox1控件一樣寬和高,返回值為圖片Image
/// </summary>
/// <param name="Width表示寬"></param>
/// <param name="Height表示高"></param>
/// <returns></returns>
public Image GetSelectImage(int Width, int Height)
{
//Image initImage = this.pictureBox1.Image;
Image initImage = Bi;
//原圖寬高均小于模版,不作處理,直接保存
if (initImage.Width <= Width && initImage.Height <= Height)
{
//initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
return initImage;
}
else
{
//原始圖片的寬、高
int initWidth = initImage.Width;
int initHeight = initImage.Height;
//非正方型先裁剪為正方型
if (initWidth != initHeight)
{
//截圖對(duì)象
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG = null;
//寬大于高的橫圖
if (initWidth > initHeight)
{
//對(duì)象實(shí)例化
pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//設(shè)置質(zhì)量
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//定位
Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight);
Rectangle toR = new Rectangle(0, 0, initHeight, initHeight);
//畫圖
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//重置寬
initWidth = initHeight;
}
//高大于寬的豎圖
else
{
//對(duì)象實(shí)例化
pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//設(shè)置質(zhì)量
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//定位
Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth);
Rectangle toR = new Rectangle(0, 0, initWidth, initWidth);
//畫圖
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//重置高
initHeight = initWidth;
}
initImage = (System.Drawing.Image)pickedImage.Clone();
// //釋放截圖資源
pickedG.Dispose();
pickedImage.Dispose();
}
return initImage;
}
}
#endregion
#region 點(diǎn)擊button2按鈕事件
private void button2_Click(object sender, EventArgs e)
{
this.label1.Text = "裁剪后的圖片寬度:"+this.pictureBox2.Width.ToString();
this.label2.Text = "裁剪后的圖片高度:"+this.pictureBox2.Height.ToString();
}
#endregion
#region 縮放、裁剪圖像用到的變量
/// <summary>
///
/// </summary>
int x1; //鼠標(biāo)按下時(shí)橫坐標(biāo)
int y1; //鼠標(biāo)按下時(shí)縱坐標(biāo)
int width; //所打開的圖像的寬
int heigth; //所打開的圖像的高
bool HeadImageBool = false; // 此布爾變量用來(lái)判斷pictureBox1控件是否有圖片
#endregion
#region 畫矩形使用到的變量
Point p1; //定義鼠標(biāo)按下時(shí)的坐標(biāo)點(diǎn)
Point p2; //定義移動(dòng)鼠標(biāo)時(shí)的坐標(biāo)點(diǎn)
Point p3; //定義松開鼠標(biāo)時(shí)的坐標(biāo)點(diǎn)
#endregion
#region 鼠標(biāo)按下時(shí)發(fā)生的事件
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
this.Cursor = Cursors.Cross;
this.p1 = new Point(e.X, e.Y);
x1 = e.X;
y1 = e.Y;
if (this.pictureBox1.Image != null)
{
HeadImageBool = true;
}
else
{
HeadImageBool = false;
}
}
#endregion
#region 移動(dòng)鼠標(biāo)發(fā)生的事件
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (this.Cursor == Cursors.Cross)
{
this.p2 = new Point(e.X, e.Y);
if ((p2.X - p1.X) > 0 && (p2.Y - p1.Y) > 0) //當(dāng)鼠標(biāo)從左上角向開始移動(dòng)時(shí)P3坐標(biāo)
{
this.p3 = new Point(p1.X, p1.Y);
}
if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) > 0) //當(dāng)鼠標(biāo)從右上角向左下方向開始移動(dòng)時(shí)P3坐標(biāo)
{
this.p3 = new Point(p2.X, p1.Y);
}
if ((p2.X - p1.X) > 0 && (p2.Y - p1.Y) < 0) //當(dāng)鼠標(biāo)從左下角向上開始移動(dòng)時(shí)P3坐標(biāo)
{
this.p3 = new Point(p1.X, p2.Y);
}
if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) < 0) //當(dāng)鼠標(biāo)從右下角向左方向上開始移動(dòng)時(shí)P3坐標(biāo)
{
this.p3 = new Point(p2.X, p2.Y);
}
this.pictureBox1.Invalidate(); //使控件的整個(gè)圖面無(wú)效,并導(dǎo)致重繪控件
}
}
#endregion
#region 松開鼠標(biāo)發(fā)生的事件,實(shí)例化ImageCut1類對(duì)像
ImageCut1 IC1; //定義所畫矩形的圖像對(duì)像
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (HeadImageBool)
{
width = this.pictureBox1.Image.Width;
heigth = this.pictureBox1.Image.Height;
if ((e.X - x1) > 0 && (e.Y - y1) > 0) //當(dāng)鼠標(biāo)從左上角向右下方向開始移動(dòng)時(shí)發(fā)生
{
IC1 = new ImageCut1(x1, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //實(shí)例化ImageCut1類
}
if ((e.X - x1) < 0 && (e.Y - y1) > 0) //當(dāng)鼠標(biāo)從右上角向左下方向開始移動(dòng)時(shí)發(fā)生
{
IC1 = new ImageCut1(e.X, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //實(shí)例化ImageCut1類
}
if ((e.X - x1) > 0 && (e.Y - y1) < 0) //當(dāng)鼠標(biāo)從左下角向右上方向開始移動(dòng)時(shí)發(fā)生
{
IC1 = new ImageCut1(x1, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //實(shí)例化ImageCut1類
}
if ((e.X - x1) < 0 && (e.Y - y1) < 0) //當(dāng)鼠標(biāo)從右下角向左上方向開始移動(dòng)時(shí)發(fā)生
{
IC1 = new ImageCut1(e.X, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //實(shí)例化ImageCut1類
}
this.pictureBox2.Width = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Width;
this.pictureBox2.Height = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Height;
this.pictureBox2.Image = IC1.KiCut1((Bitmap)(this.pictureBox1.Image));
this.Cursor = Cursors.Default;
}
else
{
this.Cursor = Cursors.Default;
}
}
#endregion
#region 獲取所選矩形圖像
/// <summary>
///
/// </summary>
/// <param name="Width"></param>
/// <param name="Height"></param>
/// <returns></returns>
public Image GetSelectImage1(int Width, int Height)
{
Image initImage = this.pictureBox1.Image;
//Image initImage = Bi;
//原圖寬高均小于模版,不作處理,直接保存
if (initImage.Width <= Width && initImage.Height <= Height)
{
//initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
return initImage;
}
else
{
//原始圖片的寬、高
int initWidth = initImage.Width;
int initHeight = initImage.Height;
//非正方型先裁剪為正方型
if (initWidth != initHeight)
{
//截圖對(duì)象
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG = null;
//寬大于高的橫圖
if (initWidth > initHeight)
{
//對(duì)象實(shí)例化
pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//設(shè)置質(zhì)量
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//定位
Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight);
Rectangle toR = new Rectangle(0, 0, initHeight, initHeight);
//畫圖
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//重置寬
initWidth = initHeight;
}
//高大于寬的豎圖
else
{
//對(duì)象實(shí)例化
pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//設(shè)置質(zhì)量
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//定位
Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth);
Rectangle toR = new Rectangle(0, 0, initWidth, initWidth);
//畫圖
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//重置高
initHeight = initWidth;
}
initImage = (System.Drawing.Image)pickedImage.Clone();
// //釋放截圖資源
pickedG.Dispose();
pickedImage.Dispose();
}
return initImage;
}
}
#endregion
#region 重新繪制pictureBox1控件,即移動(dòng)鼠標(biāo)畫矩形
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (HeadImageBool)
{
Pen p = new Pen(Color.Black, 1);//畫筆
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
//Bitmap bitmap = new Bitmap(strHeadImagePath);
Bitmap bitmap = Bi;
Rectangle rect = new Rectangle(p3, new Size(System.Math.Abs(p2.X - p1.X), System.Math.Abs(p2.Y - p1.Y)));
e.Graphics.DrawRectangle(p, rect);
}
else
{
}
}
#endregion
}
}
第二部分是輔助方法類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace ImageClassLib
{
public class ImageCut1
{
#region 剪裁圖片方法
/// <summary>
/// 剪裁 -- 用GDI+
/// </summary>
/// <param name="b">原始Bitmap,即需要裁剪的圖片</param>
/// <param name="StartX">開始坐標(biāo)X</param>
/// <param name="StartY">開始坐標(biāo)Y</param>
/// <param name="iWidth">寬度</param>
/// <param name="iHeight">高度</param>
/// <returns>剪裁后的Bitmap</returns>
public Bitmap KiCut1(Bitmap b)
{
if (b == null)
{
return null;
}
int w = b.Width;
int h = b.Height;
if (X >= w || Y >= h)
{
return null;
}
if (X + Width > w)
{
Width = w - X;
}
if (Y + Height > h)
{
Height = h - Y;
}
try
{
Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
// Create rectangle for displaying image.
Rectangle destRect = new Rectangle(0, 0, Width, Height); //所畫的矩形正確,它指定所繪制圖像的位置和大小。 將圖像進(jìn)行縮放以適合該矩形。
// Create rectangle for source image.
Rectangle srcRect = new Rectangle(X, Y, Width, Height); //srcRect參數(shù)指定要繪制的 image 對(duì)象的矩形部分。 將此部分進(jìn)行縮放以適合 destRect 參數(shù)所指定的矩形。
g.DrawImage(b, destRect, srcRect, GraphicsUnit.Pixel);
//resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, side, side), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}
#endregion
#region ImageCut1類的構(gòu)造函數(shù)
public int X;
public int Y;
public int Width ;
public int Height;
/// <summary>
/// ImageCut1類的構(gòu)造函數(shù),ImageCut1類用來(lái)獲取鼠標(biāo)在pictureBox1控件所畫矩形內(nèi)的圖像
/// </summary>
/// <param name="x表示鼠標(biāo)在pictureBox1控件上按下時(shí)的橫坐標(biāo)"></param>
/// <param name="y表示鼠標(biāo)在pictureBox1控件上按下時(shí)的縱坐標(biāo)"></param>
/// <param name="width表示鼠標(biāo)在pictureBox1控件上松開鼠標(biāo)的寬度"></param>
/// <param name="heigth表示鼠標(biāo)在pictureBox1控件上松開鼠標(biāo)的高度"></param>
public ImageCut1(int x, int y, int width, int heigth)
{
X = x;
Y = y;
Width = width;
Height = heigth;
}
#endregion
}
}
實(shí)現(xiàn)的效果如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:C#基于純數(shù)學(xué)方法遞歸實(shí)現(xiàn)貨幣數(shù)字轉(zhuǎn)換中文功能詳解
本文標(biāo)題:C# 實(shí)現(xiàn)QQ式截圖功能實(shí)例代碼
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5909.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子


