C # GDI + programming (IV)

Source: Internet
Author: User
Screen Cutting
The CopyFromScreen function in the Grahpics class allows you to copy the screen to a Graphics object. If the graphics object is created from a window form, the screen directly
displayed in the window. See Example: Add a button to the window and then add the Click event handler to the button.
The code in the function is as follows:
private void Button1_Click (object sender, EventArgs e)
{
This. CreateGraphics (). CopyFromScreen (50,50, 0, 0, this. Size,copypixeloperation.sourcecopy);
}
The first parameter and the second parameter indicate where to start copying from the screen, and the next 0, 0, is to copy the screen to the window, starting from where the window is displayed.
This. Size is the sized (wide height) to copy.

is in VC + +. There's just another way of saying this. The end result is the same.

Before the graphics object was created, all through the window, this time we create the graphics object through the picture, so that we can draw on the picture.
The creation of a picture object is not the only way to load the file. We can create an "empty" bitmap (through a constructor), there is no specific data in this bitmap, or the default data inside, there is no use, we just need such a container. Draw in the inside, add data in.


Example: Screenshot and save the picture as a file
private void Button1_Click (object sender, EventArgs e)
{
Create bitmap pictures, 800 wide, 600 high, containers of this size.
Bitmap bmp = New Bitmap (800, 600);
Create a Grahpics object from a picture
Graphics gr = graphics.fromimage (BMP);
Copy screen to bitmap
Gr. CopyFromScreen (0, 0, 0, 0, New Size (+), copypixeloperation.sourcecopy);
Save as Picture file
Bmp. Save ("d:\\screen.jpg");
}
How, simple enough, four lines of code to complete the screen, and save as a file, VC + + but a lot of lines of code. Another bitmap is derived from the image class.

Double-buffered one-time drawing
Just look at a non-double-buffering example, when the mouse moves within the window, the upper-left corner of the rectangle will show the current position of the mouse
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
This. MouseMove + = Formmousemove;
}
private void Formmousemove (object sender, MouseEventArgs e)
{
Graphics gr = this. CreateGraphics ();
Rectangle rect = new Rectangle (0, 0, 100, 35);
Fill Rectangle
LinearGradientBrush brush =
New LinearGradientBrush (Rect, Color.FromArgb (), Color.FromArgb (123, 150, 189),
Lineargradientmode.horizontal);
Gr. FillRectangle (brush, rect);
Draw a rectangle
Pen pen = new Pen (color.green, 2);
Gr. DrawRectangle (pen, rect);
Display text
Gr. TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
String str = String.Format ("Mouse Position: \n{0},{1}", e.x, e.y);
Gr. DrawString (str, new Font ("Blackbody", one-by-one), Brushes.white, rect);
}
}
As you can see, when the mouse moves, the upper-left corner of the rectangle is noticeably blinking, because it is drawn three times, fills the rectangle, draws a rectangle, and displays the text. to cause.
The more times you draw, the more obvious the flicker will be.
And the three draws end up with a result, that is, to get a picture, then we can use the double buffer to complete, first draw the drawing to bitmap inside. Then you can draw the bitmap to the window.
See Example:
private void Formmousemove (object sender, MouseEventArgs e)
{
Graphics graphics = this. CreateGraphics ();
Create a Graphics object from bitmap
Bitmap bmp=new Bitmap (100,35);
Graphics gr = graphics.fromimage (BMP);
Rectangle rect = new Rectangle (0, 0, 100, 35);
Fill Rectangle
LinearGradientBrush brush =
New LinearGradientBrush (Rect, Color.FromArgb (), Color.FromArgb (123, 150, 189),
Lineargradientmode.horizontal);
Gr. FillRectangle (brush, rect);
Draw a rectangle
Pen pen = new Pen (color.green, 2);
Gr. DrawRectangle (pen, rect);
Display text
Gr. TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
String str = String.Format ("Mouse Position: \n{0},{1}", e.x, e.y);
Gr. DrawString (str, new Font ("Blackbody", one-by-one), Brushes.white, rect);

Draw once
Graphics. DrawImage (BMP, 0, 0);
}

Gets the bitmap of the window (control)
DrawToBitmap function, you can draw the appearance of the window or control into the bitmap. It belongs to the control class, and both the window class and the control class are derived from the control class.
See Example: (a button click the code of the event handler function)
private void Button1_Click (object sender, EventArgs e)
{
Bitmap bmp = New Bitmap (Width, Height);
This. DrawToBitmap (BMP, New Rectangle (0, 0, Width, Height));
Bmp. Save ("d:\\form.jpg");
}
The second parameter of DrawToBitmap is the area where the window is displayed in the bitmap. This is not to reduce or enlarge the picture, the size of the window and the size of the bitmap is consistent,
If you fill 10,10,50,50 that is the window's 0,0,50,50 display in the 10,10,50,50 rectangular area of the bitmap, the window starting position can be specified, only from the position 0,0 start.

Gets the value of a pixel point
The GetPixel function in the bitmap class can get the color value of a pixel, and if you want to get the value of a pixel in a window, you can call the DrawToBitmap function first,
The window is saved as bitmap. and get it again. Another: there is a corresponding function SetPixel set the color value of a pixel.

Gets the PNG picture display area, creating an irregular window.
Get PNG picture area you can use the GetPixel function to get the color value of each pixel in the picture, if the alpha value is 0, it is transparent, otherwise add this point to the area. So how do you get the Alpah value of a color object, call the TOARGB member function, which is a 32-bit integer that stores exactly 4 8-byte values: A,r,g,b.
Take a look at the example, extract the alpha value.
{
Color Cor1 = Color.FromArgb (123,225,229,230);
Gets the ARGB value of color
int ARGB = Cor2. ToArgb ();
Convert to character array
byte[] Bargb = bitconverter.getbytes (ARGB);
BARGB[3] The alpha value is stored
String str = String.Format ("{0},{1},{2},{3}", Bargb[0], bargb[1], bargb[2], bargb[3]);
MessageBox.Show (str);
}
A complete example that gets the PNG display area

Example code:

Public Form1 ()
{
InitializeComponent ();
Path to store PNG opaque portions
GraphicsPath path = new GraphicsPath ();
Loading PNG pictures
Bitmap bmp = New Bitmap ("D:\\image\\win.png");
Determine the color value of each pixel to get the display area of the picture
for (int y = 0; y < bmp. Height; y++)
for (int x = 0; x < BMP. Width; X + +)
{
Color cor = BMP. GetPixel (x, y);
int ARGB = Cor. ToArgb ();
byte[] Bargb = bitconverter.getbytes (ARGB);
Pixel color values are not transparent
if (bargb[3]! = 0)
{
Add this pixel area to the path.
Path. AddRectangle (New Rectangle (x, Y, 1, 1));
}
}
Setting the window display area, creating a region from a path
This. Region = new Region (path);
}

However, this method is a bit unreliable, if the color value of some pixels in a PNG image is translucent, you will not get the desired appearance result. Although there are some places in the PNG image that seem to be transparent.
The solution is to do the PNG picture yourself, according to your own rules. Don't look for it on the Internet.

Set the irregular window, you can draw the PNG picture into the window, but because of the translucent problem, you have to first fill the window with a transparent brush, and then draw.

However, there is a problem, the Non-client Area window drawing, like our previous drawing, are drawn in the window client area.

If the picture and window you want to draw correspond to, you have to start drawing from the non-client area. How is this going to be done? Next chapter.


More C # GDI + programming (iv) related articles please follow topic.alibabacloud.com!

  • 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.