Create graphics information in ASP.NET

xiaoxiao2021-03-06  71

If there is no support for an external component, you cannot create graphics in the ASP, whether it is a chart, one banner or just a graphic counter. It is grateful that this changed in ASP.NET. Now, we only need to use the built-in function, you can easily create graphics and send a graphic with the best configuration to the client. Creating a graph with a command line program Before discussing a lot of ASP.NET code, let's perform a simple command line program to do a test, then use these source code as the foundation of the ASP.NET script. In fact, the difference between the two is that the command line program saves the graphics in a file, and the ASP.NET script sends the graphics directly to the client. What is the example of the program? According to the practice, we started from the well-known "Hello World" program, and the text information is output to a graphics file, the size of this graph is exactly the same as the font and font size of the currently selected "Hello World" text.

The following script PageCounter.cs is a typical simple command line program: If you ignore the necessary class code surrounded around it, only the main function to call when the program is running, which is also the code of generating graphics. at: using System; using System.IO; using System.Drawing; using System.Drawing.Imaging; public class CTestBitmapFunctionality {public static void Main () {Bitmap newBitmap = null; Graphics g = null; try {Font fontCounter = new Font ( "Lucida Sans Unicode", 12); // calculate size of the string newBitmap = new Bitmap (1,1, PixelFormat.Format32bppARGB);. g = Graphics.FromImage (newBitmap); SizeF stringSize = g.MeasureString ( "Hello world ", fontCounter); int nWidth = (int) stringSize.Width; int nHeight = (int) stringSize.Height; g.Dispose (); newBitmap.Dispose (); newBitmap = new Bitmap (nWidth, nHeight, PixelFormat.Format32bppARGB ); G = graphics.fromImage (new SolidBrush (New Solidbrush (New Solidbrush (New Solidbrush (New Solidbrush), New Rectangle (0,0, nwidth, nHEight); g.drawstring ("Hello World", Fontcounter, New Solidbrush ( Color.Black), 0, 0); Newbitmap.Save ("c: //test.png", imageformat.png); Catch (eXception e) {console.writeLine (E.TOString ());} finally {if (null! = g) g.dispose (); if (null! = newbitmap) newbitMap.dispose ();}}} In any case, after executing the above code, the following graphic Test.png will be generated, which will be stored on the C drive: Let's take a closer study of the source code, see how this graphic is created. The key is that the generated graphics must be the same as the fonts and fonts of the text "Hello World". Therefore, first, the size of the text is first calculated, and we use a virtual graphic with a size of 1 x 1. After the calculation, discard this virtual pattern and generate an appropriate size graphic. There is a little interesting in the source code, this is the Graphics object. What do you do when you create a bitmap? The mystery is that this is the context environment that can draw. We can use a graphic context environment on the screen, the printer and memory, accurately, is a bitmap. The graphic context environment allows us to draw a drawing on any device, or even on the virtual device.

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

New Post(0)