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!

C# and .NET provide powerful features and an efficient development environment. 1) C# is a modern, object-oriented programming language that combines the power of C and the simplicity of Java. 2) The .NET framework is a platform for building and running applications, supporting multiple programming languages. 3) Classes and objects in C# are the core of object-oriented programming. Classes define data and behaviors, and objects are instances of classes. 4) The garbage collection mechanism of .NET automatically manages memory to simplify the work of developers. 5) C# and .NET provide powerful file operation functions, supporting synchronous and asynchronous programming. 6) Common errors can be solved through debugger, logging and exception handling. 7) Performance optimization and best practices include using StringBuild

.NETFramework is a cross-language, cross-platform development platform that provides a consistent programming model and a powerful runtime environment. 1) It consists of CLR and FCL, which manages memory and threads, and FCL provides pre-built functions. 2) Examples of usage include reading files and LINQ queries. 3) Common errors involve unhandled exceptions and memory leaks, and need to be resolved using debugging tools. 4) Performance optimization can be achieved through asynchronous programming and caching, and maintaining code readability and maintainability is the key.

Reasons for C#.NET to remain lasting attractive include its excellent performance, rich ecosystem, strong community support and cross-platform development capabilities. 1) Excellent performance and is suitable for enterprise-level application and game development; 2) The .NET framework provides a wide range of class libraries and tools to support a variety of development fields; 3) It has an active developer community and rich learning resources; 4) .NETCore realizes cross-platform development and expands application scenarios.

Design patterns in C#.NET include Singleton patterns and dependency injection. 1.Singleton mode ensures that there is only one instance of the class, which is suitable for scenarios where global access points are required, but attention should be paid to thread safety and abuse issues. 2. Dependency injection improves code flexibility and testability by injecting dependencies. It is often used for constructor injection, but it is necessary to avoid excessive use to increase complexity.

C#.NET is widely used in the modern world in the fields of game development, financial services, the Internet of Things and cloud computing. 1) In game development, use C# to program through the Unity engine. 2) In the field of financial services, C#.NET is used to develop high-performance trading systems and data analysis tools. 3) In terms of IoT and cloud computing, C#.NET provides support through Azure services to develop device control logic and data processing.

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
