C# 實現QQ式截圖功能執行個體代碼詳細介紹

來源:互聯網
上載者:User
本篇文章主要介紹了C# 實現QQ式功能執行個體代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

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式功能執行個體代碼詳細介紹的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.