用webapi做一个生成二维码的接口,可是无法保存到本地,运行程序则是一直显示正在运行
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Drawing;
using System.Drawing.Imaging;
using ZXing;
using ZXing.QrCode;
using ZXing.Common;
using ZXing.QrCode.Internal;
using MvcApplication.Models;
namespace MvcApplication.Controllers
{
public class BarCodeController : Controller
{
/// <summary>
/// 生成二维码图片
/// </summary>
/// <param name="contents">要生成二维码包含的信息</param>
/// <param name="width">生成的二维码宽度(默认300像素)</param>
/// <param name="height">生成的二维码高度(默认300像素)</param>
/// <returns>二维码图片</returns>
public static Bitmap GeneratorQrImage(string msg, int width = 300, int height = 300)
{
msg = "a";
if (string.IsNullOrEmpty(msg))
{
return null;
}
EncodingOptions options = null;
BarcodeWriter writer = null;
options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = width,
Height = height,
ErrorCorrection = ErrorCorrectionLevel.H,
};
writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
Bitmap bitmap = null;
try
{
bitmap = writer.Write(msg);
string filename = @"D:\" + msg + ".png";
bitmap.Save(filename, ImageFormat.Png);
}
catch (Exception ex)
{
Console.WriteLine("捕获异常");
}
finally
{
bitmap.Dispose();
}
return bitmap;
}
}
}