C# GDI+ Programming (4)

高洛峰
Release: 2016-12-17 10:11:53
Original
1266 people have browsed it

Screenshot
The CopyFromScreen function in the Grahpics class can copy the screen to the Graphics object. If the Graphics object is created from the window Form, then the screen is directly displayed in the window. Look at the example: add a button to the window, and then add a 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);
}
One parameter and the second parameter indicate where to start copying on the screen, and the following 0,0 means copying the screen to the window and starting to display it from where in the window.
this.Size is the size (width and height) to be copied.

It’s in VC++. This is just a different way of saying it. The end result is still the same.

The previous Graphics objects were created through windows. This time we create Graphics objects through pictures, so that we can draw on the pictures.

The creation of image objects is not the only way to load files. We can create an "empty" bitmap (through the constructor). There is no specific data in this bitmap, or the default data in it is useless. We just need such a container. Draw inside and add data.

Example: Take a screenshot and save the image as a file
private void button1_Click(object sender, EventArgs e)
{
//Create a Bitmap image, 800 wide, 600 high, a container of this size.
Bitmap bmp = new Bitmap(800, 600);
//Create a Grahpics object from the image
Graphics gr = Graphics.FromImage(bmp);
//Copy the screen to Bitmap
gr.CopyFromScreen(0, 0, 0, 0, new Size(800, 600), CopyPixelOperation.SourceCopy);
//Save as image file
bmp.Save("d:\screen.jpg");
}
How about it, it’s simple enough, four lines The code completes the screenshot and saves it as a file. VC++ requires many lines of code. In addition, Bitmap is derived from the Image class.

Double buffering one-time drawing
Let’s look at an example without double buffering. When the mouse moves within the window, the rectangle in the upper left corner displays the current position of the mouse
public partial class Form1: Form
{
public Form1() I {
initializecomponent ();
this.Mousemove+= Formmousemove;
}
Private void formmousemove (Object Sender, MOUSEEVENTARGS E)
hics gr = this.creategraphics ();
Rectangle Rect = New Rectangle (0, 0 , 100, 35); 3 // Fill in rectangular
Lineargradientbrush Brush =
New Lineargradientbrush (Rect, color.fromargb (44, 55, 66), color.fromargb (123, 150, 189) Adientmode.Horizontal); gr.FillRectangle(brush, rect);
                                                                                                                                                   gr.
             gr.TextRenderingHint = System. Drawing.Text.TextRenderingHint.AntiAlias;
                String str = String.Format("Mouse position: n{0},{1}", e.X, e.Y); ),Brushes.White, rect);
}
}
You can see that when the mouse moves, the rectangle in the upper left corner obviously flashes. This is because three times are drawn, filling the rectangle, drawing the rectangle, and displaying the text. That's what caused it.
The more times you draw, the more obvious this flickering becomes.
And these three drawings only need to get one result in the end, that is, a picture. Then we can use double buffering to complete it. First draw the graphics to be drawn into the Bitmap. Then just draw the Bitmap to the window.
Look at the example:
private void formMouseMove(object sender, MouseEventArgs e)
{
graphics = this.CreateGraphics();
//Create Graphics object from Bitmap
            Bitmap bmp=new Bitmap(100,35);
          Graphics gr = Graphics.FromImage(bmp); new LinearGradientBrush(rect, Color.FromArgb(44, 55, 66) , Color.FromArgb(123, 150, 189),
                                LinearGradientMode                                                                                                     Color.Pen pen = new Pen(Color.Green, 2);
gr.DrawRectangle(pen, rect);
//Display text
gr.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                                                                                                                                              si ’ - , ” , , , , - , , , , , after- - til pu pu pu pu octo pu pu pu pu pu pt q q q q q q q q q q q ("Mouse position: n{0},{1}", e. Draw
          graphics.DrawImage(bmp, 0, 0);

          }


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

Look at the example: (Code of a button click 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 cannot reduce or enlarge the image. The size of the window is the same as the size of the Bitmap.
If you fill in 10, 10, 50, 50, then 0, 0, 50, 50 of the window will be displayed on 10, 10 of the bitmap. Within the 50,50 rectangular area, can the starting position of the window be specified? It can only start from position 0,0.

Get the value of a pixel
The GetPixel function in the Bitmap class can get the Color value of a pixel. If you want to get the color value of a certain pixel in the window, you can first call the DrawToBitmap function and save the

window as a Bitmap. Then get it again. Another: There is also a corresponding function SetPixel to set the color value of a pixel.


Get the Png image display area and create irregular windows.
To obtain the Png image area, you can use the GetPixel function to obtain the color value of each pixel in the image. If the Alpha value is 0, it is transparent, otherwise, add this point to the area. So how to get the Alpha value of a Color object? Call the ToArgb member function. This is a 32-bit integer, which can just store four 8-bit values: A, R, G, B.

Look at the example and extract the Alpha value

{
Color cor1 = Color.FromArgb(123,225,229,230);
//Get the argb value of Color
int argb = cor2.ToArgb();
//Convert to a character array
byte[] bargb = BitConverter.GetBytes(argb);
//bargb[3] stores the Alpha value
String str = String.Format("{0},{1},{2},{3}", bargb[0], bargb[1], bargb[2], bargb[3]);
MessageBox.Show(str);
}
A complete example, get the PNG display area

Sample code:​​​​

public Form1()
                                                                                                                                                                                                                           . //Load PNG image
                        Bitmap bmp = new Bitmap("d:\Image\ win.png");
                                                                                                                                                                                                      win. bmp.Width; x++)
                {
                    Color cor = bmp.GetPixel(x, y);
                    int argb = cor.ToArgb();
                    byte[] bargb = BitConverter.GetBytes(argb);
                    //像素颜色值Not transparent (if (bargb [3]! = 0) {
// add this pixel area area to the path
path.addrectangle (new rectangle (x, y, 1, 1));
}

}
                                                                                                    using   using   using   using           using                 use using ’s ’ s ’ s ‐     ‐                   This will not result in the desired appearance. Although some parts of the PNG image seem to be transparent.
The solution is to make PNG images yourself and follow your own rules. Don't look for it online.

After setting up an irregular window, you can draw the PNG image into the window. However, due to the translucency problem, you have to fill the window with a transparent brush first and then draw it.

But there is another problem. Non-client area window drawing, like our previous drawing, is all drawn in the window client area.

If the picture to be drawn corresponds to the window, you must start drawing from the non-client area. How to do this. Let’s talk about it in the next chapter.


For more C# GDI+ Programming (4) related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!