本文整理汇总了C#中Renderer.CreateImageFile方法的典型用法代码示例。如果您正苦于以下问题:C# Renderer.CreateImageFile方法的具体用法?C# Renderer.CreateImageFile怎么用?C# Renderer.CreateImageFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Renderer
的用法示例。
在下文中一共展示了Renderer.CreateImageFile方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Generate
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="content">内容</param>
/// <param name="filePath">保存文件路径</param>
/// <param name="errorMessage">错误异常消息</param>
/// <returns></returns>
public static bool Generate(string content, string filePath, out string errorMessage)
{
errorMessage = string.Empty;
var qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
try
{
QrCode qrCode;
qrEncoder.TryEncode(content, out qrCode);
var renderer = new Renderer(5, Brushes.Black, Brushes.White);
if (!Directory.Exists(Path.GetDirectoryName(filePath)))
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
else
{
FileInfo fileInfo = new FileInfo(filePath);
if (fileInfo.Exists)
fileInfo.Delete();
}
renderer.CreateImageFile(qrCode.Matrix, filePath, ImageFormat.Png);
return true;
}
catch (Exception ex)
{
errorMessage = ex.Message;
return false;
}
}
示例2: GenerateQR
public static void GenerateQR(string encodeString, string venueName, string branding)
{
//Ask the user for the path where the QR pdf file will be stored. If canceled, return and do nothing.
string filePath = GetSaveFilePath();
if (filePath.Equals(""))
return;
QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode code = encoder.Encode(encodeString);
Renderer renderer = new Renderer(25, System.Drawing.Brushes.Black, System.Drawing.Brushes.White);
renderer.CreateImageFile(code.Matrix, IMAGE_NAME, ImageFormat.Png);
PrintToPDF(venueName, branding, filePath);
}
示例3: Generate
public ActionResult Generate(String longurl)
{
var jsonResp = new UrlShortenResponse{
Success = true
};
/*
* +--------------------------------------------------------------------------------+
* | Step 1: Check if the URL is valid. We will issue a web request and see the |
* | status code. |
* | Step 2: Check if this URL is already in the system. If exists, return |
* | We will check for the current user, since we will generate a unique |
* | URL for each user for a given original URL |
* | Step 3: Generate short URL. (May be check again if we generated a duplicate?) |
* | Step 4: Generate a QR code for the URL |
* | Step 5: Save generated short url and also save the QR image. |
* | Step 6: Send Json response back to the user. |
* +--------------------------------------------------------------------------------+
*/
#region Step 1 - Check for a valid URL
bool bValidUrl = true;
try
{
HttpWebRequest request = WebRequest.Create(longurl) as HttpWebRequest;
request.Method = "GET";
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
var response = request.GetResponse();
bValidUrl = response.Headers.Count > 0;
}
catch
{
bValidUrl = false;
}
if (!bValidUrl)
{
return Json(new UrlShortenResponse
{
Success = false,
Message = "Please check if the URL is correct and try again."
}, "text/html");
}
String protoPrefix = "http://";
String webUrl = longurl;
if (longurl.Contains("://"))
{
int iIndex = longurl.IndexOf("://");
protoPrefix = longurl.Substring(0, iIndex + 3);
webUrl = longurl.Substring(iIndex + 3);
}
#endregion
#region Step 2-5 - Generate URL and save
UrlMap url = _UrlMapDataSource.FindSingleUrlByUserName(longurl, User.Identity.Name);
if (url == null)
{
url = new UrlMap();
url.OriginalUrl = longurl;
url.DateCreated = DateTime.UtcNow;
url.IsActive = true;
//Step 3:
//Now generate hash for the longUrl;
uint hash = FNVHash.fnv_32a_str(webUrl + User.Identity.Name);
//Convert hash to base36
url.ShortUrlCode = Base36Converter.Encode(hash);
url.ShortUrl = String.Format("{0}{1}", _URLShortenerHost, url.ShortUrlCode);
//Step 4:
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
qrEncoder.TryEncode(url.ShortUrl, out qrCode);
Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);
String fileName = String.Format("{0}{1}.qr", AppDomain.CurrentDomain.BaseDirectory, url.ShortUrlCode);
renderer.CreateImageFile(qrCode.Matrix, fileName, ImageFormat.Png);
jsonResp.QRCodeUrl = String.Format("{0}Home/QRImage/{1}.qr", _URLShortenerHost, url.ShortUrlCode);
//Save Url Map
_UrlMapDataSource.AddUrlMap(url, User.Identity.Name);
//Save method appends the .qr extension
_UrlMapDataSource.SaveQRCode(fileName, url.ShortUrlCode);
}
#endregion
// Step 6
jsonResp.Url = url.ShortUrl;
jsonResp.QRCodeUrl = String.Format("{0}Home/QRImage/{1}.qr", _URLShortenerHost, url.ShortUrlCode);
return Json(jsonResp, "text/html");
}