个性二维码开源专题<前背景>
//设置图片资源
private Image imgAgo; public override void SetParam() { base.SetParam(); // 读取前背景 string _imagePath = Environment.CurrentDirectory + @"\QrCodeModel\Images\Angry_Birds\boom.png"; imgAgo = Image.FromFile(_imagePath); }
//在生成二维码之后,加入前背景图层
//TODO:一系列个性二维码生成方案 public override Bitmap Encode(string content) { try { matrix = QrCodeEncoder.calQrcode(EnCoding.GetBytes(content)); } catch { throw new Exception("内容超出范围,请选择更高版本或者降低容错率"); } this.SetParam(); SolidBrush Backbrush = new SolidBrush(QrCodeEncoder.QRCodeBackgroundColor); //SolidBrush Backbrush = new SolidBrush(Color.Transparent);//背景透明 SolidBrush Forebrush = new SolidBrush(QrCodeEncoder.QRCodeForegroundColor); Bitmap image = new Bitmap(this.QrCodeW, this.QrCodeH); Graphics g = Graphics.FromImage(image); Rectangle rect = new Rectangle(); g.FillRectangle(Backbrush, new Rectangle(0, 0, image.Width, image.Height)); for (int i = 0; i < matrix.Length; i++) { for (int j = 0; j < matrix.Length; j++) { rect = new Rectangle((j + this.SpacingW) * QrCodeEncoder.QRCodeScale, (i + this.SpacingH) * QrCodeEncoder.QRCodeScale, QrCodeEncoder.QRCodeScale, QrCodeEncoder.QRCodeScale); if (matrix[j][i]) { ChangeFillShape(g, Forebrush, rect, EN_FillShape.FillRectangle, new FillShape(), Backbrush); } else ChangeFillShape(g, Backbrush, rect, EN_FillShape.FillRectangle, new FillShape(), Backbrush); } } //前背景 GraphicsPath _path = ImageFix.GetWindowRegion(image, QrCodeEncoder.QRCodeForegroundColor); TextureBrush texture1 = new TextureBrush(new Bitmap(imgAgo, new Size(image.Width, image.Height))); g.FillPath(texture1, _path); return image; }
//还可以设置,LOGO,头像等等
//TODO:一系列个性二维码生成方案 public override Bitmap Encode(string content) { try { matrix = QrCodeEncoder.calQrcode(EnCoding.GetBytes(content)); } catch { throw new Exception("内容超出范围,请选择更高版本或者降低容错率"); } this.SetParam(); SolidBrush Backbrush = new SolidBrush(QrCodeEncoder.QRCodeBackgroundColor); //SolidBrush Backbrush = new SolidBrush(Color.Transparent);//背景透明 SolidBrush Forebrush = new SolidBrush(QrCodeEncoder.QRCodeForegroundColor); Bitmap image = new Bitmap(this.QrCodeW, this.QrCodeH); Graphics g = Graphics.FromImage(image); Rectangle rect = new Rectangle(); g.FillRectangle(Backbrush, new Rectangle(0, 0, image.Width, image.Height)); for (int i = 0; i < matrix.Length; i++) { for (int j = 0; j < matrix.Length; j++) { rect = new Rectangle((j + this.SpacingW) * QrCodeEncoder.QRCodeScale, (i + this.SpacingH) * QrCodeEncoder.QRCodeScale, QrCodeEncoder.QRCodeScale, QrCodeEncoder.QRCodeScale); if (matrix[j][i]) { ChangeFillShape(g, Forebrush, rect, EN_FillShape.FillRectangle, new FillShape(), Backbrush); } else ChangeFillShape(g, Backbrush, rect, EN_FillShape.FillRectangle, new FillShape(), Backbrush); } } //Logo Image _logoImg = imgAgo; g.DrawImage(_logoImg, (this.QrCodeW - this.QrCodeW / 4) / 2, (this.QrCodeH - this.QrCodeH / 4) / 2, this.QrCodeW / 4, this.QrCodeH / 4);//插入头像 return image; }
//为了可印刷名片、图册,可以去色
//Logo Image _logoImg = imgAgo; _logoImg = ImageFix.Blocks((Bitmap)_logoImg); _logoImg = ImageFix.ColorReplace((Bitmap)_logoImg, Color.Black, QrCodeEncoder.QRCodeForegroundColor); g.DrawImage(_logoImg, (this.QrCodeW - this.QrCodeW / 4) / 2, (this.QrCodeH - this.QrCodeH / 4) / 2, this.QrCodeW / 4, this.QrCodeH / 4);//插入头像 return image;
//还可以自由配色
//Logo Image _logoImg = imgAgo; _logoImg = ImageFix.Blocks((Bitmap)_logoImg); _logoImg = ImageFix.ColorReplace((Bitmap)_logoImg, Color.Black, Color.Blue); g.DrawImage(_logoImg, (this.QrCodeW - this.QrCodeW / 4) / 2, (this.QrCodeH - this.QrCodeH / 4) / 2, this.QrCodeW / 4, this.QrCodeH / 4);//插入头像
以下是开源地址,国外github,国内oschina.net
oschina.net:
http://git.oschina.net/cheng5x/Yc.QrCode
github:
https://github.com/cheng5x/YcQrCode
码晒客讨论QQ群:
28629273
----------------------------
用心做产品 name:5+x
----------------------------
用心做产品 name:5+x
----------------------------
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端