根据模板生成图片二维码和打包zip
dll下载链接
public ActionResult Index()
{
List<string> OBJ = new List<string>();
for (int i = 0; i < 10; i++)
{
string Qrpath = SendQrCode("/Images/QrCode1.png", "http://www.baidu.com", DateTime.Now.ToString("HHmmssfff") + ".png", 690, 690, 202, 507, "门店名称:", "商户编号", "联系电话");
OBJ.Add(Qrpath);
}
string address = Zip1(OBJ, "/Upload/" + 123 + ".zip", string.Empty);
return View();
}
#region 根据模板生成二维码
public string SendQrCode(string BackGroundImagePath, string QrCodeStr, string ImageFileName, int imgWidth, int imgHeight, int paddLeft, int paddTop, string MerchantName, string MerchantNoString, string MerchantLink)
{
string savepath = "/Upload/QrCode/";
if (!System.IO.Directory.Exists(Server.MapPath(savepath)))
{
System.IO.Directory.CreateDirectory(Server.MapPath(savepath));
}
Image NowImage;
if (string.IsNullOrEmpty(BackGroundImagePath))
{
NowImage = GCode(QrCodeStr);
}
else
{
NowImage = CombinImage3(Server.MapPath(BackGroundImagePath), GCode(QrCodeStr), imgWidth, imgHeight, paddLeft, paddTop, MerchantName, MerchantNoString, MerchantLink); ;
}
NowImage.Save(Server.MapPath(savepath + ImageFileName));
return "/Upload/QrCode/" + ImageFileName;
}
#endregion
#region 生成二维码
public static System.Drawing.Image GCode(String data)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale = 5;
qrCodeEncoder.QRCodeVersion = 7;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
var pbImg = qrCodeEncoder.Encode(data, System.Text.Encoding.UTF8);
var width = pbImg.Width / 10;
var dwidth = width * 2;
Bitmap bmp = new Bitmap(pbImg.Width + dwidth, pbImg.Height + dwidth);
Graphics g = Graphics.FromImage(bmp);
var c = System.Drawing.Color.White;
g.FillRectangle(new SolidBrush(c), 0, 0, pbImg.Width + dwidth, pbImg.Height + dwidth);
g.DrawImage(pbImg, width, width);
g.Dispose();
return bmp;
}
public static System.Drawing.Image CombinImage3(string imgBack, System.Drawing.Image destImg, int newW, int newH, int paddLeft, int paddTop, string MerchantName, string MerchantNoString, string MerchantLink)
{
System.Drawing.Image img = destImg;
if (img.Height != 100 || img.Width != 100)
{
img = KiResizeImage(img, newW, newH, 0);
}
if (imgBack == null)
{
return null;
}
System.Drawing.Image nowImgbakc = System.Drawing.Image.FromFile(imgBack);
if (IsPixelFormatIndexed(nowImgbakc.PixelFormat))
{
Bitmap bmp = new Bitmap(nowImgbakc.Width, nowImgbakc.Height, PixelFormat.Format32bppArgb);
Graphics gr = Graphics.FromImage(bmp);
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.DrawImage(nowImgbakc, 0, 0, nowImgbakc.Width, nowImgbakc.Height);
gr.DrawImage(img, paddLeft, paddTop, img.Width, img.Height);
gr.Flush();
GC.Collect();
return bmp;
}
else
{
Bitmap bmp = new Bitmap(nowImgbakc.Width, nowImgbakc.Height, PixelFormat.Format32bppArgb);
Graphics gr = Graphics.FromImage(bmp);
gr.DrawImage(nowImgbakc, 0, 0, nowImgbakc.Width, nowImgbakc.Height);
gr.DrawImage(img, paddLeft, paddTop, img.Width, img.Height);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
int size = 0;
int soure = 0;
if (nowImgbakc.Width > 1000)
{
soure = 25;
size = 20;
}
else
{
soure = 25;
size = 18;
}
int ContentWidht = MerchantName.Length * 40;
int FontBackgroundWidth = ContentWidht;
int MerchantNoContentWidht = MerchantNoString.Length * soure;
int MerchantNoFontBackgroundWidth = MerchantNoContentWidht;
int MerchantLinkContentWidht = MerchantLink.Length * soure;
int MerchantLinkFontBackgroundWidth = MerchantLinkContentWidht;
Color color = System.Drawing.ColorTranslator.FromHtml("#0080ff");
Brush brush = new SolidBrush(color);
gr.DrawString(MerchantName, new Font("微软雅黑", 30, FontStyle.Regular),
brush, new Rectangle(nowImgbakc.Width / 2 - (FontBackgroundWidth / 2), newH - 230, nowImgbakc.Width, 40), sf);
gr.DrawString(MerchantNoString, new Font("微软雅黑", 20, FontStyle.Regular),
Brushes.Black, new Rectangle(100, newH - 190, nowImgbakc.Width, 1543), sf);
gr.DrawString(MerchantLink, new Font("微软雅黑", 20, FontStyle.Regular),
Brushes.Black, new Rectangle(650, newH - 190, nowImgbakc.Width, 1543), sf);
gr.Flush();
GC.Collect();
return bmp;
}
}
public static System.Drawing.Image KiResizeImage(System.Drawing.Image bmp, int newW, int newH, int Mode)
{
try
{
System.Drawing.Image b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
{
return null;
}
}
private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)
{
foreach (PixelFormat pf in indexedPixelFormats)
{
if (pf.Equals(imgPixelFormat)) return true;
}
return false;
}
private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare,
PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed,
PixelFormat.Format8bppIndexed
};
#endregion
#region 压缩多个文件
public string Zip1(List<string> files, string ZipedFileName, string Password)
{
if (files.Count == 0) throw new FileNotFoundException("未找到指定打包的文件");
ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(Server.MapPath(ZipedFileName)));
s.SetLevel(6);
if (!string.IsNullOrEmpty(Password.Trim())) s.Password = Password.Trim();
ZipFileDictory(files, s);
s.Finish();
s.Close();
return ZipedFileName;
}
private void ZipFileDictory(List<string> files, ZipOutputStream s)
{
ZipEntry entry = null;
FileStream fs = null;
Crc32 crc = new Crc32();
try
{
entry = new ZipEntry("/");
s.PutNextEntry(entry);
s.Flush();
foreach (string file in files)
{
fs = System.IO.File.OpenRead(Server.MapPath(file));
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
entry = new ZipEntry("/" + Path.GetFileName(file));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
}
finally
{
if (fs != null)
{
fs.Close();
fs = null;
}
if (entry != null)
entry = null;
GC.Collect();
}
}
#endregion