最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# 實現(xiàn)QQ式截圖功能實例代碼

 更新時間:2017年02月08日 15:29:36   作者:FelixWang  
本篇文章主要介紹了C# 實現(xiàn)QQ式截圖功能實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

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); //使用打開的圖片路徑創(chuàng)建位圖對像
          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 鼠標按下時發(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 移動鼠標發(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)   //當鼠標從左上角向開始移動時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 松開鼠標發(fā)生的事件,實例化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)  //當鼠標從左上角向右下方向開始移動時發(fā)生
        {
          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)  //當鼠標從右上角向左下方向開始移動時發(fā)生
        {
          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)  //當鼠標從左下角向右上方向開始移動時發(fā)生
        {
          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)  //當鼠標從右下角向左上方向開始移動時發(fā)生
        {
          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
  }
}

 實現(xiàn)的效果如下:

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

相關文章

  • C#如何使用SHBrowseForFolder導出中文文件夾詳解

    C#如何使用SHBrowseForFolder導出中文文件夾詳解

    這篇文章主要給大家介紹了關于C#如何使用SHBrowseForFolder導出中文文件夾的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習合作工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-11-11
  • C#使用protobuf-net進行序列化的詳細操作

    C#使用protobuf-net進行序列化的詳細操作

    本文帶領大家學習C#中protobuf-net工具的另一種使用體驗,這個工具的使用體驗屬于Code-First模式,先定義類型,并使用注解進行標記,不需要先編寫.proto文件,感興趣的朋友跟隨小編一起看看吧
    2021-11-11
  • C#如何通過QQ郵件發(fā)送驗證碼到指定郵箱

    C#如何通過QQ郵件發(fā)送驗證碼到指定郵箱

    在程序設計中發(fā)送驗證碼是常見的一個功能,用戶在注冊賬號時或忘記密碼后通常需要發(fā)送驗證碼到手機短信或郵箱來驗證身份,這篇文章主要給大家介紹了關于C#如何通過QQ郵件發(fā)送驗證碼到指定郵箱的相關資料,需要的朋友可以參考下
    2024-01-01
  • C#創(chuàng)建背景色漸變窗體的方法實例

    C#創(chuàng)建背景色漸變窗體的方法實例

    在窗體設計時,可以通過設置窗體的BackColor屬性來改變窗口的背景顏色,但是該屬性改變后整個窗體的客戶區(qū)都會變成這種顏色,這樣顯得非常單調,本文給大家介紹了C#創(chuàng)建背景色漸變窗體的方法實例,需要的朋友可以參考下
    2024-04-04
  • 基于集合的子集與集合的全排列的相關問題

    基于集合的子集與集合的全排列的相關問題

    本篇文章介紹了,集合的子集與集合的全排列的相關系列問題說明,需要的朋友參考下
    2013-05-05
  • 深入淺析Restful接口的兩種使用方式

    深入淺析Restful接口的兩種使用方式

    restful接口常用的兩種方式是get和post.接下來通過本文給大家介紹Restful接口的兩種使用方式,本文給大家介紹的非常詳細,需要的朋友參考下吧
    2018-09-09
  • C#文件路徑操作詳細總結

    C#文件路徑操作詳細總結

    本篇文章主要是對C#中的文件路徑操作進行了詳細的總結介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C#將html table 導出成excel實例

    C#將html table 導出成excel實例

    C#將html table 導出成excel實例,需要的朋友可以參考一下
    2013-04-04
  • c#?chart縮放,局部放大問題

    c#?chart縮放,局部放大問題

    這篇文章主要介紹了c#?chart縮放,局部放大問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • C#中調用SAPI實現(xiàn)語音合成的2種方法

    C#中調用SAPI實現(xiàn)語音合成的2種方法

    這篇文章主要介紹了C#中調用SAPI實現(xiàn)語音合成的2種方法,本文直接給出示例代碼,需要的朋友可以參考下
    2015-06-06

最新評論

定安县| 东平县| 玉屏| 津市市| 黑山县| 陇南市| 瑞安市| 綦江县| 沐川县| 利津县| 江油市| 东丽区| 视频| 油尖旺区| 花垣县| 兰考县| 普定县| 新竹县| 汶川县| 罗甸县| 临颍县| 七台河市| 金川县| 郸城县| 井研县| 汶上县| 交口县| 清远市| 克什克腾旗| 梨树县| 轮台县| 六盘水市| 隆回县| 沈阳市| 惠州市| 莱阳市| 临泽县| 新野县| 大化| 德庆县| 西吉县|