C# GDI+ Programming (4)
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.
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.
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
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.
{
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The usage methods of symbols in C language cover arithmetic, assignment, conditions, logic, bit operators, etc. Arithmetic operators are used for basic mathematical operations, assignment operators are used for assignment and addition, subtraction, multiplication and division assignment, condition operators are used for different operations according to conditions, logical operators are used for logical operations, bit operators are used for bit-level operations, and special constants are used to represent null pointers, end-of-file markers, and non-numeric values.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

In C, the char type is used in strings: 1. Store a single character; 2. Use an array to represent a string and end with a null terminator; 3. Operate through a string operation function; 4. Read or output a string from the keyboard.

The char array stores character sequences in C language and is declared as char array_name[size]. The access element is passed through the subscript operator, and the element ends with the null terminator '\0', which represents the end point of the string. The C language provides a variety of string manipulation functions, such as strlen(), strcpy(), strcat() and strcmp().

In C language, special characters are processed through escape sequences, such as: \n represents line breaks. \t means tab character. Use escape sequences or character constants to represent special characters, such as char c = '\n'. Note that the backslash needs to be escaped twice. Different platforms and compilers may have different escape sequences, please consult the documentation.

In C language, char type conversion can be directly converted to another type by: casting: using casting characters. Automatic type conversion: When one type of data can accommodate another type of value, the compiler automatically converts it.

In C language, the main difference between char and wchar_t is character encoding: char uses ASCII or extends ASCII, wchar_t uses Unicode; char takes up 1-2 bytes, wchar_t takes up 2-4 bytes; char is suitable for English text, wchar_t is suitable for multilingual text; char is widely supported, wchar_t depends on whether the compiler and operating system support Unicode; char is limited in character range, wchar_t has a larger character range, and special functions are used for arithmetic operations.

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.
