GDI + Are you used?

zhaozj2021-02-16  50

You can reprint, copy, but you must join the author's signature Aweay. If you are used for commercial purposes, you must agree.

Written: aw Ready

GDI Plus (GDI ) has been launched for a long time. Many programming enthusiasts under VC have experienced GDI magical and powerful, but our BCBER seems to use this powerful graphics interface. Corresponding to GDI , GDI, if you use the traditional GDI API writing process, you must have a feeling of touching it, frequently select the brush, brush, then recover, remember to release them, otherwise you will have you Familiar GDI resource leakage.

If you are bcber, you may have no such feeling. In most cases, you are in Tcanvas to operate GDI, I have to admire Borland to GDI packages, use Tcanvas, you basically don't experience the pain, but for Many GDI can't complete features, using Tcanvas can't do it, then we have more choices of GDI .

GDI is Microsoft's new graphic development interface, GDI is very easy to use, we don't have to select it like GDI, restore GDI objects, because he is stateless. Most importantly, GDI provides a lot of powerful features that make you develop graphics software, such as Alpha padding, transition-filled, anti-aliasing, and so on. And GDI is very light, just a DLL, and he is free, you can post it as your software to any computer.

Is it a heart? So powerful why our BCBER is very small? The answer is that GDI is used in BCB or needs a certain skill. If you don't know, you will have a GDI program directly. You must get a lot of mistakes. This article tells everyone that these skills, I hope you can also experience the magic of GDI .

Install GDI

For BCB6 users do not need to install GDI , BCB6 itself has a header file with GDI . If you are a BCB5 user, you need to go to Microsoft War to download the GDI development kit. Here we assume that you are using BCB6.

First we need to generate a GDI link library, this work needs to use the IMPLIB command line tool, such as:

Implib gdiplus.lib gdiplus.dll

Where gdiplus.dll is Microsoft's dynamic link library, it is necessary to download from Microsoft (before downloading, you should check your computer, in many cases, you already have this DLL).

This way we have a GDIPLUS.LIB file.

A GDI program

Here you need some tips, otherwise your program cannot be compiled.

First, for a GDI project, you need to join the Strict Category compilation option in the compilation option, then join the LIB file you just generated in the project, and finally at the head of the code, join:

#include

Using std :: min;

Using std :: max;

At this point, we can use GDI , let's take a look at an example:

/ / -------------------------------------------------------------------------------------------- ---------------------------

#include

#pragma HDRSTOP

#include using std :: min;

Using std :: max;

#include "unit1.h"

/ / -------------------------------------------------------------------------------------------- ---------------------------

#pragma package (smart_init)

#pragma resource "* .dfm"

TFORM1 * FORM1;

Using namespace gdiplus;

/ / -------------------------------------------------------------------------------------------- ---------------------------

__fastcall tform1 :: tform1 (tComponent * Owner)

: TFORM (OWNER)

{

GDIPLUSTARTUP (& gdiplustoken, & gdiplusstartupinput, null); // Initializing GDI

// gdiplusStartupinput is a gdiplus :: gdiplusstartupinput object, declares in class header file

// gdiplustoken is ULONG_PTR to turn off GDI

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

__fastcall tform1 :: ~ tform1 (void)

{

GDIPLUSSHUTDOWN (GDIPLUSTOKEN); // Turn off GDI

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

Void __fastcall tform1 :: PaintBox1paint (TOBJECT * SENDER)

{

GDIPLUS :: Graphics G (canvas-> Handle); // Initialize using HDC

Pen Pen (gdiplus :: color (255, 0, 0, 0), 3); // opaque black

g.drawline (& Pen, 50, 50, 500, 50);

G. Drawpie (& Pen, 50, 50, 200, 200, 225, 90);

Solidbrush Sbrush (GDIPLUS :: Color (128, 255, 0, 0)); // translucent red

LineargradientBrush Gbrush (

GDIPLUS :: Point (50, 100),

GDIPLUS :: Point (250, 200),

GDIPLUS :: Color (255, 255, 0, 0),

GDIPLUS :: Color (128, 0, 0, 0) // Create a gradient filling brush

);

G. FillRectangle (& Gbrush, 50, 100, 200, 100);

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

The above code demonstrates how to use GDI , and simply use the GDI Alpha padding and gradient filling function, the code is easy to understand, this shows the GDI good package.

If you have a lot of Warning, it is normal if you hate something that makes you eye-catching, you can join:

#pragma warn -inl

#pragma Warn -8022

Performance analysis

The performance analysis method of a classic 2D graphic function is to randomly draw a number of rectangles. We also do this, in the following code, we use traditional GDI and GDI to generate 10,000 rectangles, compare two The performance of the graphical interface. / / -------------------------------------------------------------------------------------------- ---------------------------

#include

#pragma HDRSTOP

#include

#include

Using std :: min;

Using std :: max;

#include "unit1.h"

/ / -------------------------------------------------------------------------------------------- ---------------------------

#pragma package (smart_init)

#pragma resource "* .dfm"

TFORM1 * FORM1;

Using namespace gdiplus;

/ / -------------------------------------------------------------------------------------------- ---------------------------

__fastcall tform1 :: tform1 (tComponent * Owner)

: TFORM (OWNER)

{

GDIPLUSSTARTUP (& gdiplustoken, & gdiplusstartupinput, null);

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

__fastcall tform1 :: ~ tform1 (void)

{

GDIPLUSSHUTDOWN (GDIPLUSTOKEN);

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

Void __fastcall tform1 :: button1click (Tobject * Sender)

{

TCOLOR CL;

INT X, Y, W, H;

INT mx = PaintBox1-> width;

INT my = PaintBox1-> height;

Randomize ();

Long ST = gettickcount ();

For (int N = 0; n <= 10000; n )

{

CL = RGB (Randomrange (0,255), Randomrange (0,255), Randomrange (0,255));

PaintBox1-> canvas-> pen-> color = cl;

Paintbox1-> canvas-> brush-> style = bsclear;

x = randomrange (0, mx);

Y = randomrange (0, my);

W = randomrange (10, mx);

h = randomrange (10, my);

Paintbox1-> canvas-> Rectangle (X, Y, W, H);

}

ST = GetTickCount () - ST;

Caption = ST;

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

Void __fastcall tform1 :: button2click (Tobject * Sender)

{

GDIPLUS :: Color Cl;

INT X, Y, W, H;

GDIPLUS :: Graphics G (PaintBox1-> Canvas-> Handle);

INT mx = PaintBox1-> width;

INT my = PaintBox1-> height;

Randomize ();

Long ST = gettickcount ();

For (int N = 0; n <= 10000; n )

{

Cl = GDIPLUS :: Color (Randomrange (0,255), Randomrange (0,255), Randomrange (0,255));

Pen Pen (cl, 1);

x = randomrange (0, mx);

Y = randomrange (0, my);

W = randomrange (10, mx);

h = randomrange (10, my);

g.drawRectangle (& Pen, X, Y, W, H);

}

ST = GetTickCount () - ST;

Caption = ST;

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

On the author's computer, traditional GDI generates 10,000 rectangular times of 205 milliseconds, while GDI uses 4053 milliseconds, you can see GDI significantly more than GDI, the speed gap is 20 times, so slow speed we use? Let's do something change again, put the above code:

Cl = GDIPLUS :: Color (Randomrange (0,255), Randomrange (0,255), Randomrange (0,255));

Change to:

CL = Gdiplus :: Color (Randomrange (0,255), Randomrange (0,255), Randomrange (0,255));

It means that Alpha transparency is also random, we found that the performance of GDI 300 rectangles is not too big, and it is completed in 5065, but the traditional GDI is used to implement Alpha transparent to paint 1000 rectangles. 5065 It is done in the time, which shows that GDI is slower than GDI, but advanced applications are very fast, so you can improve your program performance in combination with 2 different interfaces. In addition, GDI application is not reflected, but his powerful function.

AntialiaSing

In the above code, we join the 3rd button, join the code:

Void __fastcall tform1 :: button3click (TOBJECT * Sender)

{

GDIPLUS :: Color Cl;

GDIPLUS :: Graphics G (PaintBox1-> Canvas-> Handle);

CL = Gdiplus :: Color (Randomrange (0,255), Randomrange (0,255), Randomrange (0,255));

Pen Pen (cl, 15);

g.setsmoothingmode (smoothingmodehighspeed); // High-speed, low quality

g.drawellipse (& Pen, 0, 0, 200, 100);

g.setsmoothingmode (smoothingmodehighquaalog); // high quality, low speed

G. Drawellipse (& Pen, 100, 0, 200, 100);

You will see the following graphics:

Can you see the ellipse of transparency, anti-aliasing, is it convenient?

Graphic rotation

The anti-aliasing and graphic rotation is more problematic in 9CBS, solving it with GDI is so easy, let's take a look at the graphic rotation:

Void __fastcall tform1 :: button4click (TOBJECT * Sender)

{

GDIPLUS :: Graphics G (PaintBox1-> Canvas-> Handle);

GDIPLUS :: Image Image (l "E: //siney_sm.jpg");

G. DrawImage (& image, 10, 10, image.getHEight ());

Image.Rotateflip (rotate90flipy); // Rotate 90 degrees

G. DrawImage (& image, 160, 10, image.getHEight ());

}

The result is as shown:

Conclude

From the above discussion, we can fully feel

GDI

Powerful power, and all of this is just

GDI

The iceberg is a corner of the iceberg, which provides a lot of functions that have greatly accelerated the development speed of graphics software, such as cropping.

Alpha

, Anti-aliasing, thumbnail

Wait. I encourage everyone to study.

GDI

. Some of the programmers don't like it because it is slow, but there are many techniques to improve its performance. Of course

GDI

Combine

GDI

To develop high-performance graphics, if you need more high performance, you can also combine

DX

If you are interested, you can try it.

转载请注明原文地址:https://www.9cbs.com/read-25668.html

New Post(0)