With the desire to produce a case

Source: Internet
Author: User
Tags command line
If there is no external component support, there are some things that ASPs cannot do, that is, the motion diagram-whether it's a graph, a horizontal ad, or a simple graphics calculator. Fortunately, this has been changed in the ASP.net-using the built-in method, the picture can be moved and can be delivered to the client side with the maximum set capacity, and it is easy to do.

The original code used in this article must install the Microsoft. NET Framework SDKin Webserver. I also pretend that the reader has a certain degree of knowledge of the C # program.

Production diagram

Having not yet felt the asp.net, I made a tedious, simple command-line program and used this source code as the basis for our ASP.net script, "he added." The difference is that the command line saves the picture as a file, and ASP.net script sends him to the client side.

Now, what do our example programs do? As is commonly seen, we start with the "Hello World" program, which is generally liked the text will be output as a file, and the case will produce the same size "Hello world" text according to the currently selected font and font size (therefore, it is impossible to produce a large image)

The following script (Pagecounter.cs) is a typical simple instruction stroke: ignoring the class that wraps around the perimeter, only the function Main is called, which is the program in which we produce the diagram.

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 (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 does this program do? In any case, the results of the test.png will be stored drive C:

How are the pictures produced? To understand why, we have to look at the original code in detail. First, the size of the case must be the size of the font "Hello World" to be presented, so, I will first calculate the size of the text, and at the same time, I use a copy size 1 x 1, and when I'm done, I grab the picture and produce an appropriate size case.

The interesting thing about the source code is the Graphics object. When I want to produce images, why do I need this object? The reason is that this is the picture I want to draw (the context)-I can use the picture situation in the screen, the printer, and the memory-it's Bitmap. The picture scenario allows me to perform a drawing operation (both virtual) at any of the settings.

Using drawstring, I can now type "Hello world" with the square specs of a white background (using fillrectangle ). The picture is complete and I have to save it to disk. It was a hard thing to do using GDI + (Graphics Device Interface) When you used to design the file format--We just use a simple command:

Newbitmap.save ("C:\\test.png", imageformat.png);

This is it! As long as you convert imageformat.png into imageformat.jpeg, you can have JPEG files. Simple use of the picture, that's what we've always wanted.

Now there is only one exception to be explained: Some functions can cause exceptions (for example, without enough memory to produce images). Good programmers have to be able to clear themselves, I have to deal with Graphics and Bitmap -and that's what I do in the finally block (because he always gets called). and finally , the program ends.

In theory, this program works, but in the original code, to actually perform it, you have to first edit:

Csc/r:system.dll/r:system.drawing.dll Pagecounter.cs

So we can produce one. EXE file pagecounter.exe. Note: This file will not execute until the system is installed with the Microsoft. NET Framework.

Now work on the Web server

When it comes to being a command line, it's a great way to do it, but if you're asp.net script you have to use some tips:

    • Optional text (for example, a calculator)
    • Optional text color
    • Optional background color
    • Optional font
    • The font size that you can choose

If someone feels this is a bit difficult, you can first take a look at the original code of the ASP.net script file (pagecounter.aspx) in this case. All I have to do is add some error processing code to check the pass-through parameters. This can be said to be the largest part of the change.

The other thing you have to do is send the picture to the client side rather than write it into a file. This new section 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'm just putting the picture in the memory zone, and I'm sending it to this familiar function BinaryWrite is a bit of a group: I need this function clearcontent, because in the top part of this Script there is The Import instruction sends out blank columns to the client side, making the PNG file ineffective.

If you have a look at the source code, you will notice that I have sent all the optional parameters as QueryString parameters. The parameters may be too long, so to my lazy, I built a very easy to see form, so I could test different values, so I could test each of them.


This picture was originally written in German, and I tested the text into Chinese on my own machine, so the download of the original file was in German and you had to change it to Chinese characters.

The great thing about this asp.net page (pagecountertest.aspx) is that I can get a picture on the same website. The original code for this form already contains many server-side asp.net control items (controls). This means that the open stomach dish for the upcoming article will be detailed in the process and the experience of the form in the asp.net structure.

Concludes

In this article we look at some of the features of the program with flying speed. For our website plan, the ASP.net is now able to provide Web page programmers with complete usage of the Windows diagram program. Now we can throw the word "can't" behind the brain.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.