Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphics in C# (.NET)

I use this code for drawing text in a panel:

Graphics g = panel1.CreateGraphics();
g.DrawString(...);

So I want to know what size the input text will be when rendered in the panel.

like image 970
RealBoy_Ba Avatar asked Mar 08 '10 19:03

RealBoy_Ba


People also ask

What is the use of graphics in programming?

Computer graphics deals with creation, manipulation and storage of different type of images and objects. Some of the applications of computer graphics are: Computer Art: Using computer graphics we can create fine and commercial art which include animation packages, paint packages.

Is C++ used for graphics?

C and C++ languages are the most interesting because they are commonly the go-to language for graphics rendering. They're versatile, so, you may use C++ if you want to create low-end graphics too. Thus, creating basic shapes and words with stylish fonts, such as adding colors to them, may also be done using C++.

What is Initgraph () What are its parameters?

initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode.


2 Answers

Use g.MeasureString() to get the width of a string in the grapic context.

// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);

// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);
like image 96
AxelEckenberger Avatar answered Sep 28 '22 20:09

AxelEckenberger


You can also use TextRenderer.MeasureText which is sometimes easier to use than MeasureString.

like image 24
Nick Avatar answered Sep 28 '22 22:09

Nick