我们知道高质量的条码更能达到我们的目的,然而图像DPI与图像的大小成正比,即DPI越高,图片文件越大。 可能你将600DPI的图片显示在HTML上很容易,然而你会发现图片占满了整个页面,因为大多数的HTML显示分辨率为96DPI。那么今天将分享用Barcode Professional for ASP.NET在HTML支持的DPI范围内显示高清条码。代码仅供参考。 注意:我们使用的是Visual Studio 2005 (Visual Web Developer) ,不过有些VS.NET也可以使用。 步骤:
VB Dim dpi As Single = 96.0 Try dpi = Single.Parse(context.Request.QueryString("dpi")) Catch End Try Dim valueToEncode As String = context.Request.QueryString("valueToEncode") Dim bcp As New Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional() bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128 bcp.Code = valueToEncode bcp.BarWidth = 0.01F bcp.BarHeight = 0.5F context.Response.ContentType = "image/jpeg" context.Response.BinaryWrite(bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Jpeg, dpi)) C# float dpi = 96.0f; try { dpi = float.Parse(context.Request.QueryString["dpi"]); } catch { } string valueToEncode = context.Request.QueryString["valueToEncode"]; Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional bcp = new Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional(); bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128; bcp.Code = valueToEncode; bcp.BarWidth = 0.01f; bcp.BarHeight = 0.5f; context.Response.ContentType = "image/jpeg"; context.Response.BinaryWrite(bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Jpeg, dpi));
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml" > <head> <title>HTML Barcode image with DPI support</title> <script type="text/javascript"> var imageUrlGen = "BarcodeGen.ashx"; var imageID = "MyBarcodeImage"; var txtValueToEncodeID = "MyValueToEncode"; var tmpImage; var dpi = 600; function CheckForLoadedImage() { if(tmpImage.complete) { var img = document.getElementById(imageID); img.width = tmpImage.width * 96 / dpi; img.height = tmpImage.height * 96 / dpi; img.src = tmpImage.src; clearTimeout("CheckForLoadedImage()"); } else { setTimeout(“checkforloadedimage()”,500); } } function GetBarcodeImage() { var valueToEncode = document.getElementById(txtValueToEncodeID).value; tmpImage = new Image(); tmpImage.src = imageUrlGen + "?valueToEncode=" + valueToEncode + "&dpi=" + dpi; CheckForLoadedImage(); } </script> </head> <body> <p> <b>Barcode Professional in a HTML page with DPI support</b> </p> <p> <img id="MyBarcodeImage" alt="" src="" /> </p> <p> Enter a value to encode:<br /> <input id="MyValueToEncode" type="text" name="MyValueToEncode" /> </p> <p> <input id="Button1" type="button" value="Get Barcode at 600 DPI..." name="Button1" onclick="GetBarcodeImage()" /> </p> </body> </html> 完成,虽然图像的大小是经过JS代码生成的,但生成图像的质量是与打印结果一致。 |
|