백엔드 개발 C#.Net 튜토리얼 C#에서 QQ 스타일 스크린샷 기능을 구현하기 위한 예제 코드에 대한 자세한 소개

C#에서 QQ 스타일 스크린샷 기능을 구현하기 위한 예제 코드에 대한 자세한 소개

Mar 07, 2017 am 10:50 AM

이 글에서는 주로 QQ 스타일의 스크린샷 기능을 구현하기 위한 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
  }
}
로그인 후 복사

달성된 효과는 다음과 같습니다.

위는 QQ 스타일 스크린샷을 구현하기 위한 C#의 예제 코드를 자세히 소개한 것입니다. 기능 및 기타 관련 내용은 PHP 중국어 홈페이지(www.php.cn)를 주목해주세요!


본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

C#을 사용한 Active Directory C#을 사용한 Active Directory Sep 03, 2024 pm 03:33 PM

C#을 사용한 Active Directory 가이드. 여기에서는 소개와 구문 및 예제와 함께 C#에서 Active Directory가 작동하는 방식에 대해 설명합니다.

C# 직렬화 C# 직렬화 Sep 03, 2024 pm 03:30 PM

C# 직렬화 가이드. 여기에서는 C# 직렬화 개체의 소개, 단계, 작업 및 예제를 각각 논의합니다.

C#의 난수 생성기 C#의 난수 생성기 Sep 03, 2024 pm 03:34 PM

C#의 난수 생성기 가이드입니다. 여기서는 난수 생성기의 작동 방식, 의사 난수 및 보안 숫자의 개념에 대해 설명합니다.

C# 데이터 그리드 보기 C# 데이터 그리드 보기 Sep 03, 2024 pm 03:32 PM

C# 데이터 그리드 뷰 가이드. 여기서는 SQL 데이터베이스 또는 Excel 파일에서 데이터 그리드 보기를 로드하고 내보내는 방법에 대한 예를 설명합니다.

C#의 패턴 C#의 패턴 Sep 03, 2024 pm 03:33 PM

C#의 패턴 가이드. 여기에서는 예제 및 코드 구현과 함께 C#의 패턴 소개 및 상위 3가지 유형에 대해 설명합니다.

C#의 소수 C#의 소수 Sep 03, 2024 pm 03:35 PM

C#의 소수 가이드. 여기서는 코드 구현과 함께 C#의 소수에 대한 소개와 예를 논의합니다.

C#의 팩토리얼 C#의 팩토리얼 Sep 03, 2024 pm 03:34 PM

C#의 팩토리얼 가이드입니다. 여기서는 다양한 예제 및 코드 구현과 함께 C#의 계승에 대한 소개를 논의합니다.

멀티 스레딩과 비동기 C#의 차이 멀티 스레딩과 비동기 C#의 차이 Apr 03, 2025 pm 02:57 PM

멀티 스레딩과 비동기식의 차이점은 멀티 스레딩이 동시에 여러 스레드를 실행하는 반면, 현재 스레드를 차단하지 않고 비동기식으로 작업을 수행한다는 것입니다. 멀티 스레딩은 컴퓨팅 집약적 인 작업에 사용되며 비동기식은 사용자 상호 작용에 사용됩니다. 멀티 스레딩의 장점은 컴퓨팅 성능을 향상시키는 것이지만 비동기의 장점은 UI 스레드를 차단하지 않는 것입니다. 멀티 스레딩 또는 비동기식을 선택하는 것은 작업의 특성에 따라 다릅니다. 계산 집약적 작업은 멀티 스레딩을 사용하고 외부 리소스와 상호 작용하고 UI 응답 성을 비동기식으로 유지 해야하는 작업을 사용합니다.

See all articles