C # GDI + simple Drawing (iii)

Source: Internet
Author: User
The first two articles have basically introduced the basic knowledge of drawing. So, I'll use the two we learned, and do a few examples.
We first to do a simple----imitation QQ, the example of this is actually online already have this information, but for the integrity of the article, or feel the need to explain.
Let's look at the effect first:


Now let's see how this is done.
Idea: There is a button on the chat form, and when the button is clicked, the program draws the entire screen on a new full-screen form, and then displays the form. Because it's a full-screen form, and it hides the menu bar, toolbars, and so on, so it looks like a desktop to us, and then we draw rectangles on this new form, Finally, the contents of the rectangle are saved and displayed in the original chat form.
Steps:
A. Create a new form. Named catch. The FormBorderStyle for this form is then set to None,windowstate to maximized.
B. We edit the code:

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.text;using system.windows.forms;namespace client{public partial class Catch:form {public Cat        CH () {InitializeComponent (); } User variables//form initialization operation private void Catch_load (object sender, EventArgs e) {this. SetStyle (Controlstyles.optimizeddoublebuffer | Controlstyles.allpaintinginwmpaint |            Controlstyles.userpaint, True); This.            UpdateStyles (); The above two sentences are in order to set the control style to double buffering, which can effectively reduce the problem of picture flicker, about which everyone can go on their own to search under originbmp = new Bitmap (this. BackgroundImage);//backgroundimage is a full screen image, we also use variables to save the full screen picture}//right mouse click End private void Catch_mouseclick (Obje CT sender, MouseEventArgs e) {if (E.button = = mousebuttons.right) {this.                DialogResult = DialogResult.OK; This.            Close ();      }}//left mouse button pressed action  private void Catch_mousedown (object sender, MouseEventArgs e) {if (E.button = = MouseButtons.Left) {if (!                    Catchstart) {//If snapping does not start Catchstart = true; Downpoint = new Point (e.x, e.y);//save Mouse Press Coordinates}}} private void Catch_mousemove (Obje CT sender, MouseEventArgs e) {if (Catchstart) {//If Snap starts Bitmap destbmp = (Bit                MAP) Originbmp.clone ();//Create a new picture object and make it the same as the original image point Newpoint = new Point (Downpoint.x, DOWNPOINT.Y);//Get the coordinates of the mouse                Graphics g = graphics.fromimage (destbmp);//Create a new artboard on the newly created image pen P = new Pen (color.blue,1); int width = Math.Abs (e.x-downpoint.x), height = Math.Abs (E.Y-DOWNPOINT.Y);//Gets the length and width of the rectangle if (                E.x < downpoint.x) {newpoint.x = e.x;          } if (E.y < DOWNPOINT.Y)      {newpoint.y = e.y; } catchrect = new Rectangle (newpoint,new Size (width,height));//Save Rectangle G.drawrectangle (P,CATCHR                ECT);//Draw a rectangle on this artboard g.dispose ();//Release the current artboard P.dispose (); Graphics G1 = this. CreateGraphics ();//re-create a graphics class//If the previous artboard is not released, and G=this directly. CreateGraphics () does not release the first created G, because it simply transfers the address to the new G. As String G1 = this. CreateGraphics ();//Create a new artboard G1 on the entire full-screen form.                DrawImage (destbmp,new Point (0,0));//Draw the picture you just painted onto this form//This can also belong to two buffer technology, if the rectangle is drawn directly on the form, it will cause the picture to shake and there will be an infinite number of rectangles. G1.                Dispose (); Destbmp.dispose ();//To be released in time, otherwise the memory will be consumed heavily} private void Catch_mouseup (object Sende                R, MouseEventArgs e) {if (E.button = = MouseButtons.Left) {if (Catchstart)                    {Catchstart = false; catchfinished = tRue }}}//mouse double-click event, if the mouse is inside the rectangle, save the picture inside the rectangle to the clipboard private void Catch_mousedoubleclick (object sender                , MouseEventArgs e) {if (E.button = = mousebuttons.left&&catchfinished) { if (Catchrect.contains (new Point (e.x, e.y))) {Bitmap catchedbmp = new Bitmap (CATCHR Ect.                    Width, catchrect.height);//Create a new blank picture of a rectangle equal to the graphics g = graphics.fromimage (catchedbmp);                    G.drawimage (Originbmp, New Rectangle (0, 0, Catchrect.width, catchrect.height), Catchrect, GraphicsUnit.Pixel);                    Draws the specified part of the orginbmp to the artboard Clipboard.setimage (catchedbmp) by the specified size;//Save picture to Clipboard                    G.dispose ();                    Catchfinished = false; This.                    BackgroundImage = originbmp;                    Catchedbmp.dispose (); This.                    DialogResult = DialogResult.OK; ThiS.close (); }            }        }    }}

C. After the catch form is created, we add the following event to the button (on the chat form):

 private void Bcatch_click (object sender, EventArgs e) {if (bcatch_hidecurrent.checked) { This.                Hide ();//hides the current form thread.sleep (50);//Let the thread sleep for some time, the form disappears need a little time catch catchform = new catch (); Bitmap catchbmp = new Bitmap (Screen.allscreens[0]. Bounds.width, Screen.allscreens[0].                Bounds.height);//Create a new picture with the same screen size as the graphics g = graphics.fromimage (catchbmp); G.copyfromscreen (new point (0, 0), new Point (0, 0), New Size (Screen.allscreens[0]. Bounds.width, Screen.allscreens[0]. Bounds.height));//Save full screen picture Catchform.backgroundimage = catchbmp;//Set the background of the catch form to full screen picture if (CATC Hform.showdialog () = = DialogResult.OK) {//If the catch form ends, the picture in the Clipboard is placed in the message send box IDataObject iDat                    A = Clipboard.getdataobject ();                    Dataformats.format MyFormat = Dataformats.getformat (Dataformats.bitmap); if (Idata.getdatapresent (DaTaformats.bitmap)) {richTextBox1.                        Paste (MyFormat); Clipboard.clear ();//Clears the Clipboard object} this. Show ();//re-display the form}}}

So our function is done.
I think it's a difficult question for beginners how to eliminate the first drawing of pictures. If you do not take action, you will find that as long as you move the mouse, you will draw a rectangle, so that there will be more than n rectangles, and we just want the last one.
There are two ways to solve this problem in general:
1. When drawing the second shape, we will first redraw the last drawing with the same color as the background. However, this often requires a solid background color when used.
2. We do not directly draw the graphic on the artboard, we use a picture A to save the image on the original artboard. Then create a new picture B that is the same as picture A, draw the graphic we want to draw on the picture B, and then draw the picture B on the artboard. So the picture A has not been changed. So the second time, we still create a new picture with the same picture a to draw. Then the last graph will not be preserved. The problem is solved.

Next time, I'll show you how to make a program that imitates Windows Artboards.


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.