条形码和二维码生成的DLL下载地址:
https://download.csdn.net/download/weixin_42415843/11811819
没有积分的朋友请留言留下微信或者QQ 或者邮箱 我看到会发给你
二维码生成:
///
/// 创建二维码
///
/// 内容
///
public static Bitmap CreateQRCode(string content)
{
try
{
QRCodeEncoder qrEncoder = new QRCodeEncoder();
//二维码类型
qrEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
//二维码尺寸
qrEncoder.QRCodeScale = 4;
//二维码版本
qrEncoder.QRCodeVersion = 7;
//二维码容错程度
qrEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
//字体与背景颜色
qrEncoder.QRCodeBackgroundColor = Color.White;
qrEncoder.QRCodeForegroundColor = Color.Black;
//UTF-8编码类型
Bitmap qrcode = qrEncoder.Encode(content, Encoding.UTF8);
return qrcode;
}
catch (Exception)
{
return null;
}
}
///
/// 生成带logo二维码
///
///
public static Bitmap CreateQRCodeWithLogo(string content, string logopath)
{
//生成二维码
Bitmap qrcode = CreateQRCode(content);
//生成logo
Bitmap logo = new Bitmap(logopath);
//合成
ImageUtility util = new ImageUtility();
Bitmap finalImage = util.MergeQrImg(qrcode, logo);
return finalImage;
}
ImageUtility类:
class ImageUtility
{
#region 合并用户QR图片和用户头像
///
/// 合并用户QR图片和用户头像
///
/// QR图片
/// 用户头像
///
///
public Bitmap MergeQrImg(Bitmap qrImg, Bitmap headerImg, double n = 0.23)
{
int margin = 10;
float dpix = qrImg.HorizontalResolution;
float dpiy = qrImg.VerticalResolution;
var _newWidth = (10 * qrImg.Width - 36 * margin) * 1.0f / 36;
var _headerImg = ZoomPic(headerImg, _newWidth / headerImg.Width);
//处理头像
int newImgWidth = _headerImg.Width + margin;
Bitmap headerBgImg = new Bitmap(newImgWidth, newImgWidth);
headerBgImg.MakeTransparent();
Graphics g = Graphics.FromImage(headerBgImg);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
Pen p = new Pen(new SolidBrush(Color.White));
Rectangle rect = new Rectangle(0, 0, newImgWidth - 1, newImgWidth - 1);
using (GraphicsPath path = CreateRoundedRectanglePath(rect, 1))
{
g.DrawPath(p, path);
g.FillPath(new SolidBrush(Color.White), path);
}
//画头像
Bitmap img1 = new Bitmap(_headerImg.Width, _headerImg.Width);
Graphics g1 = Graphics.FromImage(img1);
g1.InterpolationMode = InterpolationMode.HighQualityBicubic;
g1.SmoothingMode = SmoothingMode.HighQuality;
g1.Clear(Color.Transparent);
Pen p1 = new Pen(new SolidBrush(Color.Gray));
Rectangle rect1 = new Rectangle(0, 0, _headerImg.Width - 1, _headerImg.Width - 1);
using (GraphicsPath path1 = CreateRoundedRectanglePath(rect1, 1))
{
g1.DrawPath(p1, path1);
TextureBrush brush = new TextureBrush(_headerImg);
g1.FillPath(brush, path1);
}
g1.Dispose();
PointF center = new PointF((newImgWidth - _headerImg.Width) / 2, (newImgWidth - _headerImg.Height) / 2);
g.DrawImage(img1, center.X, center.Y, _headerImg.Width, _headerImg.Height);
g.Dispose();
Bitmap backgroudImg = new Bitmap(qrImg.Width, qrImg.Height);
backgroudImg.MakeTransparent();
backgroudImg.SetResolution(dpix, dpiy);
headerBgImg.SetResolution(dpix, dpiy);
Graphics g2 = Graphics.FromImage(backgroudImg);
g2.Clear(Color.Transparent);
g2.DrawImage(qrImg, 0, 0);
PointF center2 = new PointF((qrImg.Width - headerBgImg.Width) / 2, (qrImg.Height - headerBgImg.Height) / 2);
g2.DrawImage(headerBgImg, center2);
g2.Dispose();
return backgroudImg;
}
#endregion
#region 图形处理
///
/// 创建圆角矩形
///
/// 区域
/// 圆角角度
///
private GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
{
//下午重新整理下,圆角矩形
GraphicsPath roundedRect = new GraphicsPath();
roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
roundedRect.CloseFigure();
return roundedRect;
}
///
/// 图片按比例缩放
///
private Image ZoomPic(Image initImage, double n)
{
//缩略图宽、高计算
double newWidth = initImage.Width;
double newHeight = initImage.Height;
newWidth = n * initImage.Width;
newHeight = n * initImage.Height;
//生成新图
//新建一个bmp图片
Image newImage = new Bitmap((int)newWidth, (int)newHeight);
//新建一个画板
Graphics newG = Graphics.FromImage(newImage);
//设置质量
newG.InterpolationMode = InterpolationMode.HighQualityBicubic;
newG.SmoothingMode = SmoothingMode.HighQuality;
//置背景色
newG.Clear(Color.Transparent);
//画图
newG.DrawImage(initImage, new Rectangle(0, 0, newImage.Width, newImage.Height), new Rectangle(0, 0, initImage.Width, initImage.Height), GraphicsUnit.Pixel);
newG.Dispose();
return newImage;
}
///
/// 创建缩略图
///
///
///
///
///
public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth)
{
Image imgSource = b;
ImageFormat thisFormat = imgSource.RawFormat;
int sW = 0, sH = 0;
// 按比例缩放
int sWidth = imgSource.Width;
int sHeight = imgSource.Height;
if (sHeight > destHeight || sWidth > destWidth)
{
if ((sWidth * destHeight) > (sHeight * destWidth))
{
sW = destWidth;
sH = (destWidth * sHeight) / sWidth;
}
else
{
sH = destHeight;
sW = (sWidth * destHeight) / sHeight;
}
}
else
{
sW = sWidth;
sH = sHeight;
}
Bitmap outBmp = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage(outBmp);
g.Clear(Color.Transparent);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
g.Dispose();
// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
imgSource.Dispose();
return outBmp;
}
#endregion
}
条形码生成:
public static Image GenerateBarCodeBitmap(string content)
{
using (var barcode = new Barcode()
{
IncludeLabel = true,
Alignment = AlignmentPositions.CENTER,
Width = 250,
Height = 100,
RotateFlipType = RotateFlipType.RotateNoneFlipNone,
BackColor = Color.White,
ForeColor = Color.Black,
})
{
return barcode.Encode(TYPE.CODE128B, content);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.pbBarcode.Image = GenerateBarCodeBitmap("123456789");
}