Teaches you how to use C # To create a bar Code (barcode) image encoded with Code 39 for ASP. NET + Crystal Reports to present and print this bar Code. This post provides sample download for ASP. NET 3.5.
-------------------------------------------------
Example download point of this post:
Http://files.cnblogs.com/WizardWu/100914.zip
In this example, the SQL Server's Northwind database, VS 2008 or IIS, and Crystal Reports 2008 Standard Edition are required. (You can download the complete installation program from the SAP website without restrictions, but before installation, enter the installation number ).
For the free and easy version of Crystal Reports built in VS 2005/2008, this post is not applicable because it does not have the "dynamic screenshot of network images" function and cannot capture existing Bar Code images.
---------------------------------------------------
A few days ago, the ASP. NET project used the Crystal Reports Crystal report. The report must be able to display and print the bar code in the browser. Originally, I used "font" to generate a bar code (a database field is directly converted into a bar code function in the crystal report), but later I found this practice, during deployment, a specific bar code font, such as free3of9 (which can be downloaded for free), must be installed on the Windows of each client to display and print the bar code correctly in the client browser. Therefore, this method was later abandoned and the "image" method was used to generate a bar code.
First use C # And. NET plotting API, combined with the most common Code 39 encoding rules in one-dimensional bar Code, to write a bar Code image that can be created. ashx (HttpHandler) or. aspx, (this file is stored in the same ASP.. NET project, you do not have to publish it as a service ). Then insert an image in the Crystal Reports file, and use the "dynamic screenshot of network images" function provided by the basic version of the Crystal Report (this function is not available in the free version of Visual Studio ), capture the created barcode image and dynamically input parameters so that the bar code can be changed when the report is changed.
First, use the drawing API of C # And. NET to encode the Code 39, which is the most common one-dimensional bar Code. Then, write a component that can create a bar Code image. Refer to the download example of this post to directly execute Code39Handler. ashx, and manually enter the bar code parameters in the URL address bar of the browser for testing. The execution result and source code (such components usually require money) are as follows:
Figure 1 bar Code image generated by C # And. NET with Code 39 encoding rules
The following Code uses the C # And. NET plotting APIs to generate bar Code images with Code 39 encoding rules.
Code39Handler
<% @ WebHandler Language = "C #" Class = "Code39Handler" %>
Using System;
Using System. Web;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. Drawing. Text;
/// <Summary>
/// Use the. NET protocol Protocol API, with the Code 39, which is the most common criterion for the condition, to generate a condition.
/// </Summary>
Public class Code39Handler: IHttpHandler {
Public void ProcessRequest (HttpContext context ){
// Context. Response. ContentType = "text/plain ";
// Context. Response. Write ("Hello World ");
// Logic to retrieve the image file
// Context. Response. ContentType = "image/jpeg ";
// Context. Response. WriteFile ("MyImage01.jpg ");
String mycode = context. Request ["code"];
String;
String character;
// String = "*-% $ *"
String = "*" + mycode + "*"; // the feature of Code 39 is that the frontend and backend devices can recognize "Star (*)」, start and end
Int uniform cloth Height = 35;
Int limit cloth Limit = 0;
Int limit x = 0;
Int y = 20;
// Int limit = 0;
If (! String. IsNullOrEmpty (mycode ))
{
Deployment cloth character = string. Length * 13;
Bitmap BMP = new Bitmap (zookeeper, Zookeeper height, System. Drawing. Imaging. PixelFormat. Format32bppPArgb );
Graphics G = Graphics. FromImage (BMP );
G. TextRenderingHint = TextRenderingHint. AntiAlias;
G. Clear (Color. White );
Brush fill Brush 1 = new SolidBrush (Color. White );
G. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
G. FillRectangle (fl 1, 0, 0, fl cloth height, fl cloth height );
For (int I = 0; I <string. Length; I ++)
{
// Obtain the Code 39 rule
Character = this. genBarcode (string. Substring (I, 1). ToUpper ());
For (int j = 0; j <4; j ++)
{
If (character. Substring (j, 1). Equals ("0 "))
{
G. DrawLine (Pens. Black, limit x, 0, limit x, limit y );
}
Else
{
G. DrawLine (Pens. Black, limit x, 0, limit x, limit y );
G. DrawLine (Pens. Black, rows x + 1, 0, rows x + 1, rows y );
Required x + = 1;
}
Required x + = 1;
If (character. Substring (j + 5, 1). Equals ("0 "))
{
G. DrawLine (Pens. White, limit x, 0, limit x, limit y );
}
Else
{
G. DrawLine (Pens. White, limit x, 0, limit x, limit y );
G. DrawLine (Pens. White, California x + 1, 0, California x + 1, California y );
Required x + = 1;
}
Required x + = 1;
} // End of loop
If (character. Substring (4, 1). Equals ("0 "))
{
G. DrawLine (Pens. Black, limit x, 0, limit x, limit y );
}
Else
{
G. DrawLine (Pens. Black, limit x, 0, limit x, limit y );
G. DrawLine (Pens. Black, rows x + 1, 0, rows x + 1, rows y );
Required x + = 1;
}
Required x + = 2;
} // End of loop
Int x = 0;
Int addx = 13;
G. DrawString ("-", new Font ("Arial", 10, FontStyle. Italic), SystemBrushes. WindowText, new PointF (x, 20 ));
X + = addx;
For (int k = 0; k <mycode. Length; k ++)
{
G. DrawString (mycode. Substring (k, 1), new Font ("Arial", 10, FontStyle. Italic), SystemBrushes. WindowText, new PointF (x, 20 ));
X = x + addx;
}
G. DrawString ("-", new Font ("Arial", 10, FontStyle. Italic), SystemBrushes. WindowText, new PointF (x, 20 ));
BMP. Save (context. Response. OutputStream, ImageFormat. Jpeg );
G. Dispose ();
BMP. Dispose ();
}
Else
{
Deployment ratio = 100;
Bitmap BMP = new Bitmap (zookeeper, Zookeeper height, System. Drawing. Imaging. PixelFormat. Format32bppPArgb );
Graphics G = Graphics. FromImage (BMP );
G. TextRenderingHint = TextRenderingHint. AntiAlias;
G. Clear (Color. White );
// Prompt content not displayed for Batch Data
G. DrawString ("unlimited production", new Font ("", 12, FontStyle. Regular), SystemBrushes. WindowText, new PointF (0, 20 ));
BMP. Save (context. Response. OutputStream, ImageFormat. Jpeg );
G. Dispose ();
BMP. Dispose ();
}
}
// Rules can be further tested site 1: http://blog.csdn.net/xuzhongxuan/archive/2008/05/28/2489358.aspx
// Rules can be taken into account site 2: http://blog.163.com/zryou/blog/static/6903184200971704226450/
/// <Summary>
/// Code 39 rules.
/// Code 39 characters can use the following characters: 0 ~ 9. ~ Z, +,-, *,/, %, $,., and white space characters.
/// </Summary>
/// <Param name = "code"> </param>
/// <Returns> </returns>
Public string genBarcode (string code)
{
Switch (code)
{
Case "0 ":
Code = "001100100 ";
Break;
Case "1 ":
Code = "100010100 ";
Break;
Case "2 ":
Code = "010010100 ";
Break;
Case "3 ":
Code = "110000100 ";
Break;
Case "4 ":
Code = "001010100 ";
Break;
Case "5 ":
Code = "101000100 ";
Break;
Case "6 ":
Code = "011000100 ";
Break;
Case "7 ":
Code = "000110100 ";
Break;
Case "8 ":
Code = "100100100 ";
Break;
Case "9 ":
Code = "010100100 ";
Break;
Case "":
Code = "100010010 ";
Break;
Case "B ":
Code = "010010010 ";
Break;
Case "C ":
Code = "110000010 ";
Break;
Case "D ":
Code = "001010010 ";
Break;
Case "E ":
Code = "101000010 ";
Break;
Case "F ":
Code = "011000010 ";
Break;
Case "G ":
Code = "000110010 ";
Break;
Case "H ":
Code = "100100010 ";
Break;
Case "I ":
Code = "010100010 ";
Break;
Case "J ":
Code = "001100010 ";
Break;
Case "K ":
Code = "100010001 ";
Break;
Case "L ":
Code = "010010001 ";
Break;
Case "M ":
Code = "110000001 ";
Break;
Case "N ":
Code = "001010001 ";
Break;
Case "O ":
Code = "101000001 ";
Break;
Case "P ":
Code = "011000001 ";
Break;
Case "Q ":
Code = "000110001 ";
Break;
Case "R ":
Code = "100100001 ";
Break;
Case "S ":
Code = "010100001 ";
Break;
Case "T ":
Code = "001100001 ";
Break;
Case "U ":
Code = "100011000 ";
Break;
Case "V ":
Code = "010011000 ";
Break;
Case "W ":
Code = "110001000 ";
Break;
Case "X ":
Code = "001011000 ";
Break;
Case "Y ":
Code = "101001000 ";
Break;
Case "Z ":
Code = "011001000 ";
Break;
Case "*":
Code = "001101000 ";
Break;
Case "-":
Code = "000111000"; // it seems that it cannot be identified
Break;
Case "% ":
Code = "100101000"; // it seems that it cannot be identified
Break;
Case "$ ":
Code = "010101000"; // it seems that it cannot be identified
Break;
Default:
Code = "010101000"; // if none of them are printed, $
Break;
}
Return code;
}
Public bool IsReusable {
Get {
Return false;
}
}
}
Execute the Crystal Report in Default. aspx. The result is 2. The bar code is an image, not a font. You don't have to worry about the failure of the browser or printer on the client to recognize a bar code font. In the other 2, The EmployeeID field of the Employees table is of the int type. In the Crystal Report, it is treated as the Number type by default, and the decimal point and the second two digits are automatically displayed. The solution will be mentioned later in this article.
Figure 2 when the report is changed, different parameter content will be passed into the bar code component we wrote, so the bar code content will also change
If you want to test this post example, go to the official SAP website and download the Crystal Reports 2008 software for the Standard Edition (download the SP or V1 software marked on the page, indicates that you have installed the main program + patch, not only the patch ). The software is the same as the policy of the Oracle database. It provides the network to download the complete installation of the main program, no time limit or functional restrictions, however, before installing Crystal Reports, you need to enter the installation sequence number (how to find the sequence number is not described in this article ). For example, 3. Select ADO for "data access.. NET function to work with ASP. NET Report method, through the App_Code folder of the website, define the content of the database to be accessed in advance. xsd (DataSet) file, used as the data source when designing the Crystal Reports report.
Figure 3. Add the "Data Access" ADO. NET option to the installation process of Crystal Reports 2008
For example, in Figure 4 and figure 5, insert an image in the newly created crystal report file and right-click the image and select "set image format 」, select the "graphic location" feature available in the Crystal Reports Standard Edition.
Figure 4 insert an image in the Crystal Report
Figure 5 "graphic location" feature available in the Crystal Reports Standard Edition, not available in the built-in version of VS 2005/2008
For example 6, in the crystal report 'S "Public Editor", enter the following content and parameters. The parameters dynamically passed in here will be passed in 1. We will use the bar code generated component written in C # in advance. In this example, the parameter content is the employee ID field of the Employees table. If the bar code in your report cannot be correctly presented through the browser, most of them are the address, port number, or content error, or the service that creates the bar code is not enabled correctly, in this case, the browser only displays the picture of the original inserted report, not the bar code.
Figure 6 syntax in the Public Editor, similar to VB or VB. NET
In addition, in 2, we mentioned the database's EmployeeID field, whose type is int. It will be treated as Number in the Crystal Report, and the Number of decimal points and double digits will be automatically added to the bar code. Solution 7: Use the CStr function provided by the crystal report to convert the field into a string.
Figure 7 if the bar code in the report cannot be correctly presented through the browser, most of the errors are caused by incorrect addresses or content, or it is not changed to the correct address and port number when you run the VS 2008 built-in Web server or IIS.
Note that when you deploy an ASP. NET Crystal Report to IIS, you must set the default IIS directory:
C: \ Inetpub \ wwwroot \
Copy the aspnet_client folder and the file (figure 8) to the ASP. NET website (Figure 9). In this way, only Crystal Reports executed by IIS can correctly display the icons in the Toolbar of the report and correctly display related functions.
Figure 8 after the Crystal Reports Main program is installed, the content of this folder is automatically added.
Figure 9 when the ASP. NET Website is deployed to IIS, The aspnet_client folder must be copied to the root directory of the website.
Note that there are two print methods for a crystal report: one is to use the print button that comes with the report Toolbar (refer to 2), and the other is to write your own code to call the PrintToPrinter method of the ReportDocument class.
In the first print mode, the browser's security restrictions can be broken. The ActiveX program is automatically downloaded to display the print preview form and the printer name on the client can be automatically captured. This method is suitable for users who print data across the Internet.
In the second printing mode, although the customization capability is strong, only the printer names on the server can be captured. Therefore, reports can only be printed on the server. This method is only applicable to users who share printers on the same LAN or Intranet.
Http://www.cnblogs.com/WizardWu/archive/2010/09/14/1825523.html reprinted