///
/// 更换小程序码中间的logo
///
/// 微信接口返回生成的小程序码路径
/// 拼接进去的图片路径
/// 保存路径
///
public string WxQrCodeDemo(string wxQrCodePath, string storeLogo, string savePath)
{
Image mImage = Image.FromFile(wxQrCodePath);
using (Bitmap bitmap = new Bitmap(mImage))
{
Rectangle rec = new Rectangle();
rec.X = 75;
rec.Y = 75;
rec.Width = 130;
rec.Height = 130;
using (Graphics g = Graphics.FromImage(bitmap))
{
Pen pen = new Pen(Color.White);
g.DrawEllipse(pen, rec);
Brush b = new SolidBrush(Color.White);
g.FillPath(b, GetRoundRectangle(rec, 65));//中间画白色圆
}
bitmap.Save(wxQrCodePath, System.Drawing.Imaging.ImageFormat.Jpeg);//注意保存路径(模板文件)
}
mImage.Dispose();
Image regionImage = Image.FromFile(wxQrCodePath);//模板文件
Image addImg = Image.FromFile(storeLogo);//需要拼接进去的图片
Image lastImg = CombinImage(regionImage, addImg, 135);//拼接图片(设置固定的135px-可根据需要改)
Bitmap lastmap = new Bitmap(lastImg);
lastmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Png);//注意保存路径
addImg.Dispose();
lastmap.Dispose();
regionImage.Dispose();
lastImg.Dispose();
return savePath;
}
///
/// 根据普通矩形得到圆角矩形的路径
///
/// 原始矩形
/// 半径
///
private static GraphicsPath GetRoundRectangle(Rectangle rectangle, int r)
{
int l = 2 * r;
// 把圆角矩形分成八段直线、弧的组合,依次加到路径中
GraphicsPath gp = new GraphicsPath();
gp.AddLine(new Point(rectangle.X + r, rectangle.Y), new Point(rectangle.Right - r, rectangle.Y));
gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F);
gp.AddLine(new Point(rectangle.Right, rectangle.Y + r), new Point(rectangle.Right, rectangle.Bottom - r));
gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Bottom - l, l, l), 0F, 90F);
gp.AddLine(new Point(rectangle.Right - r, rectangle.Bottom), new Point(rectangle.X + r, rectangle.Bottom));
gp.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - l, l, l), 90F, 90F);
gp.AddLine(new Point(rectangle.X, rectangle.Bottom - r), new Point(rectangle.X, rectangle.Y + r));
gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F);
return gp;
}
///
/// 获取缩小后的图片
///
/// 要缩小的图片
/// 要缩小的倍数
///
private Bitmap GetSmall(Bitmap bm, double times)
{
int nowWidth = (int)(bm.Width / times);
int nowHeight = (int)(bm.Height / times);
Bitmap newbm = new Bitmap(nowWidth, nowHeight);//新建一个放大后大小的图片
if (times >= 1 && times <= 1.1)
{
newbm = bm;
}
else
{
Graphics g = Graphics.FromImage(newbm);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawImage(bm, new Rectangle(0, 0, nowWidth, nowHeight), new Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel);
g.Dispose();
}
return newbm;
}
///
/// 调用此函数后使此两种图片合并,类似相册,有个
/// 背景图,中间贴自己的目标图片
///
/// 粘贴的源图片
/// 粘贴的目标图片
public Image CombinImage(Image imgBack, Image img, int r)
{
Bitmap imgMap = new Bitmap(img);
double smallTimes = imgMap.Width / r;//缩小图片倍数
img = GetSmall(imgMap, smallTimes);//进行图片缩小
if ((img.Width
if (img.Width>img.Height)
{
r = img.Height;
}
else
{
r = img.Width;
}
}
img = CutEllipse(img, new Rectangle(0, 0, r, r), new Size(r, r));
Graphics g = Graphics.FromImage(imgBack);
g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);
g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height);
GC.Collect();
g.Dispose();
img.Dispose();
return imgBack;
}
效果如下:
参考:https://www.cnblogs.com/cyj0923/p/9511030.html
}