QR Bar code Generate in asp.net using C#
Require ments:
Visual studio
MessagingToolkit.dll
Source Code(Default.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="bar.aspx.cs" Inherits="qr_barcode.bar" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="URL" runat="server"></asp:TextBox>
<br /><br />
<asp:Image ID = "imgText" runat = "server" Visible = "false" />
<br />
<%--
<asp:FileUpload ID="LogoUpload" runat="server"
/>
<br /><br />--%>
<asp:Button ID="CreateCode" runat="server" Text="Create QR Code" OnClick="CreateCode_OnClick" />
<br /><br />
<asp:Image runat="server" ID="QRImage" />
</div>
</form>
</body>
</html>
Codebehind Page(default.aspx.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MessagingToolkit.QRCode.Codec;
using MessagingToolkit.QRCode.Codec.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.IO;
namespace qr_barcode
{
public partial class bar : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CreateCode_OnClick(object sender, EventArgs e)
{
//string
path = "c:\\code\\projects\\CodeCreator\\CodeCreator\\";
string path = "D:\\anu\\qr
barcode\\qr barcode\\";
string a = URL.Text;
QRCodeEncoder encoder = new QRCodeEncoder();
encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H; // 30%
encoder.QRCodeScale = 10;
//
string text = URL.Text.Trim();
Bitmap bitmap = new Bitmap(1, 1);
Font font = new Font("Arial", 25, FontStyle.Regular, GraphicsUnit.Pixel);
Graphics graphics = Graphics.FromImage(bitmap);
int width = (int)graphics.MeasureString(text, font).Width;
int height = (int)graphics.MeasureString(text, font).Height;
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(text, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0);
graphics.Flush();
string fileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".jpg";
bitmap.Save(Server.MapPath("image/") +
fileName, ImageFormat.Jpeg);
imgText.ImageUrl = "image/" +
fileName;
//
imgText.Visible = true;
//
Bitmap img = encoder.Encode(URL.Text);
// LogoUpload.SaveAs(path +
LogoUpload.FileName);
System.Drawing.Image logo = System.Drawing.Image.FromFile(path +
imgText.ImageUrl);
// System.Drawing.Image logo =
System.Drawing.Image("URL");
int left = (img.Width / 2) - (logo.Width / 2);
int top = (img.Height / 2) - (logo.Height / 2);
Graphics g = Graphics.FromImage(img);
g.DrawImage(logo, new Point(left, top));
img.Save(path + "img.jpg", ImageFormat.Jpeg);
QRImage.ImageUrl = "img.jpg";
}
}
}