-
-
Save phaufe/7672856 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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