Remove Text from below of the BarCode in ASP.NET(C#) - c#

I am generating the barcode generation of barcode is working fine barcode also read it perfectly.followin is the code for barcode generation:
private void GenerateBarCode(string codeInfo)
{
//Settings for the Image
string TypeFaceName = "IDAutomationHC39M";
string imageLocation = Server.MapPath("2010.png");
//The format of the image file
ImageFormat format = ImageFormat.Png;
//path of unique file name
string path = "D://MyProjects//RepeaterPaging//images//vijendra.png";
//REFERENCING A FONT
PrivateFontCollection fnts = new PrivateFontCollection();
fnts.AddFontFile("IDAutomationHC39M.ttf");
FontFamily fntfam = new FontFamily(TypeFaceName);
Font fnt = new Font(fntfam, 13);
fnts.AddFontFile("Arial.ttf");
FontFamily fntfam2 = new FontFamily("Arial", fnts);
//DRAWING THE IMAGE
Bitmap bmp = new Bitmap(960, 386); //Canvas size
Graphics g = Graphics.FromImage(bmp);
Bitmap orignBitmap = new Bitmap(imageLocation);
g.Clear(Color.Transparent); //Background color
SizeF bc = g.MeasureString(codeInfo, fnt);
Brush br = new SolidBrush(Color.Black);
g.DrawImage(orignBitmap, 10, 8);
g.DrawString(codeInfo, fnt, br, 585, 170); //Drawing the Image
g.TextRenderingHint=
bmp.Save(path, format); //Saving the Image file
bmp.Dispose(); //Releasing all resources (Image file)
Response.Clear();
}
alt text http://www.freeimagehosting.net/uploads/0e033f305b.png
Now I want to remove the text which is below of the barcode.
how can I do this?.

You can set Font = null; to remove text below barcode.
Barcode128 code128 = new Barcode128();
code128.CodeType = Barcode.CODE128;
code128.Code = "123456789";
code128.Font = null;

A better alternative might be to just use a font that doesn't have the text in the first place:
Try something like: Free Barcode Font - Code 39

You are creating the barcode using a font and the charcters under the bars are part of that font.
The only way to remove them would be to modify the bitmap (or crop it) after rendering the text; which requires knowing how big the final barcode is. Not impossible to do but a pain.

Related

How to make backgroud transparent while make image from string text in c#?

I am converting string text to image in c#. It's converting text to image properly but I need image with transparent background. I have try a lot with google r&d but none of that is working.
My code is as below :
string fullName = name.Trim();
Bitmap bitmap = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
string fontName = "Airin.ttf";
PrivateFontCollection privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile(Server.MapPath("~/Content/fontCss/" + fontName));
FontFamily ff = privateFontCollection.Families[0];
Font font = new Font(ff, 25, FontStyle.Regular, GraphicsUnit.Pixel);
Graphics graphics = Graphics.FromImage(bitmap);
int width = (int)graphics.MeasureString(fullName, font).Width;
int height = (int)graphics.MeasureString(fullName, font).Height;
bitmap.MakeTransparent(Color.Transparent);
bitmap = new Bitmap(bitmap, new Size(width, height));
graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
graphics.DrawString(fullName, font, Brushes.Black, 0, 0);
string fileName = Guid.NewGuid().ToString() + ".jpg";
bitmap.Save(Server.MapPath("~/UploadedDocuments/") + fileName, ImageFormat.Jpeg);
fileName = "/UploadedDocuments/" + fileName;
So where am I need to change code here for get image with transparent backgroud ?
You need to save the image in a format that supports transparencies, such as GIF or PNG. JPEG does not.
Transparent background in JPEG image
Change the ImageFormat parameter at the end of the following line to an appropriate value (e.g. ImageFormat.Gif or ImageFormat.Png)
bitmap.Save(Server.MapPath("~/UploadedDocuments/") + fileName, ImageFormat.Jpeg);
Also, have you tried changing
graphics.Clear(Color.White);
to use Color.Transparent instead?

Bold a part of string in generated barcode

I am generating a barcode as an image. The barcode consists of few different values, such as amount, length, width and m2. These numbers are displaying below the barcode as a summary of all user entries. Is there a way to either bold or underline the m2 (square meters) in the summary under the barcode? Please see below sample of what is needed:
Here's the code I use to generate the barcode:
private void generate_Click(object sender, EventArgs e)
{
String barcode = summary.Text;
Bitmap bitmap = new Bitmap(barcode.Length * 40, 150);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
Font ofont = new System.Drawing.Font("IDAutomationHC39M", 20);
PointF point = new PointF (2f, 2f);
SolidBrush black = new SolidBrush(Color.Black);
SolidBrush White = new SolidBrush(Color.White);
graphics.FillRectangle(White, 0, 0, bitmap.Width, bitmap.Height);
graphics.DrawString("*" + barcode + "*", ofont, black, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, ImageFormat.Png);
box4.Image = bitmap;
box4.Height = bitmap.Height;
box4.Width = bitmap.Width;
}
}
You can use the constructor of Font that accepts a font style (docs)
new System.Drawing.Font("IDAutomationHC39M", 20, FontStyle.Bold);
The problem comes in determining what part of the text should be bold which means you will have to split the text up at a certain point, and ascertain the offset of the bold text
Since the font you're using (IDAutomationHC39M), renders the bar code also, this won't work unless you find a different font that will allow you to render the bars separately to the text. This leaves you with a few options.
Separate fonts
Don't make the text you want to bold
Make it stand out in a different way, colour the text in a different colour that will make it stand out / draw a line under it / etc
If this was just text
You need to break the text up into 2 parts,
string barcode1; //the normal bit
string barcode2; //the bold/underlined bit
Font ofont = new System.Drawing.Font("IDAutomationHC39M", 20);
Font ofontBold = new System.Drawing.Font("IDAutomationHC39M", 20, FontStyle.Bold);
Then render text in 3 stages, measuring the offset of each previous part:
graphics.DrawString("*" + barcode1, ofont, black, point);
var point2 = new PointF(point.X + graphics.MeasureString("*" + barcode1, ofont).Width, point.Y);
graphics.DrawString(barcode2, ofontBold, black, point2);
var point3 = new PointF(point2.X + graphics.MeasureString(barcode2, ofontBold).Width, point2.Y);
graphics.DrawString("*", ofont, black, point3);
However the font includes the lines
So I think the best you can do is to draw an underline using the same string measuring techniques:
string barcode1; //the normal bit
string barcode2; //the underlined bit
var lineStartX = point.X + graphics.MeasureString("*" + barcode1, ofont).Width;
var lineWidth = graphics.MeasureString(barcode2).Width;

How to convert string to a PictureBox in C# [duplicate]

This question already has answers here:
How to Add the values of List<string> into the List<PictureBox> after encoding it to Barcode in c#
(3 answers)
Closed 9 years ago.
I have an array of type PictureBox. I want to fill it the List of string and then covert it to the Barcode. But I am uncle to convert the string to the PictureBox. Is there any step I can do to make them compatible?
System.Windows.Forms.PictureBox[] PictureBoxArray = new PictureBox[3];
List<string> serial = new List<string>;
public void ConvertToBarCode()
{
BarcodeLib.TYPE barcodetype1 = BarcodeLib.TYPE.CODE39;
BarcodeLib.Barcode bar1 = new BarcodeLib.Barcode();
bar1.IncludeLabel = true;
PictureBoxArray[0] = serial[0]; // Want to Convert String to PictureBox
PictureBoxArray[0].Image = bar1.Encode(barcodetype1, SerialNumberList[0]);
}
I have filles the serial List with the string now just want the conversion.
you want like this.. right?? see this is the representaion of the this string "S1253551" in 3of9 and plain text and finally as image right??
public Image stringToImage(string inputString)
{
string text = inputString.Trim();
Bitmap bmp = new Bitmap(1, 1);
//Set the font style of output image
Font font = new Font("Free 3 of 9", 25, FontStyle.Regular, GraphicsUnit.Pixel);
Font font2 = new Font("Arial", 15, FontStyle.Regular, GraphicsUnit.Pixel);
Graphics graphics = Graphics.FromImage(bmp);
int width = (int)graphics.MeasureString(text, font).Width;
int height = (int)graphics.MeasureString(text, font).Height;
int height2 = (int)graphics.MeasureString(text, font2).Height;
bmp = new Bitmap(bmp, new Size(width, height+height2));
graphics = Graphics.FromImage(bmp);
//Specify the background color of the image
graphics.Clear(Color.Cyan);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
//Specify the text, font, Text Color, X position and Y position of the image
graphics.DrawString(text, font, new SolidBrush(Color.Black), 0, 0);
graphics.DrawString(text, font2, new SolidBrush(Color.Black), 0, height);
graphics.Flush();
graphics.Dispose();
//if you want to save the image uncomment the below line.
//bmp.Save(#"d:\myimage.jpg", ImageFormat.Jpeg);
return bmp;
}
Remember you must have installed "free 3 of 9" font.
you pass the string "S1253551" and it generate the barcode and add the plain text at bottom and finally return it as image.
Its working code i have tried at my end. Enjoy. :)
Download the working code from here Download

Download generated bitmap image from ASP.NET website

I'm dynamically generating image from text, and existing image on my asp.net website.
Here is the code:
string barcode = Request.QueryString["BarCode"];
int w = barcode.Length * 40;
// Create a bitmap object of the width that we calculated and height of 100
Bitmap oBitmap = new Bitmap(w, 50);
// then create a Graphic object for the bitmap we just created.
Graphics oGraphics = Graphics.FromImage(oBitmap);
// Now create a Font object for the Barcode Font
// (in this case the IDAutomationHC39M) of 18 point size
Font oFont = new Font("BarcodeFont", 12);
// Let's create the Point and Brushes for the barcode
PointF oPoint = new PointF(2f, 2f);
SolidBrush oBrushWrite = new SolidBrush(Color.Black);
SolidBrush oBrush = new SolidBrush(Color.White);
// Now lets create the actual barcode image
// with a rectangle filled with white color
oGraphics.FillRectangle(oBrush, 0, 0, w, 100);
// We have to put prefix and sufix of an asterisk (*),
// in order to be a valid barcode
oGraphics.DrawString(barcode, oFont, oBrushWrite, oPoint);
// Then we send the Graphics with the actual barcode
Response.ContentType = "image/png";
oBitmap.Save(Response.OutputStream, ImageFormat.Png);
As you can see the bitmap is saved and showed on aspx page after postback. What I wanna do is when user click Button1, then image is generated and browser download window pops up, without saving on server or showing on page. How to do this? Please help me.
You should update your response like following:
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition","attachment; filename=downloadedFile.JPG");
Response.TransmitFile( #"c:/my documents/images/file.xxx" );
Response.End();
For more Information: http://www.west-wind.com/weblog/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET

Created image from text cannot be displayed?

I have converted text to Image(.png) using ASP.NET C# in Visual Studio 2010. But the image created cannot be displayed in the browser after submitting and it shows an message
The image "http://localhost:49670/WebSite1/Default.aspx" cannot be displayed, because it contains errors.
During debugging there is no error or warning or anything like that. How can I resolve this?
Looking at the Url there is a chance that you are rendering image intermixed with HTML content...
Using ASHX is better option to render images - check out http://aspalliance.com/1322_Displaying_Images_in_ASPNET_Using_HttpHandlers.all
Try this....
private Bitmap CreateBitmapImage(string sImageText)
{
Bitmap objBmpImage = new Bitmap(1, 1);
int intWidth = 0;
int intHeight = 0;
// Create the Font object for the image text drawing.
Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
// Create a graphics object to measure the text's width and height.
Graphics objGraphics = Graphics.FromImage(objBmpImage);
// This is where the bitmap size is determined.
intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;
// Create the bmpImage again with the correct size for the text and font.
objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
// Add the colors to the new bitmap.
objGraphics = Graphics.FromImage(objBmpImage);
// Set Background color
objGraphics.Clear(Color.White);
objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
objGraphics.Flush();
return (objBmpImage);
}

Categories

Resources