c#裁剪图片后使用zxing生成二维码示例分享
发布时间:2021-09-08 17:06:58
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="fileName">生成二维码路径</param>
/// <param name="url">生成的内容</param>
/// <param name="width">二维码宽</param>
/// <param name="height">二维码高</param>
/// <param name="userFace">需生成的Logo图片</param>
/// <returns></returns>
private Bitmap GetCodeImgUrl(string fileName, string url, int width, int height, string userFace)
{
BarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Renderer = new BitmapRenderer
{
Foreground = Color.Black
},
Options = new ZXing.QrCode.QrCodeEncodingOptions
{
DisableECI = true,
Height = height,
Width = width,
Margin = 0,
CharacterSet = "UTF-8",
ErrorCorrection = ErrorCorrectionLevel.M
}
};
Bitmap bitmap = writer.Write(url);
if (!string.IsNullOrEmpty(userFace))
{
Bitmap bits = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(userFace);
if (bits != null)
{
//剪裁一个80*80的Logo图片
ImageCut img = new ImageCut(0, 0, 80, 80);
System.Drawing.Bitmap icon = img.KiCut(bits);
//userFace_b.jpg是一个边框的图片
Bitmap bits2 = new System.Drawing.Bitmap((System.Drawing.Bitmap)System.Drawing.Image.FromFile(Application.StartupPath + "/user/userFace_b.jpg"), 84, 84);
if (icon != null)
{
try
{
//画了2个边框,一个是logo,一个在logo周围加了一个边框
using (var graphics = System.Drawing.Graphics.FromImage(bitmap))
{
graphics.DrawImage(bits2, (bitmap.Width - bits2.Width) / 2, (bitmap.Height - bits2.Height) / 2);
graphics.DrawImage(icon, (bitmap.Width - icon.Width) / 2, (bitmap.Height - icon.Height) / 2);
}
}
catch (Exception ex)
{
}
finally
{
icon.Dispose();
GC.Collect();
}
}
bitmap.Save(fileName, ImageFormat.Jpeg);
}
}
return bitmap;
}
public class ImageCut
{
/// <summary>
/// 剪裁 -- 用GDI+
/// </summary>
/// <param name="b">原始Bitmap</param>
/// <param name="StartX">开始坐标X</param>
/// <param name="StartY">开始坐标Y</param>
/// <param name="iWidth">宽度</param>
/// <param name="iHeight">高度</param>
/// <returns>剪裁后的Bitmap</returns>
public Bitmap KiCut(Bitmap b)
{
if (b == null)
{
return null;
}
int w = b.Width;
int h = b.Height;
int intWidth = 0;
int intHeight = 0;
if (h * Width / w > Height)
{
intWidth = Width;
intHeight = h * Width / w;
}
else if (h * Width / w < Height)
{
intWidth = w * Height / h;
intHeight = Height;
}
else
{
intWidth = Width;
intHeight = Height;
}
Bitmap bmpOut_b = new System.Drawing.Bitmap(b, intWidth, intHeight);
w = bmpOut_b.Width;
h = bmpOut_b.Height;
if (X >= w || Y >= h)
{
return null;
}
if (X + Width > w)
{
Width = w - X;
}
else
{
X = (w-Width) / 2;
}
if (Y + Height > h)
{
Height = h - Y;
}
try
{
Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(bmpOut_b, new Rectangle(0, 0, Width, Height), new Rectangle(X, Y, Width, Height), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}
public int X = 0;
public int Y = 0;
public int Width = 120;
public int Height = 120;
public ImageCut(int x, int y, int width, int heigth)
{
X = x;
Y = y;
Width = width;
Height = heigth;
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
string UserId = "1245460396";
string curFilePath = "/user/";
string curFileName_b = "DimensionalPig_" + UserId + "_b";
string path = Application.StartupPath + curFilePath;
if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
{
Directory.CreateDirectory(path);
}
string fileName_b = Application.StartupPath + curFilePath + "/" + curFileName_b + ".jpg";//获得上传文件名
string UserUrl = string.Format("https://www.jb51.net/u{0}", UserId.Trim());
string userFace_b = Application.StartupPath + "/user/" + UserId + "_b.jpg";
Bitmap bitmap_b = GetCodeImgUrl(fileName_b.Replace("_b.", "_b_ewm."), UserUrl, 400, 400, userFace_b);
this.p.Image =(System.Drawing.Image)bitmap_b;
this.p.Image.Save(fileName_b.Replace("_b.", "_b_ewm."));


猜你喜欢
- Maven 多profile及指定编译要点项目A依赖项目B,项目A、B都有对应的多个profile,通过mvn –P参数指定profile,
- View的平滑滚动效果什么是实现View的平滑滚动效果呢,举个简单的例子,一个View从在我们指定的时间内从一个位置滚动到另外一个位置,我们
- 本文实例讲述了C#简单遍历指定文件夹中所有文件的方法。分享给大家供大家参考,具体如下:C#遍历指定文件夹中的所有文件:DirectoryIn
- 昨天弄了一天的Android Studio svn,感觉没有eclipse的svn好装,中间遇到很多的麻烦问题。这里来记录下吧下载下来的时候
- 前台form 表单:设置method=post,enctype=multipart/form-data。struts2在原有的上传解析器继承
- 目录一、二叉树的顺序存储1.堆的存储方式2.下标关系二、堆(heap)1.概念2.大/小 根堆2.1小根堆2.2大根堆3.建堆操作3.1向下
- 编写规范目的:能够在编码过程中实现规范化,为以后的程序开发中养成良好的行为习惯。1、 项目名全部小写2、 包名全部小写3、 类名首字母大写,
- 可空类型用途主要是从数据库读取数据有可能为空,而不是插入使用,插入数据都要进行验证,如果要插入数据库的null,则使用DBNull.valu
- 前言SpringBoot是Spring的包装,通过自动配置使得SpringBoot可以做到开箱即用,上手成本非常低,但是学习其实现原理的成本
- 1. 前言本文主要是介绍一下RocketMQ消息生产者在发送消息的时候发送失败的问题处理?这里有两个点,一个是关于消息的处理,一个是关于br
- 继承和多态派生类具有基类所有非私有数据和行为以及新类自己定义的所有其他数据或行为,即子类具有两个有效类型:子类的类型和它继承的基类的类型。对
- 本文实例讲述了C#使用StopWatch获取程序毫秒级执行时间的方法。分享给大家供大家参考。具体分析如下:这个比时间通过DateTime在程
- 一、前言随着 JDK 1.8 Streams API 的发布,使得 HashMap 拥有了更多的遍历的方式,但应该选择那种遍历方式?反而成了
- 因为在framework中想添加这个功能,所以写了个appliction来实现一下获取正在运行的应用程序: 还是先看图吧: 这个app主要是
- 在上一篇文章:Flutter进阶—实现动画效果(二)的最后,我们实现了一个控件,其中包含各种布局和状态处理控件。以及使用自定义的动画感知绘图
- 开发过程中经常遇到需要用某些http://maven.apache.org/中没有的jar包,这个时候可以用maven命令自己添加通常这些j
- 点击按钮,先自动进行下拉刷新,也可以手动刷新,刷新完后,最后就多一行数据。有四个选项卡。前两天导师要求做一个给本科学生预定机房座位的app,
- 本文实例讲述了Android实现图片轮播效果代码,分享给大家供大家参考。具体如下:运行效果截图如下:具体代码如下:首先看下一下布局文件:&l
- 项目中很多时候需要读取自定义配置文件,本地开发工具怎么写都成功但是部署到服务其上就出现问题,异常BOOT-INF/classes!/conf
- 一、效果图 二、RippleDrawable基本概念介绍 (1)、RippleDrawableRippleDrawable可以实