C # Implementation of the QQ screenshot function instance code detailed introduction

Source: Internet
Author: User
This article mainly introduces the C # implementation of QQ-style function example code, small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.

This function is composed of two parts, the first part is the form code, the other part is an auxiliary method. Directly post the code for your reference:

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 Click Open Image public string Strheadimagepath; Open the path of the picture Bitmap Bi; Defines a bitmap pair like private void button1_click (object sender, EventArgs e) {openfiledialog1.filter = "*.gif|*.jpg|*. jpeg|*.     Jpeg|*.bmp|*.bmp "; Set Read picture type if (openfiledialog1.showdialog () = = DialogResult.OK) {try {Strheadimagepath          = Openfiledialog1.filename; This.          Show (Strheadimagepath); Bi = new Bitmap (Strheadimagepath);   Use an open picture path to create a bitmap pair like ImageCut1 IC = new IMAGECUT1 (+, this.pictureBox1.Width, this.pictureBox1.Height); Instantiate the Imagecut class, four parameters are represented as: X, y, Width, heigth, (40, 112) indicate picThe coordinates of the lcation of the TureBox1, (120, 144) represent the width and height of the PictureBox1 control This.pictureBox1.Image = IC. KICUT1 ((Bitmap) (this.   Getselectimage (This.pictureBox1.Width, this.pictureBox1.Height)); (120, 144) indicates the width and height of the PictureBox1 control//this.picturebox1.image = (this.        Getselectimage (120, 144));          } catch (Exception ex) {MessageBox.Show ("not formatted"); Ex.        ToString ();      }}} #endregion #region define the display image method that will open the image in the PictureBox1 control that displays public void Show (string strheadimagepath) {  This.pictureBox1.Load (@strHeadImagePath);  #endregion #region Get the image///<summary>///Get the image of the specified width and height even if the picture and the PictureBox1 control are as wide and high, the return value is picture image//    </summary>//<param name= "width for width" ></param>//<param name= "height = high" ></param> <returns></returns> public Image getselectimage (int Width, int Height) {//image initimage =      This.pictureBox1.Image;      Image initimage = Bi; The original width and height are smaller than the template, not for processing,Save directly if (initimage.width <= Width && initimage.height <= Height) {//initimage.save (Filesa        Veurl, System.Drawing.Imaging.ImageFormat.Jpeg);      return initimage;        } else {//the width, height int initwidth = Initimage.width of the original picture;        int initheight = Initimage.height; Non-square first clipped to square if (initwidth! = initheight) {//object System.Drawing.Image pickedimage = nul          L          System.Drawing.Graphics pickedg = null; Wide-height cross-view if (Initwidth > Initheight) {//object instantiation Pickedimage = new System.draw Ing.            Bitmap (Initheight, initheight);            PICKEDG = System.Drawing.Graphics.FromImage (pickedimage);            Set quality Pickedg.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;            Pickedg.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; Positioning Rectangle FROMR = new RectAngle ((initwidth-initheight)/2, 0, Initheight, initheight);            Rectangle ToR = new Rectangle (0, 0, initheight, initheight);            Drawing Pickedg.drawimage (Initimage, ToR, FROMR, System.Drawing.GraphicsUnit.Pixel);          Reset width initwidth = initheight; }//tall and wide vertical diagram else {//object instantiation Pickedimage = new System.Drawing.Bitmap (INITW            Idth, Initwidth);            PICKEDG = System.Drawing.Graphics.FromImage (pickedimage);            Set quality Pickedg.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;            Pickedg.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;            Positioning Rectangle FROMR = new Rectangle (0, (initheight-initwidth)/2, initwidth, initwidth);            Rectangle ToR = new Rectangle (0, 0, initwidth, initwidth); Drawing Pickedg.drawimage (Initimage, ToR, FROMR, System.Drawing.GraphicsUnIt.            Pixel);          Reset High initheight = Initwidth;          } initimage = (System.Drawing.Image) pickedimage.clone ();          Release resources Pickedg.dispose ();        Pickedimage.dispose ();      } return initimage; }} #endregion #region Click button2 button Event private void Button2_Click (object sender, EventArgs e) {This.lab El1.      Text = "Cropped picture width:" +this.picturebox2.width.tostring ();    This.label2.Text = "Cropped picture height:" +this.picturebox2.height.tostring ();   } #endregion #region The variables used to scale, crop the image///<summary>//////</summary> int x1;   Mouse pressed when the horizontal axis int y1; When the mouse is pressed, ordinate int width; The wide int heigth of the open image;  The open image has a high bool Headimagebool = false;  This Boolean variable is used to determine if the PictureBox1 control has a picture #endregion #region the variable point P1 used to draw the rectangle;  Defines the point P2 when the mouse is pressed;  Defines the point P3 when moving the mouse; Defines the coordinate point at which the mouse is released #endregion events that occur when you #region the mouse button private void Picturebox1_mousedown (object sender, MouseEventArgsE) {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 Move the event that occurs with the mouse 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 coordinates {THIS.P3 = new Point (p1) when the mouse moves from the upper left to the beginning. X, p1.        Y); } if ((P2. X-p1. X) < 0 && (p2. Y-p1. Y) > 0)//P3 coordinates {THIS.P3 = new Point (p2) when the mouse moves from the upper right to the lower left. X, p1.        Y); } if ((P2. X-p1. X) > 0 && (p2. Y-p1. Y) < 0)//P3 coordinates {THIS.P3 = new Point (p1) when the mouse moves up from the lower-left corner. X, p2.        Y); } if ((P2. X-p1. X) < 0 && (p2. Y-p1.         Y) < 0)//P3 coordinates when the mouse starts to move from the lower-right corner to the left direction { THIS.P3 = new Point (P2. X, p2.        Y); } this.pictureBox1.Invalidate (); Invalidates the entire surface of the control and causes the control to redraw}} #endregion #region release the mouse event, instantiate the IMAGECUT1 class pair like ImageCut1 IC1;        Defines the image of the drawn rectangle as 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)//occurs {IC1 = new ImageCut1 (x1, y1, M) when the mouse moves from the upper-left corner to the lower right Ath.  Abs (E.X-X1), Math.Abs (e.y-y1));          Instantiate IMAGECUT1 Class} if ((E.X-X1) < 0 && (e.y-y1) > 0)//Occurs when the mouse moves from the upper-right corner to the lower-left to start  IC1 = new ImageCut1 (e.x, Y1, Math.Abs (e.x-x1), Math.Abs (e.y-y1));          Instantiate IMAGECUT1 Class} if ((e.x-x1) > 0 && (e.y-y1) < 0)//Occurs when the mouse moves from the lower-left corner to the top-right to the beginning of the {  IC1 = new IMAGECUT1 (x1, e.y, Math.Abs (e.x-x1), Math.Abs (e.y-y1)); Instantiate the ImageCut1 class} if (E.x-x1) < 0 && (E.Y-Y1) < 0)//occurs {IC1 = new ImageCut1 (e.x, E.y, Math.Abs (E.) When the mouse moves from the lower-right corner to the top-left.   X-X1), Math.Abs (e.y-y1)); Instantiate IMAGECUT1 class} 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 get the selected rectangle image///<summary>///////<param name= "Wid TH "></param>//<param name=" Height "></param>//<returns></returns> public Im      Age GetSelectImage1 (int Width, int Height) {Image initimage = this.pictureBox1.Image;      Image initimage = Bi; The original width and height are smaller than the template, without processing, directly save if (initimage.width <= width && initimage.height < = Height) {//initimage.save (Filesaveurl, System.Drawing.Imaging.ImageFormat.Jpeg);      return initimage;        } else {//the width, height int initwidth = Initimage.width of the original picture;        int initheight = Initimage.height; Non-square first clipped to square if (initwidth! = initheight) {//object System.Drawing.Image pickedimage = nul          L          System.Drawing.Graphics pickedg = null; Wide-height cross-view if (Initwidth > Initheight) {//object instantiation Pickedimage = new System.draw Ing.            Bitmap (Initheight, initheight);            PICKEDG = System.Drawing.Graphics.FromImage (pickedimage);            Set quality Pickedg.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;            Pickedg.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;           Positioning Rectangle FROMR = new Rectangle ((initwidth-initheight)/2, 0, Initheight, initheight); Rectangle ToR = new Rectangle (0, 0, initheight, initheight);            Drawing Pickedg.drawimage (Initimage, ToR, FROMR, System.Drawing.GraphicsUnit.Pixel);          Reset width initwidth = initheight; }//tall and wide vertical diagram else {//object instantiation Pickedimage = new System.Drawing.Bitmap (INITW            Idth, Initwidth);            PICKEDG = System.Drawing.Graphics.FromImage (pickedimage);            Set quality Pickedg.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;            Pickedg.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;            Positioning Rectangle FROMR = new Rectangle (0, (initheight-initwidth)/2, initwidth, initwidth);            Rectangle ToR = new Rectangle (0, 0, initwidth, initwidth);            Drawing Pickedg.drawimage (Initimage, ToR, FROMR, System.Drawing.GraphicsUnit.Pixel);          Reset High initheight = Initwidth; }          Initimage = (System.Drawing.Image) pickedimage.clone ();          Release resources Pickedg.dispose ();        Pickedimage.dispose ();      } return initimage;  }} #endregion #region redraw the PictureBox1 control, that is, move the mouse to draw the rectangle private void Picturebox1_paint (object sender, PaintEventArgs e) {if (headimagebool) {Pen p = new Pen (Color.Black, 1);//Brush P.dashstyle = System.Drawing.Dr        Awing2D.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}}

The second part is the Auxiliary method class

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 clipping picture square French//<summary>///tailoring-with GDI +//</summary>//<param name= "B" > Original bitmap, which is a picture to be cropped </     param>//<param name= "StartX" > Start coordinates x</param>//<param name= "Starty" > Start coordinates y</param> <param name= "iwidth" > Width </param>//<param name= "iheight" > Height </param>//<returns       > Trimmed 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); The rectangle you draw is correct, which specifies the position and size of the image being drawn.        The image is scaled to fit the rectangle.        Create rectangle for source image.   Rectangle srcrect = new Rectangle (X, Y, Width, Height); The srcrect parameter specifies the rectangular portion of the image object to be drawn.        This section is scaled to fit the rectangle specified by the Destrect parameter.        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 The constructor of the IMAGECUT1 class public int X;     public int Y;     public int Width;    public int Height; <summary>///IMAGECUT1 class constructor, ImaThe GECUT1 class is used to get the image of the mouse in the rectangle drawn by the PictureBox1 control.//</summary>//<param name= "x represents the horizontal axis when the mouse is pressed on a pictureBox1 control" ></p aram>//<param name= "Y represents the ordinate when the mouse is pressed on the PictureBox1 control" ></param>//<param name= "width means mouse in PictureBox 1 release the mouse width on the control "></param>//<param name=" heigth indicates that the mouse has released the mouse height on the pictureBox1 control "></param> public Image      CUT1 (int x, int y, int width, int heigth) {x = x;      y = y;      width = width;    Height = Heigth; } #endregion}}

The effect of the implementation is as follows:

The above is the C # implementation of QQ-style function instance code details of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.