【www.bbyears.com--图片处理】
带logo的二维码生成分为两步骤:首先根据输入的内容生成二维码图片,然后读取本地的logo图片,通过图片处理生成带logo的二维码。
生成的二维码效果如下:
下面直接贴出二维码生成类 QRCodeHelper.cs ,直接调用 CreateQRCodeWithLogo 方法,传入相应参数返回bitmap类型的数据,直接将返回的数据绑定到图片控件,如果是web可以先将图片保存到服务器指定地址在获取显示
代码如下///
/// 生成带logo二维码
///
publicclassQRCodeHelper
{///
/// 创建二维码
///
///
///
///
publicstaticBitmap Create(stringcontent)
{
try
{
//var options = new QrCodeEncodingOptions
//{
// DisableECI = true,
// CharacterSet = "UTF-8",
// Width = size,
// Height = size,
// Margin = 0,
// ErrorCorrection = ErrorCorrectionLevel.H
//};
//var writer = new BarcodeWriter();
//writer.Format = BarcodeFormat.QR_CODE;
//writer.Options = options;
//var bmp = writer.Write(content);
//return bmp;
QRCodeEncoder qRCodeEncoder =newQRCodeEncoder();
qRCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//设置二维码编码格式
qRCodeEncoder.QRCodeScale = 4;//设置编码测量度
qRCodeEncoder.QRCodeVersion = 7;//设置编码版本
qRCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//设置错误校验
Bitmap image = qRCodeEncoder.Encode(content);
returnimage;
}
catch(Exception ex)
{
returnnull;
}
}
///
/// 获取本地图片
///
///
///
privatestaticBitmap GetLocalLog(stringfileName)
{
Bitmap newBmp =newBitmap(fileName);
//Bitmap bmp = new Bitmap(newBmp);
returnnewBmp;
}
///
/// 生成带logo二维码
///
///
publicstaticBitmap CreateQRCodeWithLogo(stringcontent,stringlogopath)
{
//生成二维码
Bitmap qrcode = Create(content);
//生成logo
Bitmap logo = GetLocalLog(logopath);
ImageUtility util =newImageUtility();
Bitmap finalImage = util.MergeQrImg(qrcode, logo);
returnfinalImage;
}
}
下面是从网上找的图片处理类 ImageUtility.cs
代码如下publicclassImageUtility
{
#region 合并用户QR图片和用户头像
///
/// 合并用户QR图片和用户头像
///
///QR图片
///用户头像
///
///
publicBitmap MergeQrImg(Bitmap qrImg, Bitmap headerImg,doublen = 0.23)
{
intmargin = 10;
floatdpix = qrImg.HorizontalResolution;
floatdpiy = qrImg.VerticalResolution;
var _newWidth = (10 * qrImg.Width - 36 * margin) * 1.0f / 36;
var _headerImg = ZoomPic(headerImg, _newWidth / headerImg.Width);
//处理头像
intnewImgWidth = _headerImg.Width + margin;
Bitmap headerBgImg =newBitmap(newImgWidth, newImgWidth);
headerBgImg.MakeTransparent();
Graphics g = Graphics.FromImage(headerBgImg);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
Pen p =newPen(newSolidBrush(Color.White));
Rectangle rect =newRectangle(0, 0, newImgWidth - 1, newImgWidth - 1);
using(GraphicsPath path = CreateRoundedRectanglePath(rect, 1))
{
g.DrawPath(p, path);
g.FillPath(newSolidBrush(Color.White), path);
}
//画头像
Bitmap img1 =newBitmap(_headerImg.Width, _headerImg.Width);
Graphics g1 = Graphics.FromImage(img1);
g1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g1.Clear(Color.Transparent);
Pen p1 =newPen(newSolidBrush(Color.Gray));
Rectangle rect1 =newRectangle(0, 0, _headerImg.Width - 1, _headerImg.Width - 1);
using(GraphicsPath path1 = CreateRoundedRectanglePath(rect1, 1))
{
g1.DrawPath(p1, path1);
TextureBrush brush =newTextureBrush(_headerImg);
g1.FillPath(brush, path1);
}
g1.Dispose();
PointF center =newPointF((newImgWidth - _headerImg.Width) / 2, (newImgWidth - _headerImg.Height) / 2);
g.DrawImage(img1, center.X, center.Y, _headerImg.Width, _headerImg.Height);
g.Dispose();
Bitmap backgroudImg =newBitmap(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 =newPointF((qrImg.Width - headerBgImg.Width) / 2, (qrImg.Height - headerBgImg.Height) / 2);
g2.DrawImage(headerBgImg, center2);
g2.Dispose();
returnbackgroudImg;
}
#endregion
#region 图形处理
///
/// 创建圆角矩形
///
///区域
///圆角角度
///
privateGraphicsPath CreateRoundedRectanglePath(Rectangle rect,intcornerRadius)
{
//下午重新整理下,圆角矩形
GraphicsPath roundedRect =newGraphicsPath();
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();
returnroundedRect;
}
///
/// 图片按比例缩放
///
privateImage ZoomPic(Image initImage,doublen)
{
//缩略图宽、高计算
doublenewWidth = initImage.Width;
doublenewHeight = initImage.Height;
newWidth = n * initImage.Width;
newHeight = n * initImage.Height;
//生成新图
//新建一个bmp图片
System.Drawing.Image newImage =newSystem.Drawing.Bitmap((int)newWidth, (int)newHeight);
//新建一个画板
System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);
//设置质量
newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//置背景色
newG.Clear(Color.Transparent);
//画图
newG.DrawImage(initImage,newSystem.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height),newSystem.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);
newG.Dispose();
returnnewImage;
}
///
/// 创建缩略图
///
///
///
///
///
publicstaticBitmap GetThumbnail(Bitmap b,intdestHeight,intdestWidth)
{
System.Drawing.Image imgSource = b;
System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;
intsW = 0, sH = 0;
// 按比例缩放
intsWidth = imgSource.Width;
intsHeight = 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 =newBitmap(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,newRectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
g.Dispose();
// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams =newEncoderParameters();
long[] quality =newlong[1];
quality[0] = 100;
EncoderParameter encoderParam =newEncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
imgSource.Dispose();
returnoutBmp;
}
#endregion
}