generating qr-code image [duplicate] - c#

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

Related

How to convet telerik radbarcode to bitmap without missing label?

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");

Generating IDAutomationHC39M barcode is generating well but need to remove value

Hi I am using below code and the bar code is generating well but how can I remove text written down to label.
public void generateBarcode(string id)
{
int w = id.Length * 55;
Bitmap oBitmap = new Bitmap(w, 100);
Graphics oGraphics = Graphics.FromImage(oBitmap);
Font oFont = new Font("IDAutomationHC39M", 18);
PointF oPoint = new PointF(2f, 2f);
SolidBrush oBrushWrite = new SolidBrush(Color.Black);
SolidBrush oBrush = new SolidBrush(Color.White);
oGraphics.FillRectangle(oBrush, 0, 0, w, 100);
oGraphics.DrawString("*" + id + "*", oFont, oBrushWrite, oPoint);
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (System.IO.FileStream fs = System.IO.File.Open(Server.MapPath("~/img/barcodes/") + id + ".jpg", FileMode.Create))
{
oBitmap.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
}
oBitmap.Dispose();
imgbarcode.ImageUrl = "~/img/barcodes/" + id + ".jpg";
}
My bar code is generating as below. Here I need to remove the bar code text as 76
According to this, the font you need to use without text is "IDAutomationC39M". Unfortunately, that's not in the free version.

Adding dynamic text to image

I currently have an image which gets displayed to the user, I'm trying to add dynamic text to this image based on two parameters passed in.
The issue I have is when I step through the code it all seems to be working correctly, however when I see the image on the screen after the below code has run it doesn't have the text on it.
Below is my current set up of code:
public ActionResult GenerateImage(string savingAmount, string savingDest)
{
// Hardcoding values for testing purposes.
savingAmount = "25,000.00";
savingDest = "Canada";
PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);
Image imgBackground = Image.FromFile(Server.MapPath("~/assets/img/fb-share.jpg"));
int phWidth = imgBackground.Width; int phHeight = imgBackground.Height;
Bitmap bmBackground = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
bmBackground.SetResolution(72, 72);
Graphics grBackground = Graphics.FromImage(bmBackground);
Bitmap bmWatermark;
Graphics grWatermark;
bmWatermark = new Bitmap(bmBackground);
bmWatermark.SetResolution(imgBackground.HorizontalResolution, imgBackground.VerticalResolution);
grWatermark = Graphics.FromImage(bmWatermark);
grBackground.SmoothingMode = SmoothingMode.AntiAlias;
// Now add the dynamic text to image
using (Graphics graphics = Graphics.FromImage(imgBackground))
{
using (Font arialFont = new Font("Arial", 10))
{
grWatermark.DrawString(savingAmount, arialFont, Brushes.White, firstLocation);
grWatermark.DrawString(savingDest, arialFont, Brushes.White, secondLocation);
}
}
imgBackground.Save(Response.OutputStream, ImageFormat.Png);
Response.ContentType = "image/png";
Response.Flush();
Response.End();
return null;
}
As mentioned after this code has run, I then see the image in the browser however text is not displayed on the image, can anyone see / suggest what maybe causing this issue?
I feel like there are way to many images in that code for what you are describing as the intent of the code. What you want should reduce to this:
Load Image
Create Graphics on that Image
Draw into the Graphics and close
Output image to client
In the code sample you provided you are opening the Graphics on imgBackground then drawing into the grWatermark graphics which is opened earlier on against an image you never touch again.
public ActionResult GenerateImage(string savingAmount, string savingDest)
{
// Hardcoding values for testing purposes.
savingAmount = "25,000.00";
savingDest = "Canada";
PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);
Image imgBackground = Image.FromFile(Server.MapPath("~/assets/img/fb-share.jpg"));
using (Graphics graphics = Graphics.FromImage(imgBackground))
{
using (Font arialFont = new Font("Arial", 10))
{
graphics.DrawString(savingAmount, arialFont, Brushes.White, firstLocation);
graphics.DrawString(savingDest, arialFont, Brushes.White, secondLocation);
}
}
imgBackground.Save(Response.OutputStream, ImageFormat.Png);
Response.ContentType = "image/png";
Response.Flush();
Response.End();
return null;
}

How to draw a image using C# grapics class

I want to draw a image using using C# graphics class using provided X,Y coordinates and zoom Value. I tried to do this but it is not giving me the correct result.
Stream originalStream = ImageHelper.UrlToImageStream(list1.FirstOrDefault().OriginalImageUrl);
var bmp = new Bitmap(bmp.Width, bmp.Height);
int width = 0;
int height = 0;
var img = new Bitmap(bmp,
(int)(bmp.Size.Width / zoomLevel),
(int)(bmp.Size.Height / zoomLevel));
var g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawString(Text, SystemFonts.DefaultFont, Brushes.White, new Rectangle((int)CurrentTextX, (int)CurrentTextY, bmp.Width, bmp.Height));
g.DrawImage(img, new Rectangle((int)CurrentX, (int)CurrentY, bmp.Width, bmp.Height));
var stream = new System.IO.MemoryStream();
img.Save(stream, ImageFormat.Jpeg);
private void mapPaint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Image img = Image.FromFile(#"C:\img.png");
g.DrawImage(img, new Rectangle(10,10,img.Width/zoomLevel, img.Height/zoomLevel);
}
Try it like this also what is the incorrect result you are getting right now?

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