Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote a basic HTML helper method to emit the correct <code>&lt;img&gt;</code> tag to take advantage of Google's API. So, on your page (assuming ASPX view engine) use something like this:</p> <pre><code>&lt;%: Html.QRCodeImage(Request.Url.AbsolutePath) %&gt; &lt;%: Html.QRCodeImage("Meagre human needs a phone to read QR codes. Ha ha ha.") %&gt; </code></pre> <p>Or if you want to specify the size in pixels (the image is always square):</p> <pre><code>&lt;%: Html.QRCodeImage(Request.Url.AbsolutePath, size: 92) %&gt; </code></pre> <p>Here is the code:</p> <pre><code>public static class QRCodeHtmlHelper { /// &lt;summary&gt; /// Produces the markup for an image element that displays a QR Code image, as provided by Google's chart API. /// &lt;/summary&gt; /// &lt;param name="htmlHelper"&gt;&lt;/param&gt; /// &lt;param name="data"&gt;The data to be encoded, as a string.&lt;/param&gt; /// &lt;param name="size"&gt;The square length of the resulting image, in pixels.&lt;/param&gt; /// &lt;param name="margin"&gt;The width of the border that surrounds the image, measured in rows (not pixels).&lt;/param&gt; /// &lt;param name="errorCorrectionLevel"&gt;The amount of error correction to build into the image. Higher error correction comes at the expense of reduced space for data.&lt;/param&gt; /// &lt;param name="htmlAttributes"&gt;Optional HTML attributes to include on the image element.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static MvcHtmlString QRCode(this HtmlHelper htmlHelper, string data, int size = 80, int margin = 4, QRCodeErrorCorrectionLevel errorCorrectionLevel = QRCodeErrorCorrectionLevel.Low, object htmlAttributes = null) { if (data == null) throw new ArgumentNullException("data"); if (size &lt; 1) throw new ArgumentOutOfRangeException("size", size, "Must be greater than zero."); if (margin &lt; 0) throw new ArgumentOutOfRangeException("margin", margin, "Must be greater than or equal to zero."); if (!Enum.IsDefined(typeof(QRCodeErrorCorrectionLevel), errorCorrectionLevel)) throw new InvalidEnumArgumentException("errorCorrectionLevel", (int)errorCorrectionLevel, typeof (QRCodeErrorCorrectionLevel)); var url = string.Format("http://chart.apis.google.com/chart?cht=qr&amp;chld={2}|{3}&amp;chs={0}x{0}&amp;chl={1}", size, HttpUtility.UrlEncode(data), errorCorrectionLevel.ToString()[0], margin); var tag = new TagBuilder("img"); if (htmlAttributes != null) tag.MergeAttributes(new RouteValueDictionary(htmlAttributes)); tag.Attributes.Add("src", url); tag.Attributes.Add("width", size.ToString()); tag.Attributes.Add("height", size.ToString()); return new MvcHtmlString(tag.ToString(TagRenderMode.SelfClosing)); } } public enum QRCodeErrorCorrectionLevel { /// &lt;summary&gt;Recovers from up to 7% erroneous data.&lt;/summary&gt; Low, /// &lt;summary&gt;Recovers from up to 15% erroneous data.&lt;/summary&gt; Medium, /// &lt;summary&gt;Recovers from up to 25% erroneous data.&lt;/summary&gt; QuiteGood, /// &lt;summary&gt;Recovers from up to 30% erroneous data.&lt;/summary&gt; High } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload