Crystal Report uses bar code and supports PDF export

Source: Internet
Author: User
C.r. Use a bar code,

1. Generate the barcode through the code39drawimagebord. CS class into a byte [] variable.

2. Save the byte [] variable to the image object in SQL Server.

3. Drag the image variable in the database to the Crystal Report.

OK done!

The barcode encoding rules in the format of code 39 are used here. To support other codes such as 128 encoding, you only need to add a new CS class and inherit idrawbarcode. CS.

Below isCode:

Statement of reference:

// Generate an image
Code39drawimagebord DR = new code39drawimagebord ("*" + shiporder. refnumber + "*");
Entity. barcodenumberimage = dr. getimagebytes ();

Base class:

Using system;
Using system. Collections. Generic;
Using system. IO;
Using system. LINQ;
Using system. text;
Using system. drawing;
Using system. Drawing. imaging;

Namespace shipfromherebusiness. Barcode
{
Public abstract class idrawbarcode
{
Protected virtual string bordrulename
{
Get {return string. Empty ;}
}

Protected virtual system. Collections. hashtable roles
{
Get {return new system. Collections. hashtable ();}
}

String drawstring;
Int width = 800; // The width of the canvas (computable)
Int Height = 36; // 1 CM
Int unitwidth = 1 ;//

Int currentlocation = 0;
Bool boolautolength = false;

Public idrawbarcode (string S, int intwidht, int intheight, int intstepwidth)
{
Drawstring = s;
Width = intwidht;
Height = intheight;
Unitwidth = intstepwidth;

}

Public idrawbarcode (string s)
{< br> drawstring = s;
boolautolength = true;
width = 30 + S. length * 18;
Height = 36;
unitwidth = 1;
}

Public Virtual byte [] getimagebytes ()
{
Bitmap Bm = new Bitmap (width, height );
Graphics G = graphics. fromimage (BM );
G. smoothingmode = system. Drawing. drawing2d. smoothingmode. default;
G. textrenderinghint = system. Drawing. Text. textrenderinghint. antialias;

// Canvas and edge settings
G. Clear (color. White );

G. drawrectangle (pens. white, 0, 0, width, height);
for (INT I = 0; I {< br> This. drawstring (drawstring [I]. tostring (), g);
}< br> return bitmap2bytes (BM);
}

Public Virtual void draw (system. Io. stream target)
{
Bitmap Bm = new Bitmap (width, height );
Graphics G = graphics. fromimage (BM );
G. smoothingmode = system. Drawing. drawing2d. smoothingmode. default;
G. textrenderinghint = system. Drawing. Text. textrenderinghint. antialias;

// Canvas and edge settings
G. Clear (color. White );

G. drawrectangle (pens. White, 0, 0, width, height );
For (INT I = 0; I <drawstring. length; I ++)
{
This. drawstring (drawstring [I]. tostring (), g );
}
BM. Save (target, imageformat. JPEG );
}

Private byte [] bitmap2bytes (Bitmap BM)
{
Memorystream STRM = new memorystream ();
BM. Save (STRM, imageformat. JPEG );
Return STRM. toarray ();

}
Protected virtual void drawstring (string S, graphics g)
{
System. Collections. hashtable hash = This. roles;
Object o = hash [s];
If (O = NULL) return;
Char [] chars = O. tostring (). tochararray ();
If (chars. length> 9) return;
Solidbrush blackbrush = new solidbrush (color. Black );
Solidbrush witebrush = new solidbrush (color. White );

For (INT I = 0; I <5; I ++)
{
// Draw the first 0 black bars
If (chars [I] = '0 ')
{
Rectangle RE1 = new rectangle (currentlocation, 0, unitwidth, height );
G. fillrectangle (blackbrush, RE1 );
Currentlocation + = unitwidth;
}
Else
{
Rectangle RE1 = new rectangle (currentlocation, 0, 3 * unitwidth, height );
G. fillrectangle (blackbrush, RE1 );
Currentlocation + = 3 * unitwidth;
}
// Draw 6th white stripes
If (I + 5) <9)
{
If (chars [I + 5] = '0 ')
{
Rectangle RE1 = new rectangle (currentlocation, 0, unitwidth, height );
G. fillrectangle (witebrush, RE1 );
Currentlocation + = unitwidth;
}
Else
{
Rectangle RE1 = new rectangle (currentlocation, 0, 3 * unitwidth, height );
G. fillrectangle (witebrush, RE1 );
Currentlocation + = 3 * unitwidth;
}
}
}
Rectangle re2 = new rectangle (currentlocation, 0, unitwidth, height );
G. fillrectangle (witebrush, re2 );
Currentlocation + = unitwidth;

}
}

}

Code 39 encoding rule class:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace shipfromherebusiness. Barcode
{
/**/
/**/
/**/
/// <Summary>
/// Code39drawimagebord abstract description
/// </Summary>
Public class code39drawimagebord: idrawbarcode
{
Private system. Collections. hashtable hash = new system. Collections. hashtable ();

Public code39drawimagebord (string S, int intwidht, int intheight, int intstepwidth): Base (S, intwidht, intheight, intstepwidth)
{
}

Public code39drawimagebord (string S)
: Base (s)
{
}

Protected override string bordrulename
{
Get {return "code39 ";}
}

Protected override system. Collections. hashtable roles
{
Get
{
If (hash. Count> 0) return hash;
Hash. Add ("0", "001100100 ");
Hash. Add ("1", "100010100 ");
Hash. Add ("2", "010010100 ");
Hash. Add ("3", "110000100 ");
Hash. Add ("4", "001010100 ");
Hash. Add ("5", "101000100 ");
Hashing. Add ("6", "011000100 ");
Hash. Add ("7", "000110100 ");

hash. add ("8", "100100100");
hash. add ("9", "010100100");
hash. add ("A", "100010010");
hash. add ("B", "010010010");
hash. add ("C", "110000010");
hash. add ("D", "001010010");
hash. add ("e", "101000010");

hash. add ("F", "011000010");
hash. add ("g", "000110010");
hash. add ("H", "100100010");
hash. add ("I", "010100010");
hash. add ("J", "001100010");
hash. add ("k", "100010001");
hash. add ("L", "010010001");

hash. add ("M", "110000001");
hash. add ("N", "001010001");
hash. add ("O", "101000001");
hash. add ("P", "011000001");
hash. add ("Q", "000110001");
hash. add ("r", "100100001");
hash. add ("S", "010100001");

hash. add ("T", "001100001");
hash. add ("u", "100011000");
hash. add ("V", "010011000");
hash. add ("W", "110001000");
hash. add ("X", "001011000");
hash. add ("Y", "101001000");
hash. add ("Z", "011001000");

Hash. Add ("-", "000111000 ");
Hash. Add ("%", "100101000 ");
Hash. Add ("$", "010101000 ");
Hash. Add ("*", "001101000 ");

Return hash;
}
}
}

}

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.