How to convet telerik radbarcode to bitmap without missing label? - c#

I am using Telerik RadBarcode in my Asp.Net web application. See my code to generate the RadBarcode programmatically.
RadBarcode barcode = new RadBarcode();
barcode.Type = BarcodeType.Code128;
barcode.ID = "RadBarcode1";
rcode.Text = "656146114";
barcode.ShowText = true;
barcode.OutputType = BarcodeOutputType.SVG_VML;
Bitmap bitmap1 = new Bitmap(barcode.GetImage());
bitmap1.Save("D:\\" + "\\bmap.jpg");
While saving the barcode as an image the text under the barcode is missing in my case.

We can place a label below the barcode image to show the text and create one new bitmap image which contains the text and barcode data. Refer to the example below;
RadBarcode barcodeRB = new RadBarcode();
barcodeRB.Type = BarcodeType.Code128;
barcodeRB.Text ="123456789";
barcodeRB.ShowText = true;
barcodeRB.OutputType = BarcodeOutputType.SVG_VML;
Bitmap bitmap1 = new Bitmap(barcodeRB.GetImage());
System.Drawing.Image image = barcodeRB.GetImage();
Bitmap barCanvas = new Bitmap(435, 205);
barCanvas.SetResolution(90, 90);
using (Graphics gfx = Graphics.FromImage(barCanvas))
using (SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255)))
{
gfx.FillRectangle(brush, 0, 0, 435, 205);
}
using (Graphics gr = Graphics.FromImage(barCanvas))
{
RectangleF rectf = new RectangleF(150, 170, 300, 50);
gr.DrawImage(image, 20, 20, 400, 150);
gr.DrawString(studentTest.BarcodeString, new Font("GenericSansSerif", 26), Brushes.Black, rectf);
}
barCanvas.Save("D:\" + "\bmap.jpg");

Related

Printing Multiple Pages C#

I'm trying to print some text data using an event handler, and then print a PDF on the second page.
I'm using pdfium viwer to do this and it works individually. But I'm struggling to combine them.
I need to get the two to print as 2 pages of the same print document, as I want to use a duplex printer to print the pdf on the back. I don't just want to send two individual pages.
Sample code:
To Print PDF:
private void button1_Click(object sender, EventArgs e)
{
string filena = #textBox1.Text;
PrintPDF("M2020", "A4", filena, 1);
}
public bool PrintPDF(string printer, string paperName, string filename, int copies)
{
try
{
// Create the printer settings for our printer
var printerSettings = new PrinterSettings
{
PrinterName = printer,
Copies = (short)copies,
};
// Create our page settings for the paper size selected
var pageSettings = new PageSettings(printerSettings)
{
Margins = new Margins(0, 0, 0, 0),
};
foreach (PaperSize paperSize in printerSettings.PaperSizes)
{
if (paperSize.PaperName == paperName)
{
pageSettings.PaperSize = paperSize;
break;
}
}
// Now print the PDF document
using (var document = PdfDocument.Load(filename))
{
using (var printDocument = document.CreatePrintDocument())
{
printDocument.PrinterSettings = printerSettings;
printDocument.DefaultPageSettings = pageSettings;
printDocument.PrintController = new StandardPrintController();
printDocument.Print();
}
}
return true;
}
catch
{
return false;
}
}
And my original code to print a QR code:
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
string s2 = "Modified by: ";
string s6 = "Author: " + author;
string s4 = "Created: " + drawdate;
QRCodeGenerator qrGenerator1 = new QRCodeGenerator();
QRCodeData qrCodeDataPrint = qrGenerator1.CreateQrCode(qrcodedata, QRCodeGenerator.ECCLevel.Q, false, false);
Bitmap qrCodeImagePrint = (new QRCode(qrCodeDataPrint)).GetGraphic(20);
Bitmap bmimg = new Bitmap(this.pictureBox2.Width, this.pictureBox2.Height);
this.pictureBox2.DrawToBitmap(bmimg, new Rectangle(0, 0, this.pictureBox2.Width, this.pictureBox2.Height));
System.Drawing.Font f1 = new System.Drawing.Font("Arial", 5f, FontStyle.Bold, GraphicsUnit.Millimeter);
System.Drawing.Font f2 = new System.Drawing.Font("Arial", 2f, GraphicsUnit.Millimeter);
System.Drawing.Font f3 = new System.Drawing.Font("Arial", 3f, GraphicsUnit.Millimeter);
System.Drawing.Font barc = new System.Drawing.Font("Code39Azalea", 36f, GraphicsUnit.Point);
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
Pen blackPen = new Pen(Color.Black, 1f);
Rectangle rect = new Rectangle(165, 35, 30, 30);
Rectangle qrcodetest = new Rectangle(170, 40, 25, 25);
Rectangle btmimg = new Rectangle(60, 140, 120, 120);
Rectangle combox = new Rectangle(20, 145, 160, 40);
e.Graphics.DrawImage(qrCodeImagePrint, qrcodetest);
e.Graphics.DrawRectangle(blackPen, qrcodetest);
e.Graphics.DrawString(this.procreq, f1, Brushes.Black, new Point(50, 5));
e.Graphics.DrawString(s1, f1, Brushes.Black, new Point(150, 70));
e.Graphics.DrawString(s1z, f2, Brushes.Black, new Point(150, 75));
e.Graphics.DrawString(qrissuedby, f2, Brushes.Black, new Point(20, 264));
e.Graphics.DrawString(s6, f2, Brushes.Black, new Point(20, 267));
e.Graphics.DrawString(s4, f2, Brushes.Black, new Point(20, 270));
e.Graphics.DrawString(s2, f2, Brushes.Black, new Point(20, 273));
e.Graphics.DrawString(s3, f2, Brushes.Black, new Point(20, 276));
e.Graphics.DrawString(s5, f2, Brushes.Black, new Point(20, 279));
e.Graphics.DrawString(this.notebox.Text, f3, Brushes.Black, combox);
e.Graphics.DrawImage(bitmap1, btmimg);
bmimg.Dispose();
bitmap1.Dispose();
}
Any help would be much appreciated!
Thanks
Andrew
Ok, think i was going about that in completely the wrong way. What I eventually did was use the pdfium viewer to render the pdf as a bitmap, and then printed that as the second page.
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
Pen blackPen = new Pen(Color.Black, 1f);
int actualwidth = (int)(e.Graphics.DpiX * 210 / 25.4f);
int actualHeight = (int)(e.Graphics.DpiY * 297 / 25.4f);
using (var pdfDocument = PdfiumViewer.PdfDocument.Load(#textBox1.Text))
{
var bitmapImage = pdfDocument.Render(0, actualwidth, actualHeight, false);
pictureBox1.Image = bitmapImage;
pictureBox1.Image.RotateFlip((RotateFlipType.Rotate90FlipNone));
}
if (pgnum == 1)
{
Debug.WriteLine("firing first page " + pgnum);
Rectangle qrcodetest = new Rectangle(170, 40, 25, 25);
e.Graphics.DrawRectangle(blackPen, qrcodetest);
e.HasMorePages = true;
pgnum++;
Debug.WriteLine(pgnum);
}
else
{
Debug.WriteLine("firing second page " + pgnum);
GraphicsUnit units = GraphicsUnit.Millimeter;
Rectangle srcRect = new Rectangle(0, 0, 199, 281);
//e.Graphics.Q
e.Graphics.Clear(Color.White);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
e.Graphics.DrawImage(pictureBox1.Image, srcRect);
e.HasMorePages = false;
}
This does work.... but the printed version of the PDF is very poor quality. I've tried using interpolation etc.... but doesn't seem to make any difference, so not sure i'm putting it in the right place?
Also, i've tried using the bitmap variable rather than the picturebox image, but it seems to generate the same result. The PDF prints much better.
Thanks
Andrew

Adding two image into one image c#?

I want to combine two image but I cant success . Help me please.
When try the combine PictureBox show only first image but there is not second image , when I remove first image I can see second image.
Also I tried setting first image and draw the text on image that's also not working. Please help.
Image myimg = Code128Rendering.MakeBarcodeImage(textBox1.Text, 2, true);
Bitmap image = new Bitmap(myimg.Width + 20, myimg.Height + 50);
pictureBox1.DrawToBitmap(image, new Rectangle(0, 0, myimg.Width + 20, myimg.Height + 50));
Bitmap bmp = new Bitmap(myimg.Width + 20, myimg.Height);
Bitmap bmp2 = new Bitmap(myimg.Width + 20, 20);
Graphics Cizgi2 = Graphics.FromImage(bmp2);
Graphics Cizgi = Graphics.FromImage(bmp);
Cizgi.DrawImage(myimg, 0, 0);
FontStyle sitil = FontStyle.Bold;
Font fonts = new Font(new FontFamily("Arial"), 10, sitil);
Cizgi2.DrawString(textBox1.Text, fonts, Brushes.Black, 5, myimg.Height + 10);
Graphics g = Graphics.FromImage(image);
g.DrawImage(bmp, new Point(10, 0));
g.DrawImage(bmp2, new Point(0, bmp.Height + 10));
I want to image seems like first but i cant make
It looks like you are trying to concatenate two images vertically? It's pretty simple actually, you can look here (C# image concatenation), but I've also modified it for your needs. I think this should work:
float drawBorderX = 5;
float drawBorderY = 5;
//Set up our two images
Bitmap barCode = Code128Rendering.MakeBarcodeImage(textBox1.Text, 2, true);
Bitmap text = new Bitmap(barCode.Width, 50);
Graphics textGraphics = Graphics.FromImage(text);
//Draw the text to the bottom image.
FontStyle sitil = FontStyle.Bold;
Font fonts = new Font(new FontFamily("Arial"), 10, sitil);
textGraphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, text.Width, text.Height));
textGraphics.DrawString(textBox1.Text, fonts, Brushes.Black, drawBorderX, drawBorderY);
//Vertically concatenate the two images.
Bitmap resultImage = new Bitmap(Math.Max(barCode.Width, text.Width), barCode.Height + text.Height);
Graphics g = Graphics.FromImage(resultImage);
g.DrawImage(barCode, 0, 0);
g.DrawImage(text, 0, barCode.Height);
Edit: Note that resultImage will contain the image you want, so you can set your PictureBox to be that image at the end.

generating qr-code image [duplicate]

This question already has answers here:
Free c# QR-Code generator [closed]
(4 answers)
Closed 7 years ago.
After using this lines of code bellow
private void button1_Click(object sender, EventArgs e)
{
string barcode = textBox1.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 white = new SolidBrush(Color.White);
SolidBrush black = new SolidBrush(Color.Black);
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);
pictureBox1.Image = bitmap;
pictureBox1.Height = bitmap.Height;
pictureBox1.Width = bitmap.Width;
}
}
i was able to generate this output image barcode
i want to generate a bar code that output like the image bellow how can i achieve this
You can do it like this
private System.Drawing.Image GenerateQRCode(string content, int size)
{
QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode;
encoder.TryEncode(content, out qrCode);
GraphicsRenderer gRenderer = new GraphicsRenderer(new FixedModuleSize(4, QuietZoneModules.Two), System.Drawing.Brushes.Black, System.Drawing.Brushes.White);
//Graphics g = gRenderer.Draw(qrCode.Matrix);
MemoryStream ms = new MemoryStream();
gRenderer.WriteToStream(qrCode.Matrix, ImageFormat.Bmp, ms);
var imageTemp = new Bitmap(ms);
var image = new Bitmap(imageTemp, new System.Drawing.Size(new System.Drawing.Point(size, size)));
//image.Save("file.bmp", ImageFormat.Bmp);
return (System.Drawing.Image)image;
}
Implementaion
string barcode = textBox1.Text;
codeImage = GenerateQRCode(barcode, 120);
// you can make a smaller image as per your need
rect = new System.Drawing.Rectangle(1080, 530, codeImage.Width, codeImage.Height);
using (Graphics g = Graphics.FromImage(picEdit))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(codeImage, rect);
}
Do not forget to add
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
from GitHub: QrCode.Net

Printing XRLabel on every Report Page

I have a method which get a XtraReport as Parameter. It should printing an infoarea on all Pages the Report got. How can i achieve this goal? It must be printed on DetailBand and the LabelText got Angle so i cant use PageInfo Control.
In fact the Problem is: How can i print a XRLabel on all ReportPages of a XtraReport Object.
I tried this out but without success:
XRLabel druckinfo = new XRLabel();
druckinfo.Angle = 90F;
druckinfo.Padding = new PaddingInfo(2, 2, 0, 0, 96F);
druckinfo.SizeF = new SizeF(29.16666F, 500F);
druckinfo.Font = new Font(StyleVerwaltung.Instance.Schriftart,
StyleVerwaltung.Instance.SchriftgroesseDruckInfo);
druckinfo.Text = text;
druckinfo.LocationF = new PointF(0F, 500F);
foreach (Band band in _Report.Bands)
{
if (band is DetailBand)
{
band.Controls.Add(druckinfo);
}
}
The DevExpress Support show me a way to solve my problem:
Image img = new Bitmap(300, 300);
Graphics g = Graphics.FromImage(img);
Font schriftart = new Font(StyleVerwaltung.Instance.Schriftart,
StyleVerwaltung.Instance.SchriftgroesseDruckInfo);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
_Report.Watermark.ImageAlign = ContentAlignment.BottomLeft;
_Report.Watermark.ImageViewMode = ImageViewMode.Clip;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TranslateTransform(0, g.VisibleClipBounds.Size.Height);
g.RotateTransform(270f);
g.DrawString(text, schriftart, Brushes.Black,
new Rectangle(0, 0, (int)g.VisibleClipBounds.Size.Width,
(int)g.VisibleClipBounds.Height),
format);
g.ResetTransform();
g.Flush();
_Report.Watermark.Image = img;
_Report.Watermark.ShowBehind = true;
It uses the Watermark to accomplish this task. It just works if you dont use Watermark in other context but for my goal it works as expected.

Generate transparent PNG c#

I have the function below to generate a sample logo. What I want to do is to return a transparent png or gif instead of a white background.
How can I do that?
private Bitmap CreateLogo(string subdomain)
{
Bitmap objBmpImage = new Bitmap(1, 1);
int intWidth = 0;
int intHeight = 0;
Font objFont = new Font(
"Arial",
13,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Pixel);
Graphics objGraphics = Graphics.FromImage(objBmpImage);
intWidth = (int)objGraphics.MeasureString(subdomain, objFont).Width;
intHeight = (int)objGraphics.MeasureString(subdomain, objFont).Height;
objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
objGraphics = Graphics.FromImage(objBmpImage);
objGraphics.Clear(Color.White);
objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
objGraphics.DrawString(
subdomain, objFont,
new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
objGraphics.Flush();
return (objBmpImage);
}
Here is the end result:
context.Response.ContentType = "image/png";
using (MemoryStream memStream = new MemoryStream())
{
CreateLogo(_subdname).Save(memStream, ImageFormat.Png);
memStream.WriteTo(context.Response.OutputStream);
}
In the CreateLogo function:
objGraphics.Clear(Color.White) was changed to objGraphics.Clear(Color.Transparent)
new SolidBrush(Color.FromArgb(102, 102, 102)) changed to new SolidBrush(Color.FromArgb(255, 255, 255))
You can do something like this:
Bitmap bmp = new Bitmap(300, 300);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent);
g.FillRectangle(Brushes.Red, 100, 100, 100, 100);
g.Flush();
bmp.Save("test.png", System.Drawing.Imaging.ImageFormat.Png);
Take a look at Can you make an alpha transparent PNG with C#?

Categories