using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BarcodeLib;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
using System.Drawing.Imaging;
using System.IO; namespace Utils
{
/// <summary>
/// 条形码/二维码帮助类
/// </summary>
public class CodeHelper
{
/// <summary>
/// 生成条形码
/// </summary>
/// <param name="Code"></param>
public static void CreateBarCode(string Code, string fileName)
{
BarcodeLib.Barcode barcode = new BarcodeLib.Barcode()
{
IncludeLabel = true,
Alignment = AlignmentPositions.CENTER,
Width = ,
Height = ,
RotateFlipType = RotateFlipType.RotateNoneFlipNone,
BackColor = Color.White,
ForeColor = Color.Black,
}; System.Drawing.Image img = barcode.Encode(TYPE.CODE128B, Code);
img.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
//using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
//{
//img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
//HttpContext.Current.Response.ClearContent();
//HttpContext.Current.Response.ContentType = "image/png";
//HttpContext.Current.Response.BinaryWrite(ms.ToArray());
//}
} /// <summary>
/// 生成二维码
/// </summary>
/// <param name="data">二维码内容</param>
/// <param name="filename">生成的图片路径名称(如:D:\123.png)</param>
/// <param name="moduleSize">图片大小</param>
/// <returns></returns>
public static void CreateQRCode(string data, string filename, int moduleSize = )
{
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
MemoryStream ms = new MemoryStream();
qrEncoder.TryEncode(data, out qrCode); //GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two));
//renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
//System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
//img.Save(filename, System.Drawing.Imaging.ImageFormat.Png); GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two), Brushes.Black, Brushes.White);
using (FileStream stream = new FileStream(filename, FileMode.Create))
{
render.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream);
}
} /// <summary>
/// 生成带Logo的二维码
/// </summary>
/// <param name="data"></param>
/// <param name="filename"></param>
/// <param name="logPath"></param>
/// <param name="moduleSize"></param>
public static void CreateQRCodeHasLog(string data, string filename, string logPath, int moduleSize = )
{
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
QrCode qrCode = qrEncoder.Encode(data);
//保存成png文件
GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two), Brushes.Black, Brushes.White); DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
Graphics g = Graphics.FromImage(map);
render.Draw(g, qrCode.Matrix);
//追加Logo图片 ,注意控制Logo图片大小和二维码大小的比例
System.Drawing.Image img = System.Drawing.Image.FromFile(logPath);
Point imgPoint = new Point((map.Width - img.Width) / , (map.Height - img.Height) / );
g.DrawImage(img, imgPoint.X, imgPoint.Y, img.Width, img.Height);
map.Save(filename, ImageFormat.Png);
}
}
}

C# CodeHelper的更多相关文章

  1. iBatis for Net 代码生成器(CodeHelper)附下载地址(已经升级为V 1.1)

    CodeHelper是一款可以自己定义模板和生成内容的代码生成器,目前只支持MsSql数据库,这款代码生成器的初衷也只是为了生成MyBatis.net框架的配置文件而写的一个轻量级的代码生成器. Co ...

  2. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(25)-权限管理系统-系统管理员(附生成器)

    系列目录 这一节我们要着手建立系统管理员表,但发布之前,我先发布一个代码生成器给大家先用着. 这个生成器是为这个项目而生的,理论不能用于其他项目,而且写得比较潦草,但能用 下载地址 有兴趣要生成器源码 ...

  3. 全自动迁移数据库的实现 (Fluent NHibernate, Entity Framework Core)

    在开发涉及到数据库的程序时,常会遇到一开始设计的结构不能满足需求需要再添加新字段或新表的情况,这时就需要进行数据库迁移. 实现数据库迁移有很多种办法,从手动管理各个版本的ddl脚本,到实现自己的mig ...

  4. 分享公司Basecode的用法

    主题 公司在basecode的用法上是比较有新意的,所以准备记录分享下公司的用法. 说明 basecode公司的一个主要用途就是用于一些基础的代码表,参数表的前台操作.这些表有很多,用spring d ...

  5. 使用 Entity Framework Core 时,通过代码自动 Migration

    一 介绍 在使用 Entity Framework Core (下面就叫 EF Core 吧)进行开发时,如果模型有变动,我们要在用 EF Core 提供的命令行工具进行手工迁移,然后再运行程序.但是 ...

  6. 手把手教你用动软.NET代码生成器实例教程

    动软实战攻略 手把手教你用动软 文档编号:20110421 版权所有 © 2004-2011 动软 在线帮助:http://help.maticsoft.com 目录   一.        产品介绍 ...

  7. Orm图解教程

    entity framework框架生成摘要文档为空(没有元数据文档可用)的bug解决方案 西安.王磊 2012-10-25 10:47 阅读:1234 评论:2   ORM for Net主流框架汇 ...

  8. 分享原创可复用且非侵入性代码生成工具(for .net)

    入行IT十年了,这是本人第一次网上’献丑‘.迫于工作压力,花了大半年时间写了这个生成器以辅助开发.如有价值请多多给予建议,谢谢 好了,废话少说,开动! QA. 1.为什么要用代码生成器? 当然是为了快 ...

  9. C# QRCode、DataMatrix和其他条形码的生成和解码软件

    今天制造了一个C#的软件,具体是用于生成二维码和条形码的,包括常用的QRCode.DataMatrix.Code128.EAN-8等等. 使用的第三方类库是Zxing.net和DataMatrix.n ...

随机推荐

  1. MVC4中使用Ninject

    MVC4中使用Ninject 1.NuGet获取Ninject.dll .NET技术交流群 199281001 .欢迎加入. 2.全局注册  Global.asax.cs  RegisterNinje ...

  2. EF的四种开发模式

    EF提供了四种开发模式,具体如下:(转载)Code First(New DataBase) :在代码中定义类和映射关系并通过model生成数据库,使用迁移技术更新数据库.Code First(Exis ...

  3. 根据首尾字节的tcp分包断包算法

    这个算是我的一点小总结吧,放出来分享给大家,原来在网上找这种算法都找了N久没找到,自己写也是走了许多弯路,就放出来遛一遛吧 大家将就这个看看, 这是其中的一个主要的方法,其余的我就不放出来了,其中的I ...

  4. 一个自己给自己挖的MVC的坑

    事情是这样的,我前几天突然想起没用过MVC提交之后刷新当前页面的方式更新View(一般情况是不会有人这么做的吧),我这人手快,就试了下,结果出现了个我想不明白的结果,Action代码如下: [Http ...

  5. DEBUG不能进断点 “exited with code -1073741515”

    需要把相关的DLL文件加入到项目对应的DEBUG路径下 IDE :Qt Creator 系统:Win7

  6. Visual Studio 2013发布Cloud Service至Azure China

    Visual Studio 2013发布Cloud Service至Azure China <Windows Azure Platform 系列文章目录> 之前有很多网友询问我如何通过VS ...

  7. ASP.NET MVC应用程序使用axd格式文件

    ASP.NET MVC应用程序使用axd格式文件 axd格式文件,不管是在asp.net还是现在开发asp.net MVC应用程序,都是Insus.NET较喜欢使用的. 因为我们可以虚拟一个在应用程序 ...

  8. 深入剖析Provider Model

    Membership三步曲之进阶篇 - 深入剖析Provider Model Membership 三步曲之进阶篇 - 深入剖析Provider Model 本文的目标是让每一个人都知道Provide ...

  9. C++反汇编与逆向分析技术揭秘

    C++反汇编-继承和多重继承   学无止尽,积土成山,积水成渊-<C++反汇编与逆向分析技术揭秘> 读书笔记 一.单类继承 在父类中声明为私有的成员,子类对象无法直接访问,但是在子类对象的 ...

  10. 一致性hash和虚拟节点

    consistent hashing 算法的原理 consistent hashing 是一种 hash 算法,简单的说,在移除 / 添加一个 cache 时,它能够尽可能小的改变已存在key 映射关 ...