Create QRCODE with the help of handler.before writting this code i tried many libraries but none of them work for me.In the last i found handler which is given below
QRCode Problem Resolved
- No Watermark
- You can give width and height. Both Will be same such as qrcode image will be 200 X 200 px when you are giving input 200.
- Images are automatically save in the folder.
- Here i m creating QRCODE Image, Pdf, Eps format.
How to Create QRCODE
- Create Default website File --> New --> Website
- Add To Dll
- Gma.QrCodeNet.Encoding.Net35.dll
- itextsharp.dll
- Rename Default.aspx to test.aspx
Here the test.aspx Code copy and paste it
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet">
<script type="text/javascript">
function generate() {
var t = document.getElementById('t').value;
var e = document.getElementById('e').value;
//var q = document.getElementById('q').value;
var q = document.getElementById('size').value;
var s = document.getElementById('s').value;
var f = document.getElementById('f').value;
var image = document.getElementById('result');
//image.src = 'QrCodeHandler.ashx?e=' + e + '&q=' + q + '&s=' + s + '&f=' + f + '&t=' + encodeURI(t);
image.src = 'QrCodeHandler.ashx?e=' + e + '&q=' + q + '&s=' + s + '&f=' + f + '&t=' + encodeURI(t);
image.alt = image.title = t;
return false;
}
</script>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Native JavaScript <a href="/index.htm" class="pull-right"><i class="icon-home"></i></a></h1>
<p>A single JavaScript function building the URL to get the QR code image. Doesn't use jQuery or any other library.</p>
</div>
<div class="row">
<div class="span8">
<form class="form-horizontal" runat=server id="form1">
<div class="control-group">
<label class="control-label" for="t">QR code content</label>
<div class="controls">
<input type="text" id="t" placeholder="QR code content" class="input-block-level">
</div>
</div>
<div class="control-group">
<label class="control-label" for="e">Error correction level</label>
<div class="controls">
<select id="e">
<option value="L">Low</option>
<option value="M" selected="selected">Medium</option>
<option value="Q">Quartile</option>
<option value="H">High</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="size">QR code content size</label>
<div class="controls">
<input type="text" id="size" placeholder="QR code content" class="input-block-level">
</div>
</div>
<div class="control-group">
<label class="control-label" for="f">Image Format</label>
<div class="controls">
<select id="f">
<option value="Png" selected="selected">Png</option>
<option value="Gif" >Gif</option>
<option value="jpeg">Jpeg</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="s">Module size</label>
<div class="controls">
<select id="s">
<option value="4">4</option>
<option value="8">8</option>
<option value="12" selected="selected">12</option>
</select>
</div>
</div>
<div class="control-group">
<div class="controls">
<a onclick="return generate()" class="btn">Generate</a>
<asp:Button ID="btnsave" runat="server" Text="Save" onclick="btnsave_Click" class="btn" />
</div>
</div>
</form>
</div>
<div class="span4">
<img id="result" runat=server/>
</div>
</div>
</div>
</body>
</html>
Test.aspx.cs
using System;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Web;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void saveimage(string ImageLink)
{
}
protected void btnsave_Click(object sender, EventArgs e)
{
Response.Write(result.Src.ToString().Trim());
}
}
- Create Three Folder Name them eps, images, pdf on the root .
- Right Click On Solution Explorer -->Add New item --> Generic handler
- Name handler as QrCodeHandler.ashx
QrCodeHandler.ashx.cs Code
- using System;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Web;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
using System.Drawing;
using iTextSharp.text.pdf;
/// <summary>
/// Summary description for QrCodeHandler
/// </summary>
public class QrCodeHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// Retrieve the parameters from the QueryString
var codeParams = CodeDescriptor.Init(context.Request);
// Encode the content
if (codeParams == null || !codeParams.TryEncode())
{
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
return;
}
//
// Render the QR code as an image
using (var ms = new MemoryStream())
{
codeParams.Render(ms);
string appPath = HttpContext.Current.Request.ApplicationPath;
string physicalPath = HttpContext.Current.Request.MapPath(appPath);
string strpath = physicalPath + "";
string WorkingDirectory = strpath;
System.Drawing.Image imgSave = System.Drawing.Image.FromStream(ms);
Bitmap bmSave = new Bitmap(imgSave);
Bitmap bmTemp = new Bitmap(bmSave);
// Graphics grSave = Graphics.FromImage(bmTemp);
//grSave.DrawImage(imgSave, 0, 0, 12*33, 12*33);
String pdfextension = Convert.ToString(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Second.ToString());
bmTemp.Save(WorkingDirectory + "\\images\\" + "abc1" + pdfextension + "."+codeParams.ImgFormat.ToString().Trim());
// Create eps
bmTemp.Save(WorkingDirectory + "\\eps\\" + "abc1" + pdfextension + ".eps");
// eps close
// Create and Save pdf
var doc1 = new iTextSharp.text.Document();
// path = image source
String path = WorkingDirectory + "\\images\\" + "abc1" + pdfextension + "." + codeParams.ImgFormat.ToString().Trim();
PdfWriter.GetInstance(doc1, new FileStream(WorkingDirectory + "/pdf/" +"abc1"+ pdfextension + ".pdf", FileMode.Create));
doc1.Open();
String filename = path;
String[] getimage = filename.Split('\\');
using (FileStream fs = new FileStream(WorkingDirectory+ "\\images\\" + getimage[4], FileMode.Open))
{
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(Image.FromStream(fs), System.Drawing.Imaging.ImageFormat.Png);
// png.ScaleAbsolute(500, 300);
doc1.Add(png);
}
doc1.Close();
// pdf close
imgSave.Dispose();
bmSave.Dispose();
bmTemp.Dispose();
// grSave.Dispose();
context.Response.ContentType = codeParams.ContentType;
context.Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length);
}
}
/// <summary>
/// Return true to indicate that the handler is thread safe because it is stateless
/// </summary>
public bool IsReusable
{
get { return true; }
}
}
/// <summary>
/// Class containing the description of the QR code and wrapping encoding and rendering.
/// </summary>
internal class CodeDescriptor
{
public ErrorCorrectionLevel Ecl;
public string Content;
public QuietZoneModules QuietZones;
public int ModuleSize;
public BitMatrix Matrix;
public string ContentType;
public string ImgFormat;
public int ImgSize;
/// <summary>
/// Parse QueryString that define the QR code properties
/// </summary>
/// <param name="request">HttpRequest containing HTTP GET data</param>
/// <returns>A QR code descriptor object</returns>
public static CodeDescriptor Init(HttpRequest request)
{
var cp = new CodeDescriptor();
// Error correction level
if (!TryParse(request.QueryString["e"], out cp.Ecl))
cp.Ecl = ErrorCorrectionLevel.M;
// Code content to encode
cp.Content = request.QueryString["t"];
// Size of the quiet zone
//if (!TryParse(request.QueryString["q"], out cp.QuietZones))
// cp.QuietZones = QuietZoneModules.Two;
// Module size
if (!int.TryParse(request.QueryString["s"], out cp.ModuleSize))
cp.ModuleSize = 12;
if (!int.TryParse(request.QueryString["q"], out cp.ImgSize))
cp.ImgSize = 396;
if (request.QueryString["f"].ToString().ToLower() == "png")
{
cp.ImgFormat = "png";
}
else if (request.QueryString["f"].ToString().ToLower() == "jpeg")
{
cp.ImgFormat = "jpeg";
}
else if (request.QueryString["f"].ToString().ToLower() == "gif")
{
cp.ImgFormat = "gif";
}
else
{
cp.ImgFormat = "png";
}
return cp;
}
/// <summary>
/// Encode the content with desired parameters and save the generated Matrix
/// </summary>
/// <returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns>
public bool TryEncode()
{
var encoder = new QrEncoder(Ecl);
QrCode qr;
if (!encoder.TryEncode(Content, out qr))
return false;
Matrix = qr.Matrix;
return true;
}
/// <summary>
/// Render the Matrix as a PNG image
/// </summary>
/// <param name="ms">MemoryStream to store the image bytes into</param>
internal void Render(MemoryStream ms)
{
// var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));
//test
var renderer = new GraphicsRenderer(new FixedCodeSize(ImgSize, QuietZoneModules.Two), Brushes.Black, Brushes.White);
// var renderer = new GraphicsRenderer(new FixedCodeSize(400, QuietZoneModules.Zero), Brushes.Black, Brushes.White);
// renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png,/*output*/);
// close test
//
if (ImgFormat == "png")
{
// render.WriteToStream(Matrix, ImageFormat.Png, ms);
renderer.WriteToStream(Matrix, ImageFormat.Png, ms);
ContentType = "image/png";
}
else if (ImgFormat == "gif")
{
// render.WriteToStream(Matrix, ImageFormat.Gif, ms);
renderer.WriteToStream(Matrix, ImageFormat.Png, ms);
ContentType = "image/gif";
}
else if (ImgFormat == "jpeg")
{
// render.WriteToStream(Matrix, ImageFormat.Jpeg, ms);
renderer.WriteToStream(Matrix, ImageFormat.Png, ms);
ContentType = "image/jpeg";
}
}
public static bool TryParse<TEnum>(string value, out TEnum result)
where TEnum : struct, IConvertible
{
var retValue = value == null ?
false :
Enum.IsDefined(typeof(TEnum), value);
result = retValue ?
(TEnum)Enum.Parse(typeof(TEnum), value) :
default(TEnum);
return retValue;
}
}
WebConfig
<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<appSettings/>
<connectionStrings/>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true">
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls>
</pages>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="OptionInfer" value="true"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
<!--
The system.webServer section is required for running ASP.NET AJAX under Internet
Information Services 7.0. It is not necessary for previous version of IIS.
-->
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ScriptModule"/>
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory"/>
<remove name="ScriptHandlerFactoryAppServices"/>
<remove name="ScriptResource"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>