public string Draw()
{
//背景圖片,海報背景
string path = Server.MapPath("/Content/tg.jpg");
System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(path);
//處理二維碼圖片大小 240*240px
System.Drawing.Image qrCodeImage = ReduceImage("https://api.ooopn.com/qr/api.php?text=https://www.sssam.com&size=360px", 0, 0);
//處理頭像圖片大小 100*100px,我這里沒放頭像,所以注釋掉,用到的話放開注釋自己測。同時下方也放開
//Image titleImage = ReduceImage(user.headimgurl, 100, 100);
using (Graphics g = Graphics.FromImage(imgSrc))
{
//畫專屬推廣二維碼
g.DrawImage(qrCodeImage, new Rectangle(imgSrc.Width - qrCodeImage.Width -450,//-450這個數,越小越靠左,可以調整二維碼在背景圖的位置
imgSrc.Height - qrCodeImage.Height-650 ,//同理-650越小越靠上
qrCodeImage.Width,
qrCodeImage.Height),
0, 0, qrCodeImage.Width, qrCodeImage.Height, GraphicsUnit.Pixel);
//畫頭像
//g.DrawImage(titleImage, 8, 8, titleImage.Width, titleImage.Height);
Font font = new Font("宋體", 30, FontStyle.Bold);
g.DrawString("這里輸入文字", font, new SolidBrush(Color.Red), 500, 800);
}
string newpath = Server.MapPath(@"/Content/newtg_" + Guid.NewGuid().ToString() + ".jpg");
imgSrc.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg);
return newpath;
}
//獲取圖片並處理成指定只存返回,寬高填寫0,直接返回原尺寸
public Image ReduceImage(string url, int toWidth, int toHeight)
{
//這里網絡方式獲取圖片二維碼,本地讀取請自己寫
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
Image originalImage = Image.FromStream(responseStream);
if (toWidth <= 0 && toHeight <= 0)
{
return originalImage;//這里直接返回
}
else if (toWidth > 0 && toHeight > 0)
{
if (originalImage.Width < toWidth && originalImage.Height < toHeight)
return originalImage;
}
else if (toWidth <= 0 && toHeight > 0)
{
if (originalImage.Height < toHeight)
return originalImage;
toWidth = originalImage.Width * toHeight / originalImage.Height;
}
else if (toHeight <= 0 && toWidth > 0)
{
if (originalImage.Width < toWidth)
return originalImage;
toHeight = originalImage.Height * toWidth / originalImage.Width;
}
Image toBitmap = new Bitmap(toWidth, toHeight);
using (Graphics g = Graphics.FromImage(toBitmap))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
g.DrawImage(originalImage,
new Rectangle(0, 0, toWidth, toHeight),
new Rectangle(0, 0, originalImage.Width, originalImage.Height),
GraphicsUnit.Pixel);
originalImage.Dispose();
return toBitmap;
}
}
這里是生成分享海報,下一篇將完成---->將海報分享至微信及朋友圈。