亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

C# 實現QQ式截圖功能實例代碼

2019-10-29 21:14:07
字體:
來源:轉載
供稿:網友

這個功能一共有兩部分組成,第一部分是窗體代碼,另外的一部分是一個輔助方法。直接貼出代碼,以供大家參考:

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 點擊打開圖像    public string strHeadImagePath; //打開圖片的路徑    Bitmap Bi; //定義位圖對像    private void button1_Click(object sender, EventArgs e)    {      openFileDialog1.Filter = "*.gif|*.jpg|*.JPEG|*.JPEG|*.bmp|*.bmp";     //設置讀取圖片類型      if (openFileDialog1.ShowDialog() == DialogResult.OK)      {        try        {          strHeadImagePath = openFileDialog1.FileName;          //this.Show(strHeadImagePath);          Bi = new Bitmap(strHeadImagePath); //使用打開的圖片路徑創建位圖對像          ImageCut1 IC = new ImageCut1(40, 112, this.pictureBox1.Width, this.pictureBox1.Height);   //實例化ImageCut類,四個參數據分別表示為:x、y、width、heigth,(40、112)表示pictureBox1的Lcation的坐標,(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("格式不對");          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)        {          //截圖對象           System.Drawing.Image pickedImage = null;          System.Drawing.Graphics pickedG = null;          //寬大于高的橫圖           if (initWidth > initHeight)          {            //對象實例化             pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);            pickedG = System.Drawing.Graphics.FromImage(pickedImage);            //設置質量             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          {            //對象實例化            pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);            pickedG = System.Drawing.Graphics.FromImage(pickedImage);            //設置質量             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 點擊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;   //鼠標按下時橫坐標    int y1;   //鼠標按下時縱坐標    int width; //所打開的圖像的寬    int heigth; //所打開的圖像的高    bool HeadImageBool = false;  // 此布爾變量用來判斷pictureBox1控件是否有圖片    #endregion    #region 畫矩形使用到的變量    Point p1;  //定義鼠標按下時的坐標點    Point p2;  //定義移動鼠標時的坐標點    Point p3;  //定義松開鼠標時的坐標點    #endregion    #region 鼠標按下時發生的事件    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 移動鼠標發生的事件    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)   //當鼠標從左上角向開始移動時P3坐標        {          this.p3 = new Point(p1.X, p1.Y);        }        if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) > 0)   //當鼠標從右上角向左下方向開始移動時P3坐標        {          this.p3 = new Point(p2.X, p1.Y);        }        if ((p2.X - p1.X) > 0 && (p2.Y - p1.Y) < 0)   //當鼠標從左下角向上開始移動時P3坐標        {          this.p3 = new Point(p1.X, p2.Y);        }        if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) < 0)   //當鼠標從右下角向左方向上開始移動時P3坐標        {          this.p3 = new Point(p2.X, p2.Y);        }        this.pictureBox1.Invalidate(); //使控件的整個圖面無效,并導致重繪控件      }    }    #endregion    #region 松開鼠標發生的事件,實例化ImageCut1類對像    ImageCut1 IC1; //定義所畫矩形的圖像對像    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)  //當鼠標從左上角向右下方向開始移動時發生        {          IC1 = new ImageCut1(x1, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1));  //實例化ImageCut1類        }        if ((e.X - x1) < 0 && (e.Y - y1) > 0)  //當鼠標從右上角向左下方向開始移動時發生        {          IC1 = new ImageCut1(e.X, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1));  //實例化ImageCut1類        }        if ((e.X - x1) > 0 && (e.Y - y1) < 0)  //當鼠標從左下角向右上方向開始移動時發生        {          IC1 = new ImageCut1(x1, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1));  //實例化ImageCut1類        }        if ((e.X - x1) < 0 && (e.Y - y1) < 0)  //當鼠標從右下角向左上方向開始移動時發生        {          IC1 = new ImageCut1(e.X, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1));   //實例化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)        {          //截圖對象           System.Drawing.Image pickedImage = null;          System.Drawing.Graphics pickedG = null;          //寬大于高的橫圖           if (initWidth > initHeight)          {            //對象實例化             pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);            pickedG = System.Drawing.Graphics.FromImage(pickedImage);            //設置質量             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          {            //對象實例化            pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);            pickedG = System.Drawing.Graphics.FromImage(pickedImage);            //設置質量             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控件,即移動鼠標畫矩形    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">開始坐標X</param>     /// <param name="StartY">開始坐標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);    //所畫的矩形正確,它指定所繪制圖像的位置和大小。 將圖像進行縮放以適合該矩形。        // Create rectangle for source image.        Rectangle srcRect = new Rectangle(X, Y, Width, Height);   //srcRect參數指定要繪制的 image 對象的矩形部分。 將此部分進行縮放以適合 destRect 參數所指定的矩形。        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類的構造函數    public int X;     public int Y;     public int Width ;     public int Height;    /// <summary>    /// ImageCut1類的構造函數,ImageCut1類用來獲取鼠標在pictureBox1控件所畫矩形內的圖像    /// </summary>    /// <param name="x表示鼠標在pictureBox1控件上按下時的橫坐標"></param>    /// <param name="y表示鼠標在pictureBox1控件上按下時的縱坐標"></param>    /// <param name="width表示鼠標在pictureBox1控件上松開鼠標的寬度"></param>    /// <param name="heigth表示鼠標在pictureBox1控件上松開鼠標的高度"></param>    public ImageCut1(int x, int y, int width, int heigth)    {      X = x;      Y = y;      Width = width;      Height = heigth;    }    #endregion  }}

 實現的效果如下:

C#實現QQ截圖工具,C#實現截圖工具,C#截圖工具

 以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
精品人伦一区二区三区蜜桃网站| 日韩av免费网站| 国语自产精品视频在免费| 一本色道久久综合狠狠躁篇的优点| 国产精品狠色婷| 久久在线精品视频| 九九九久久久久久| 日韩av手机在线观看| 97免费视频在线播放| 日韩高清电影好看的电视剧电影| 日本在线观看天堂男亚洲| 国产欧美韩国高清| 国产成人在线一区| 久久久免费观看视频| 中文字幕最新精品| 国产成人亚洲综合91| 国产一区二区三区18| 日本久久久久久久久久久| 国产日韩欧美91| 日韩欧美成人免费视频| 精品夜色国产国偷在线| 亚洲国产私拍精品国模在线观看| 欧洲日韩成人av| 国语自产精品视频在线看| 国产精品激情自拍| 亚洲视频在线免费观看| 久久免费视频在线观看| 久久精品电影一区二区| 日日狠狠久久偷偷四色综合免费| 疯狂做受xxxx高潮欧美日本| 97在线视频免费| 久久久久久成人精品| 91免费综合在线| 亚洲黄一区二区| 欧美日韩国产一中文字不卡| 亚洲精品免费在线视频| 成人有码视频在线播放| 国内精品模特av私拍在线观看| 国产精品99久久久久久白浆小说| 夜夜嗨av一区二区三区免费区| 亚洲天天在线日亚洲洲精| 午夜精品美女自拍福到在线| 国产精品一二三视频| 日韩欧美第一页| 日韩av电影手机在线| 国产盗摄xxxx视频xxx69| 亚洲精品视频在线播放| 亚洲精品wwww| 成人xvideos免费视频| 一本色道久久综合亚洲精品小说| y97精品国产97久久久久久| 一个人看的www久久| 欧美日韩亚洲国产一区| 亚洲第一天堂av| 成人h视频在线观看播放| 色哟哟亚洲精品一区二区| 久久久国产影院| 亚洲欧洲成视频免费观看| 免费91在线视频| 在线日韩第一页| 精品免费在线观看| 91中文字幕一区| 国产美女主播一区| 久久精品国产v日韩v亚洲| 欧美—级高清免费播放| 日韩电视剧免费观看网站| 亚洲福利小视频| 国产精品三级久久久久久电影| 中文字幕日韩欧美精品在线观看| 欧美成人四级hd版| 国产精品成人v| 亚洲自拍偷拍区| 成人精品aaaa网站| 国产精品一区二区三区毛片淫片| 欧美中文在线免费| 91最新国产视频| 国产69精品久久久久久| 97在线视频免费观看| 国产精品pans私拍| 亚洲高清色综合| 日本不卡视频在线播放| 亚洲精品自拍偷拍| 欧美激情视频在线免费观看 欧美视频免费一| 日韩精品在线免费观看视频| 欧美自拍大量在线观看| 国产亚洲成av人片在线观看桃| **欧美日韩vr在线| 欧美亚洲在线播放| 日韩视频一区在线| 成人黄色午夜影院| 日韩在线精品一区| 久久综合国产精品台湾中文娱乐网| 亚洲a级在线播放观看| 中文字幕av一区二区| www.欧美三级电影.com| 欧美xxxx14xxxxx性爽| 亚洲乱码av中文一区二区| 欧美自拍大量在线观看| 91精品国产自产在线观看永久| 欧美激情xxxx性bbbb| 91超碰中文字幕久久精品| 国产性猛交xxxx免费看久久| 伊人久久大香线蕉av一区二区| 国产精品一区专区欧美日韩| 国产精品扒开腿做爽爽爽视频| 一区二区三区视频在线| 久久99国产综合精品女同| 国产精品大片wwwwww| 亚洲aⅴ日韩av电影在线观看| 日韩高清电影免费观看完整| 欧美亚洲国产视频小说| 精品国产网站地址| 国产精品一区二区av影院萌芽| 久久免费国产视频| 欧美另类69精品久久久久9999| 国产精品久久久av| 久久亚洲影音av资源网| 日本午夜人人精品| 国色天香2019中文字幕在线观看| 国产精品电影一区| 亚洲一区二区久久久| 久久精品99无色码中文字幕| 欧美精品videosex极品1| 国产一区二区三区网站| 日韩一区二区精品视频| 精品国产成人av| 日韩在线精品一区| 久久精品国产久精国产一老狼| 亚洲三级免费看| 国产视频精品久久久| 国产精品久久久久福利| 日本一区二区在线免费播放| 国产精品久久久久久久久久久久| 日韩欧美主播在线| 国产亚洲一区精品| 日韩欧美大尺度| 全色精品综合影院| 日韩在线视频网| 亚洲最大成人网色| 97视频免费在线看| 中文字幕在线成人| 国产精品18久久久久久麻辣| 国产精品色婷婷视频| 午夜精品一区二区三区在线| 一区二区三区高清国产| 国产精品日本精品| 国产成人精品电影久久久| 亚洲自拍偷拍视频| 国产精品69久久久久| 国产不卡av在线免费观看| 亚洲国产日韩欧美在线动漫| 91精品久久久久久久久久久久久久| 91亚洲国产成人久久精品网站| 97成人精品区在线播放| 国产成+人+综合+亚洲欧洲| 在线观看国产欧美| 久久久999国产精品| 在线一区二区日韩| 日韩精品免费一线在线观看| 久久99久久99精品中文字幕| 亚洲精品国精品久久99热一| 国产视频精品在线| 日韩欧美在线观看| 欧美肥婆姓交大片|