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
Related
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);
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 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;
}
I am using the below code to create bitmap. Image is getting created but not with the specified size. Is there any issue with code?
public class ProcessImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var bmpOut = new Bitmap(100, 100);
var request = (HttpWebRequest)WebRequest.Create("http://media.zenfs.com/en_us/Sports/ap/201305061639599673916-p2.jpeg");
request.AllowWriteStreamBuffering = true;
var response = (HttpWebResponse) request.GetResponse();
var myStream = response.GetResponseStream();
var b = new Bitmap(myStream);
var g = Graphics.FromImage(bmpOut);
g.InterpolationMode = InterpolationMode.High;
g.DrawImage(b,0,0,50, 50);
context.Response.ContentType = "image/png";
var stream = new MemoryStream();
b.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
b.Dispose();
stream.WriteTo(HttpContext.Current.Response.OutputStream);
//context.Response.Write("Hello World");
}
private Bitmap ResizeImage(Image image, int width, int height)
{
Bitmap result = new Bitmap(width, height);
result.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(result))
{
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.DrawImage(image, 0, 0, result.Width, result.Height);
}
return result;
}