.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
}


}

 

转载于:https://www.cnblogs.com/xiaoyao123/p/11579539.html

// AddTextDlg.cpp : implementation file // #include "stdafx.h" #include "Test.h" #include "AddTextDlg.h" #include ".\addtextdlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAddTextDlg dialog CAddTextDlg::CAddTextDlg(CWnd* pParent /*=NULL*/) : CDialog(CAddTextDlg::IDD, pParent) { //{{AFX_DATA_INIT(CAddTextDlg) m_strText = _T(""); //}}AFX_DATA_INIT m_strBmpFilePath = ""; ZeroMemory(&m_bmpIfHi, sizeof(BITMAPINFOHEADER)); m_dwSize = 0; m_lpDIBits = NULL; } void CAddTextDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAddTextDlg) DDX_Control(pDX, IDC_EDIT_TEXT, m_edtText); DDX_Control(pDX, IDC_STATIC_SHOW, m_BmpShow); DDX_Text(pDX, IDC_EDIT_TEXT, m_strText); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAddTextDlg, CDialog) //{{AFX_MSG_MAP(CAddTextDlg) ON_EN_CHANGE(IDC_EDIT_TEXT, OnChangeEditText) //}}AFX_MSG_MAP ON_BN_CLICKED(IDOK, OnBnClickedOk) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CAddTextDlg message handlers BOOL CAddTextDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here m_edtText.EnableWindow(FALSE); int nCharLimit = 0; if (m_strBmpFilePath.GetLength() > 0) {//传文件路径 if (m_BmpShow.SetReadBmpPath(m_strBmpFilePath, &nCharLimit)) { m_edtText.EnableWindow(); m_edtText.SetLimitText(nCharLimit); } else { PostMessage(WM_CLOSE, 0, 0); } } else {//传位图信息和数据 if (NULL == m_lpDIBits) { PostMessage(WM_CLOSE, 0, 0); } if (m_BmpShow.SetBmpInfo(&m_bmpIfHi, m_lpDIBits, &nCharLimit)) { m_edtText.EnableWindow(); m_edtText.SetLimitText(nCharLimit); } else { PostMessage(WM_CLOSE, 0, 0); } } return TRUE; // return TRUE unless you set the focus to a control } void CAddTextDlg::OnChangeEditText() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. UpdateData(); m_BmpShow.SetBmpText(m_strText); // TODO: Add your control notification handler code here } BOOL CAddTextDlg::SetBmpFilePath(LPCTSTR pszPath) { if (0 == _mbstrlen(pszPath)) { return FALSE; } m_strBmpFilePath = pszPath; return TRUE; } void CAddTextDlg::OnOK() { // TODO: Add extra validation here if (m_strBmpFilePath.GetLength() > 0) { m_BmpShow.SetSaveBmpPath(m_strBmpFilePath); } if (m_lpDIBits != NULL) { delete[] m_lpDIBits; m_lpDIBits = NULL; } m_lpDIBits = new BYTE[m_dwSize]; m_BmpShow.SaveModify(&m_bmpIfHi, m_lpDIBits); CDialog::OnOK(); } void CAddTextDlg::OnCancel() { // TODO: Add extra cleanup here CDialog::OnCancel(); } BOOL CAddTextDlg::SetBmpInfo(const LPBITMAPINFOHEADER lpbmih, const LPVOID lpvBits) { ASSERT((lpbmih != NULL) && (lpvBits != NULL)); m_strBmpFilePath = ""; if (m_lpDIBits != NULL) { delete[] m_lpDIBits; m_lpDIBits = NULL; } if ((lpbmih->biBitCount < 16) || (lpbmih->biBitCount > 32)) { return FALSE; } memcpy(&m_bmpIfHi, lpbmih, sizeof(BITMAPINFOHEADER)); m_BmpShow.ComputeImageSize(&m_bmpIfHi, &m_dwSize); /* if (m_dwSize != lpbmih->biSizeImage) { return FALSE; }*/ m_lpDIBits = new BYTE[m_dwSize]; memcpy(m_lpDIBits, lpvBits, lpbmih->biSizeImage); // memcpy(m_lpDIBits, lpvBits, m_dwSize); return TRUE; } CAddTextDlg::~CAddTextDlg() { if (m_lpDIBits != NULL) { delete[] m_lpDIBits; m_lpDIBits = NULL; } } BOOL CAddTextDlg::GetBmpInfo(LPBITMAPINFOHEADER lpbmih, LPVOID lpvBits, LPDWORD pdwSize) { memcpy(lpbmih, &m_bmpIfHi, sizeof(BITMAPINFOHEADER)); *pdwSize = m_dwSize; memcpy(lpvBits, m_lpDIBits, m_dwSize); return TRUE; } void CAddTextDlg::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 OnOK(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值