文字,圖片合成在背景圖上
//str_img_bg背景圖片路徑
using (System.Drawing.Image pickedImage = new System.Drawing.Bitmap(str_img_bg))
{
//str_img_head頭像路徑
using (StreamReader srhead = new StreamReader(str_img_head))
{
System.Drawing.Image img_head = System.Drawing.Image.FromStream(srhead.BaseStream, true);
img_head = KiResizeImage(img_head, width , height );//頭像
//定位
Rectangle fromRhead = new Rectangle(0, 0, 56, 56);//寬高
Rectangle toRhead = new Rectangle(12, 56, 12, 56);//嵌入圖片的位置 x,y座標 和 寬高。
using (System.Drawing.Graphics pickedG = System.Drawing.Graphics.FromImage(pickedImage))
{
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//匯入圖片
pickedG.DrawImage(img_head, toRhead, fromRhead, System.Drawing.GraphicsUnit.Pixel);
//匯入文字 emsize字型的全身大小(以磅值為單位)
//color字型顏色
//x,y在背景圖上得x,y座標
pickedG.DrawString(str_text, new Font("Microsoft YaHei", emsize), new SolidBrush(System.Drawing.ColorTranslator.FromHtml((color))), new PointF(x,y));
//jpg檔案輸出
ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo i in icis)
{
if (i.MimeType == "image/jpeg")
{
ici = i;
}
}
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)90);
//儲存圖片
pickedImage.Save();
}
}
public static System.Drawing.Image KiResizeImage(System.Drawing.Image bmp, int newW, int newH, int Mode)
{
try
{
System.Drawing.Image b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
// 插值演算法的質量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
{
return null;
}
}