Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google Charts QR Code generation.
async void Main()
{
var content = "Hello, World!";
var size = QRCodeSize.Large;
var errorCorrection = QRErrorCorrection.H;
var qrUrl = GenerateQRCodeUrl(content, size, errorCorrection);
qrUrl.Dump();
var image = await GetQRCodeImage(qrUrl);
image.Dump();
}
public enum QRCodeSize
{
Small = 120,
Medium = 230,
Large = 350
}
public enum QRErrorCorrection
{
L,
M,
Q,
H
}
private string GenerateQRCodeUrl(string content, QRCodeSize size, QRErrorCorrection errorCorrection)
{
var baseUrl = "http://chart.apis.google.com/chart?cht=qr&chs={0}x{0}&chld={1}&choe=UTF-8&chl={2}";
return string.Format(baseUrl, (int)size, errorCorrection.ToString(), Uri.EscapeDataString(content));
}
private async Task<Image> GetQRCodeImage(string url)
{
using (var client = new HttpClient())
{
var msg = await client.GetAsync(url);
if (msg.IsSuccessStatusCode)
{
using (var stream = await msg.Content.ReadAsStreamAsync())
{
var image = Image.FromStream(stream);
return image;
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.