.net 两个图片合成,图片添加文字,图片尺寸压缩

public class QRCodePoster
{
int qrX = 350;
int qrY = 480;
int qrWidth = 170;
int qrHeight = 170;
int fontX = 350;
int fontY = 670;
int fontSize = 50;
string rootPath = "";
/// <summary>
///
/// </summary>
/// <param name="qrx">二维码横坐标</param>
/// <param name="qry">二维码纵坐标</param>
/// <param name="qrwidth">二维码宽度</param>
/// <param name="qrheight">二维码高度</param>
/// <param name="fontx">文字横坐标</param>
/// <param name="fonty">文字纵坐标</param>
/// <param name="fontsize">文字字体大小</param>
public QRCodePoster(int qrx, int qry, int qrwidth, int qrheight, int fontx, int fonty, int fontsize, string rootPath)
{
qrX = qrx;
qrY = qry;
qrWidth = qrwidth;
qrHeight = qrheight;
fontX = fontx;
fontY = fonty;
fontSize = fontsize;
this.rootPath = rootPath;
}


ChangeImgSize changesize = new ChangeImgSize();

/// <summary>
/// 合成图片方法
/// </summary>
/// <param name="qrPath">二维码路径</param>
/// <param name="customId">合成图片名称</param>
/// <param name="textName">需要添加的文字内容</param>
/// <returns></returns>
public string CreatePoster(string qrPath, string imgName, string textName)
{

//string qrPath = "D://qrcode//" + customId + ".jpg";
if (!File.Exists(qrPath))
{
LogHelper.Instance.Error("找不到qrPath路径:" + qrPath);
return "";
}
//改变二维码尺寸后保存路径
if (!Directory.Exists(Path.Combine(rootPath, "images//newqrcode")))
{
Directory.CreateDirectory(Path.Combine(rootPath, "images//newqrcode"));
}

string newQrPath = qrPath.Replace("qrcode", "newqrcode");
changesize.ResizePic(qrPath, newQrPath, qrWidth, qrHeight);

string newPath = GetSchoolPoster(qrPath, newQrPath, textName, imgName);

return newPath;
}

 

public string GetSchoolPoster(string logoPath, string ewmPath, string textName, string customId)
{
try
{
// string rootStr = AppDomain.CurrentDomain.BaseDirectory;

string mainPath = Path.Combine(rootPath, "images//main", "main.jpg");
// string mainPath = Path.Combine(rootPath, surl);

string newPoster = customId + ".jpg";
if (!Directory.Exists(Path.Combine(rootPath, "images//poster")))
{
Directory.CreateDirectory(Path.Combine(rootPath, "images//poster"));
}
var newPath = Path.Combine(rootPath, "images//poster", newPoster);

using (var poster = new PosterGenerator(mainPath))
{
// poster.AddImage(rootStr + logoPath, 20, 20);
poster.AddImage(ewmPath, qrX, qrY);
poster.AddText(textName, fontSize, fontX, fontY);
poster.Save(newPath);
}
return Path.Combine("images//poster", newPoster);
}
catch (Exception ex)
{
LogHelper.Instance.Error("合成图片异常:" + ex.Message, ex);
string ss = ex.Message;
}


return "";
}

}

 

public class PosterGenerator : IDisposable
{

public PosterGenerator(string srcImgPath)
{
this.SrcImg = Image.FromFile(srcImgPath);
this.Graphics = Graphics.FromImage(SrcImg);

}

public Image SrcImg { get; set; }
public Graphics Graphics { get; set; }

//添加二维码图片
public void AddImage(string Path_sypf, int x = 0, int y = 0)
{
using (Image waterMark = Image.FromFile(Path_sypf))
{

this.Graphics.DrawImage(waterMark, new Rectangle(x, y, waterMark.Width, waterMark.Height), 0, 0, waterMark.Width, waterMark.Height, GraphicsUnit.Pixel);
}
}

/// <summary>
///添加文字
/// </summary>
/// <param name="logoText"></param>
public void AddText(string logoText, int size = 50, int left = 350, int top = 670)
{
this.Graphics.DrawString(logoText, new Font("SimHei", size), new SolidBrush(Color.White), left, top);
}


public void AddText2(string logoText, int size = 10, int left = 20, int top = 20)
{
this.Graphics.DrawString(logoText, new Font("SimHei", size), new SolidBrush(Color.White), left, top);
}

public void Save(string savePath)
{
this.SrcImg.Save(savePath);
}

public void Dispose()
{
if (this.SrcImg != null) this.SrcImg.Dispose();
if (this.Graphics != null) this.Graphics.Dispose();
}
}

 

public class ChangeImgSize
{
public string ResizePic(string oldPath, string newPath, int maxWidth = 170, int maxHeight = 170)
{
#region 压缩图片开始
string filePath = ""; //文件路径

// int maxWidth = 600; //图片宽度最大限制
// int maxHeight = 400; //图片高度最大限制
System.Drawing.Image imgPhoto =
System.Drawing.Image.FromFile(oldPath);
int imgWidth = imgPhoto.Width;
int imgHeight = imgPhoto.Height;
if (imgWidth > imgHeight) //如果宽度超过高度以宽度为准来压缩
{
if (imgWidth > maxWidth) //如果图片宽度超过限制
{
float toImgWidth = maxWidth; //图片压缩后的宽度
float toImgHeight = imgHeight / (float)(imgWidth / toImgWidth); //图片压缩后的高度
System.Drawing.Bitmap img = new System.Drawing.Bitmap(imgPhoto,
int.Parse(toImgWidth.ToString()),
int.Parse(toImgHeight.ToString()));
string strResizePicName = newPath;
img.Save(newPath); //保存压缩后的图片
filePath = newPath; //返回压缩后的图片路径
}
}
else
{
if (imgHeight > maxHeight)
{
float toImgHeight1 = maxHeight;
float toImgWidth1 = imgWidth / (float)(imgHeight / toImgHeight1);

System.Drawing.Bitmap img = new System.Drawing.Bitmap(imgPhoto,
int.Parse(toImgWidth1.ToString()),
int.Parse(toImgHeight1.ToString()));
//string strResizePicName = Server.MapPath(strImgPath) + filePathName + "/_small_" + fileSysName;
img.Save(newPath);
filePath = newPath; //strImgPath + filePathName + "/_small_" + fileSysName;
}
}

return filePath;
#endregion
}


}

 

posted @ 2019-09-24 17:22  Mr.Alpha  阅读(352)  评论(0编辑  收藏  举报