I can dynamically create a barcode using sample tutorials.
This block of code is in a loop and generates a number of images determined by user input. The image is in a place holder and is displayed on the same webpage.
I want the user to be able to have all the images on a separate page or file for them to print, but I'm not quite sure the best approach to do that. I tried placing it in a session, but I only get 1 image as opposed to the amount the user entered.
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 27, 100))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 16);
PointF point = new PointF(2f, .2f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
}
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
bitMap.Save(ms, ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
plBarCode.Controls.Add(imgBarCode);
}
Related
I have this code for generating barcode when page is loaded.(code below) txtBarcodeBlack.Text content is pulled from SQL Server Database as varchar.
I have a button submit that will do another process like saving the txtBarcodeBlack.text to database. My problem is when I press the button, it gives me this Problem please help. I'm stuck for 2 days.
private void loadBlackBarcode() {
//barcode black
//txtBarcodeBlack.Text = vBarcodeBlack;
string barCodeBlack = txtBarcodeBlack.Text;
System.Web.UI.WebControls.Image imgBarCodeBlack = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCodeBlack.Length * 40, 80))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 16);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + barCodeBlack + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream(100000))
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCodeBlack.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
PlaceHolder1.Controls.Add(imgBarCodeBlack);
}
}
<div class="col-xs-12 col-sm-12 col-lg-12" style="margin-bottom:20px;">
<h3>Digital Black</h3>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
UPDATE
Okay I tried searching for other solution, and I found the answer.
I changed this line:
using (Bitmap bitMap = new Bitmap(barCodeBlack.Length * 40, 80))
to this line:
using (Bitmap bitMap = new Bitmap(800, 80))
for some reason the new Bitmap does not accept variable inside the parameter, so I guess I need to hard code my measurement of the barcode.
I have a webapp that generates a barcode:
protected void btnGenerate_Click(object sender, EventArgs e)
{
string barCode = Barcode + txtCode.Text;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 50, 90))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 18);
PointF point = new PointF(3f, 3f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString(barCode, oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
plBarCode.Controls.Add(imgBarCode);
}
It generates the barcodes on the same page, what I was hoping to do, is once the button is pressed it displays the results in a new webpage.
I created a session
Session.Add("BarCodes", plBarCode);
Response.Redirect("barcodes.aspx);
And then on the barcordes.aspx pages"
protected void Page_Load(object sender, EventArgs e)
{
plbcode = (PlaceHolder)Session["Barcodes"];
}
That is incorrect since nothing shows up, but I don't know what else to try. I know I cannot use imgBarCode since it will be throw an exception of an Invalid Cast.
Keeping WebControl in Session is not a good idea. Better way is to move barcode image generation code to second page and pass all necessary params via querystring:
imgBarCode.ImageUrl = "barcodes.aspx?text=foobar&barcode=12345...";
Second page generates an image and writes it directly to Output stream, with image/png content type, without any html markup. Only image data.
i have an asp.net website, and i want to print some barcodes in c#. i use "IDAutomationHC39M" font as bellow:
public static void PrintSmallBarcode(HtmlGenericControl divBarCode, string barCode)
{
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(75, 18))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 7);
PointF point = new PointF(0, 0);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString(barCode, oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
imgBarCode.CssClass = "cssbarcode";
}
}
div.Controls.Add(imgBarCode);
}
it works, but the quality of barcode is sow low, and my barcode reader device failed to read it. i can not increase the size of image, and of course i see a lot of good barcodes smaller than my size. i replace "IDAutomationHC39M" font with "free3of9.ttf", but this font just draw the number and no barcode line!.
how can i get a better barcode?
This is a long-standing problem with barcode fonts; they are enormous and scan horribly because printers try to anti-alias the bars.
Use images, not fonts, to draw barcodes. There are many packages available, for example Barcode Rendering Framework.
You can use other datas to change the size of the barcode.I used keepautomation barcode generator for asp.net to create barcode images in Visual C# ASP.NET project.After my settings,i can preview the size of the barcode.
i am using this code in my asp.net website. its barcode generation code.. problem this code is depend on (IDAutomationHC39M) this font. so on localhost i have installed this font in my fonts folder and code is working successfully in local. but i do not know how to install this on server
string barCode = Request.QueryString["id"].ToString();
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 16);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
plBarCode.Controls.Add(imgBarCode);
}
Assuming a web based font will not work (ie you need to render your barcode server side and possibly embed it with other graphics / images), you can take the following approach. I didn't write this code but have used it and it does work.
You will need to load your font from a resource or possibly via a URL. Then pass the bits to the following. If loading from a resource, google that as there are a fair number of examples.
You can also use fontCollection.AddFontFile() and simplify your code but that will require access to the local (on server) filesystem.
public FontFamily GetFontFamily(byte[] bytes)
{
FontFamily fontFamily = null;
var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try
{
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(bytes, 0);
var fontCollection = new PrivateFontCollection();
fontCollection.AddMemoryFont(ptr, bytes.Length);
fontFamily = fontCollection.Families[0];
}
finally
{
// don't forget to unpin the array!
handle.Free();
}
return fontFamily;
}
if you are developing for relatively modern browsers you can read up on #font-face
or, it may be just a simple matter of installing the font on your web server, typically you just need to copy the font file over to some folder on the server, say the desktop, and it should just be a simple matter of right clicking and selecting "install font"
I created a function to allow uploaded transparent .png files to be inserted into a SQL Server database and the displayed on a web page via an HttpHandler.
While this all works, the png transparency changes to black when it's viewed on the web page. Is there a way of preserving the transparency?
Here's my image service which inserts into the database from the MVC controller:
public void AddImage(int productId, string caption, byte[] bytesOriginal)
{
string jpgpattern = ".jpg|.JPG";
string pngpattern = ".png|.PNG";
string pattern = jpgpattern;
ImageFormat imgFormat = ImageFormat.Jpeg;
if (caption.ToLower().EndsWith(".png"))
{
imgFormat = ImageFormat.Png;
pattern = pngpattern;
}
ProductImage productImage = new ProductImage();
productImage.ProductId = productId;
productImage.BytesOriginal = bytesOriginal;
productImage.BytesFull = Helpers.ResizeImageFile(bytesOriginal, 600, imgFormat);
productImage.BytesPoster = Helpers.ResizeImageFile(bytesOriginal, 198, imgFormat);
productImage.BytesThumb = Helpers.ResizeImageFile(bytesOriginal, 100, imgFormat);
productImage.Caption = Common.RegexReplace(caption, pattern, "");
productImageDao.Insert(productImage);
}
And here's the "ResizeImageFile" helper function:
public static byte[] ResizeImageFile(byte[] imageFile, int targetSize, ImageFormat imageFormat)
{
using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
{
Size newSize = CalculateDimensions(oldImage.Size, targetSize);
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
{
using (Graphics canvas = Graphics.FromImage(newImage))
{
canvas.SmoothingMode = SmoothingMode.AntiAlias;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
MemoryStream m = new MemoryStream();
newImage.Save(m, imageFormat);
return m.GetBuffer();
}
}
}
}
What do I need to do to preserve the png transparency? Please show examples. I'm seriously not an expert with image manipulation.
Thanks.
Maybe try changing pixel format form PixelFormat.Format24bppRgb to PixelFormat.Format32bppRgb. You need the extra 8 bits to hold the alpha channel.
Using PixelFormat.Format32bppRgb didn't work for me. What worked however is using oldImage.PixelFormat when drawing the new image. So the corresponding line of code becomes:
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, oldImage.PixelFormat))