Pattern (.NET) as you want to produce

xiaoxiao2021-03-06  38

Christoph Wille

TRANSLATED by:

Jacky chen

First Published: 2000/07/28

If there is no external component support, some things are ASP unable to do, that is, dynamic generating patterns - whether it is chart, banner, or a simple graphic counter. Fortunately, this has changed in ASP.NET - using built-in methods, pattern can be generated and can transmit to the client end with maximum configuration setting capability, and it is easy to do.

The original program code using this article must install Microsoft .NET Framework SDK in Web Server. At the same time, I also assume that the reader has a certain degree of understanding of the C # program.

Generate pattern

Under ASP.NET, I didn't feel the huge pressure on ASP.NET, I made a more boring simple instruction trip, then use this original program code as the basis of our ASP.NET Script. The difference is that this instruction line will store the pattern as a file, and the ASP.NET Script sent him to the Client side.

Now, what is our example? Just like a general common, we started using the "Hello World" program that usually like to use, the text will output into a pattern file, then the pattern will generate the same size "Hello World" based on the originally selected font selected and the character size. "Text (therefore, it is not possible to calculate a big image)

The following script (pageCounter.cs) is a typical simplicity instruction stroke: ignoring the wrapped Class, only the letter main execution will be called, which is the program where we generate the pattern.

Using system;

Using system.io;

Using system.drawing;

Using system.drawing.image;

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 ();

Bamboo

 Newbitmap = New Bitmap (nwidth, nheight, pixelformat.format32bppargb);

 g = graphics.fromImage (newbitmap);

 g.fillRectangle (New Solidbrush (Color.White),

 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 ();

}

}

}

What did this program do? Anyway, the results pattern Test.png will be stored in Drive C:

How is the pattern generated? To understand the reason, we must take a look at the original code in detail. First, the pattern size must be the same as the word "Hello World" to be presented, so I will first calculate the text size, at the same time, I use a Size 1 x 1 imitation pattern, when I calculate it, I grasp the pattern then generate a proper size pattern.

The interesting point in the original code is the Graphics object. What do I need to generate images? The reason is that this is the pattern of the pattern I want to draw - I can use the pattern situation in the screen, the printer, and the memory. - Correctly is Bitmap. The pattern context allows me to perform drawing operations in any device (both time is virtual).

With DrawString, I can now output text "Hello World" based on a white background (using FillRectangle). The pattern is complete, I must save it to the disk. I have designed the pattern file format knowing that this is a difficult thing, using GRAPHICS Device Interface is not the case - we just use a simple command:

Newbitmap.save ("c: //test.png", imageformat.png);

Just! Just swap imageformat.png into imageformat.jpeg, you can have JPEG files. Simple use of patterns, this is what we have always wanted.

Now there is an exception to process it to be explained: Some functions will cause exceptions (for example, there is not enough memory to generate images). A good programmer must be able to clear itself, I have to deal with the release of Graphics and Bitmap - this is what I did in the Finally block (because he will always be called). The time after Finally is completed.

In theory, this program can work, but only in the original code, let it actually execute, must first compile:

CSC /R :system.dll /r:system.drawing.dll PageCounter.cs

This way we can generate a .exe file PageCounter.exe. Note: This archive can only be executed after installing Microsoft .NET Framework!

Now WEB Server work

When the instruction line application is active, if it is necessary as an ASP.NET Script, some tips must be used:

Bamboo

Alternative text (eg, counter)  Oblective text color  Optional background color  Optional font  Optional font size

If someone feels this is a bit difficult, you can first look at the original code of this pattern ASP.NET Script file (PageCounter.aspx). What I have to do is to add some error handling code to check the transmitted verification parameters. This can be said to be the largest part of the need to change. In addition, it must be done to send the pattern to the Client side, not to write it into a file. This new part is as follows:

MemoryStream TempStream = New MemoryStream ();

Newbitmap.save (TempStream, Imageformat.png);

Response.clearContent ();

Response.contentType = "image / png";

Response.binaryWrite (TempStream.toArray ());

Response.end ();

I just put the pattern into the memory buffer, then transferred to this familiar Funwriting BinaryWrite is a bit set, at the same time: I need this letter ClearContent, because in the top of this Script, IMPORT instructions will send blank columns to The Client end makes the PNG icon invalid.

If you have a closer look at the original code, you will notice that I have sent all the optional parameters as the querystring parameter. Such parameters may be too long, so lazy to me, I constructed a list of comforts that look comfortably, so I can test a variety of different values.

[Note] The original text of this pattern is German. I tested the text into Chinese on my machine. Therefore, downloading the original file is using German, you must change itself into the Chinese word.

This ASP.NET PAGE (PageCounterTest.aspx) is better that I can get patterns in the same page. The original code of this Form already contains a number of ASP.NET controls (Controls) of the Server end. This means that it can be used as an appetizer in the future articles, and will be detailed in the ASP.NET architecture for Form's processing and verification.

in conclusion

In this article we look at some of the characteristics of the pattern. For our website plan, the ASP.NET architecture can now provide a complete use of the WINDOWS pattern design in the ASP.NET architecture. Now we can throw the brain "Don't do it".

Download process

Press here to start downloading.

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

New Post(0)