C\#QQ 캡 처 기능 인 스 턴 스 코드 구현

16243 단어 캡 처C#
이 기능 은 모두 두 부분 으로 구성 되 어 있 으 며,첫 번 째 부분 은 창 코드 이 고,다른 부분 은 보조 적 인 방법 이다.여러분 이 참고 할 수 있 도록 코드 를 직접 붙 입 니 다.

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
  }
}

 실현 효 과 는 다음 과 같다.

 이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기