I'm creating a web app that generates barcodes that uses information that's databound. That information is a name from a database.
The barcode generates correctly, but I want to add text to it.
Here is my code for the barcode generator.
protected void btnGenerate_Click(object sender, EventArgs e)
{
foreach (ListItem item in BarCode.Items)
{
if (item.Selected)
{
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);
}
}
}
}
I want to add
"QTY:_____________"
underneath the barcode when it's generated. Although I believe the formatting of the barcode is limited to the code and I don't believe I can create a string literal
string lit = "QTY:_______________";
and add it to the barcode string:
string barCode = Barcode + txtCode.Text + Environement.NewLine + lit;
Is it possible to add that underneath programmatically?
If I understand you right You don't need to add text into code, you need to print some text under bar code. Just add into:
using (Graphics graphics
somthig like this:
// put coordinates here:
RectangleF rectf = new RectangleF(70, 90, 90, 50);
graphics.DrawString(lit , new Font("Tahoma",8), Brushes.Black, rectf);
Related
I have two columns in my database(Column 1: text ,column 2: image). Now I need to Display image, and on that image I need to display column1's text, Finally the Result should be like this image
enter image description here
Here Healthy foods is my column1's text and background is column2's image.
I am using C# and Winforms.
After bringing your data from database to datatable after
byte[] fileContents = (byte[])dts.Rows[0]["image"];
var memoryStream = new MemoryStream(fileContents);
var biId = new Bitmap(memoryStream);
Bitmap bitMapImage = new System.Drawing.Bitmap(biId);
Graphics graphicImage = Graphics.FromImage(bitMapImage);
graphicImage.SmoothingMode = SmoothingMode.HighQuality;
SolidBrush blueBrush = new SolidBrush(Color.Blue);
SolidBrush redBrush = new SolidBrush(Color.Red);
SolidBrush greenBrush = new SolidBrush(Color.Green);
RectangleF yourtextmessage= new RectangleF(285, 20, 250, 250);
graphicImage.DrawString(dts.Rows[0]["text1"].ToString() + " ,", new Font("Verdana", 12, FontStyle.Italic), Brushes.White, yourtextmessage);
context.Response.ContentType = "image/jpeg";
bitMapImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
graphicImage.Dispose();
bitMapImage.Dispose();
I have generated dynamic barcode image in asp.net web application but its not getting scanned/reading through barcode scanner.
Below is the code where i have generated barcode image.kindly find it out.
Need Your Suggestion and help,awaiting your response.
string barCode = LblSerialNo.Text;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 11, 45))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 8);
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);
}
I want to save the image created from graphics class how to do it?
(if i can get the imagestream then i can do it using below code)
var bmp = Bitmap.FromStream(originalStream);
// var newImage = new Bitmap(bmp.Width, bmp.Height);
var img = new Bitmap(bmp,
(int)(bmp.Size.Width / zoomLevel),
(int)(bmp.Size.Height / zoomLevel));
var g = Graphics.FromImage(img);
g.DrawImageUnscaled(bmp, (int)CurrentX, (int)CurrentY);
g.DrawString(Text, SystemFonts.DefaultFont, Brushes.Black, (float)CurrentTextX, (float)CurrentTextY);
string originalImagePath = string.Concat(#"Photos/", string.Format("{0:yyyy-MM-dd}", ApplicationDateTime.Today) + "/" + Session[Keys.UserId], "/", url.Split('/').Last());
var originalImageamazonResponse = _s3AmazonServices.StoreImage(originalStream, originalImagePath, Configuration.ConfigurationNew.Current.S3Bucket, Configuration.ConfigurationNew.Current.S3AmazonAccessKey, Configuration.ConfigurationNew.Current.S3AmazonSecretKey);
I believe that img gets modified with everything you did to the g that got created from it. So you should be able to call img.Save().
I am trying for create a barcode in my web form.for that i download font IDAutomationHC39M and install in my system,then i run my wesite in localhost but barcode cannot be generated.This is my code
protected void Button1_Click1(object sender, EventArgs e)
{
string barCode = TextBox1.Text;
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);
}
And the result is appear like that code
I need to print a barcode from client Machine, While running the program in Visual Studio, the barcode prints perfectly but when the code is published & put into the IIS server, the barcode is not printing on the client machine but rather prints on the server side.
Even if I try to print from client machine it does not work. Once the barcode printer machine connects with the server it prints all the barcode given on client machine as well as on the server. The printer is dedicated to barcode printing only and is connected to the server and not the client.
"The barcode MUST print on the CLIENT MACHINE"
MY CODE:
protected void btnPrint_Click(object sender, EventArgs e)
{
List<string> list = new List<string>();
foreach (String printer in PrinterSettings.InstalledPrinters)
{
list.Add(printer.ToString());
}
string printerName = "";
for (int i = 0; i < list.Count; i++) // Loop through List with for
{
printerName = list[i].ToString().ToLower();
if (printerName.Equals(#"tsc ttp-244 plus"))
{
// Console.WriteLine("Printer = " + printer["Name"]);
if (list[i].ToString().ToLower().Equals("true"))
{
// printer is offline by user
//lblMsg.Text = "Your Plug-N-Play printer is not connected.";
}
else
{
// printer is not offline
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.PrinterSettings.PrinterName = "TSC TTP-244 Plus";
pd.DefaultPageSettings.PaperSize = new PaperSize("Label", 197, 98);//Document No Size
// pd.DefaultPageSettings.Margins = new Margins(70, 12, 12, 12);
// pd.DefaultPageSettings.Margins.Left = 100;
pd.Print();
}
}
}
PlBarcodeFileNo.Controls.Add(_BarcodeImageView(lblBarcode.Text));
}
private System.Web.UI.WebControls.Image _BarcodeImageView(string barCodeimgNo)
{
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCodeimgNo.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("*" + barCodeimgNo + "*", 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);
}
}
return imgBarCode;
}