using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace 自定画图300
{
public class DrawLord
{
private ImageCodecInfo _imageCodecInfo = null;
private EncoderParameters _encoderParameters = null;
public DrawLord()
{
_imageCodecInfo = GetEncoder(ImageFormat.Png);
EncoderParameters _encoderParameters = new EncoderParameters(1);
_encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);//0-100,100不失真
}
public void CreateImage(string path, int width, int height)
{
using (Bitmap bitmap = new Bitmap(width, height))
{
bitmap.Save(path, ImageFormat.Png);
bitmap.Dispose();
}
}
public void DrawImage(string source_path, string target_path, int y, int width, int height)
{
Bitmap source_image = new Bitmap(source_path);
int x = (source_image.Width - width) / 2;
MemoryStream memory = new MemoryStream();
source_image.Save(memory, ImageFormat.Png);
source_image.Dispose();
Bitmap target_image = new Bitmap(target_path);
Bitmap temp_image = (Bitmap)Bitmap.FromStream(memory);
Graphics g = Graphics.FromImage(temp_image);
g.DrawImage(target_image, x, y, width, height);
g.Dispose();
temp_image.Save(source_path, _imageCodecInfo, _encoderParameters);
temp_image.Dispose();
}
public void DrawImage(string source_path, string target_path, int x, int y, int width, int height)
{
Bitmap source_image = new Bitmap(source_path);
MemoryStream memory = new MemoryStream();
source_image.Save(memory, ImageFormat.Png);
source_image.Dispose();
Bitmap target_image = new Bitmap(target_path);
Bitmap temp_image = (Bitmap)Bitmap.FromStream(memory);
Graphics g = Graphics.FromImage(temp_image);
g.DrawImage(target_image, x, y, width, height);
g.Dispose();
temp_image.Save(source_path, _imageCodecInfo, _encoderParameters);
temp_image.Dispose();
}
public void DrawString(string source_path, string chars, Font font, SolidBrush brush, int y)
{
Bitmap source_image = new Bitmap(source_path);
int width = source_image.Width;
MemoryStream memory = new MemoryStream();
source_image.Save(memory, ImageFormat.Png);
source_image.Dispose();
Bitmap temp_image = (Bitmap)Bitmap.FromStream(memory);
Graphics g = Graphics.FromImage(temp_image);
SizeF sizeF = g.MeasureString(chars, font); //居中操作
g.DrawString(chars, font, brush, (width - sizeF.Width) / 2, y);
g.Dispose();
temp_image.Save(source_path, ImageFormat.Png);
temp_image.Dispose();
}
public void DrawString(string source_path, string chars, Font font, SolidBrush brush, int x, int y)
{
Bitmap source_image = new Bitmap(source_path);
MemoryStream memory = new MemoryStream();
source_image.Save(memory, ImageFormat.Png);
source_image.Dispose();
Bitmap temp_image = (Bitmap)Bitmap.FromStream(memory);
Graphics g = Graphics.FromImage(temp_image);
g.DrawString(chars, font, brush, x, y);
g.Dispose();
temp_image.Save(source_path, ImageFormat.Png);
temp_image.Dispose();
}
public void DrawStringLineBreak(string source_path, string chars, Font font, SolidBrush brush, int x, int y)
{
Bitmap source_image = new Bitmap(source_path);
int width = source_image.Width - x;
int height = source_image.Height - y;
MemoryStream memory = new MemoryStream();
source_image.Save(memory, ImageFormat.Png);
source_image.Dispose();
StringFormat format = new StringFormat();
format.LineAlignment = StringAlignment.Near;//左对齐
format.FormatFlags = StringFormatFlags.LineLimit;//自动换行
Rectangle r = new Rectangle(x, y, width, height);
Bitmap temp_image = (Bitmap)Bitmap.FromStream(memory);
Graphics g = Graphics.FromImage(temp_image);
g.DrawString(chars, font, brush, r, format);
g.Dispose();
temp_image.Save(source_path, ImageFormat.Png);
temp_image.Dispose();
}
private static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
}
}
调用
private void button1_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
string path = Path.Combine(Application.StartupPath, i + ".png");
test(path, i.ToString());
}
}
private void test(string save_path, string temp)
{
DrawLord drawLord = new DrawLord();
drawLord.CreateImage(save_path, 574, 800);
string you_hui_quan_path = Path.Combine(Application.StartupPath, "测试\\优惠券.PNG");
string fang_kuang_path = Path.Combine(Application.StartupPath, "测试\\方框.png");
string er_wei_ma_path = Path.Combine(Application.StartupPath, "测试\\联图二维码.png");
string dian_pu_path = Path.Combine(Application.StartupPath, "测试\\店铺头像.jpg");
string zhu_tu_path = Path.Combine(Application.StartupPath, "测试\\主图图片.jpg");
string jing_path = Path.Combine(Application.StartupPath, "测试\\金.png");
drawLord.DrawImage(save_path, you_hui_quan_path, 0, 0, 574, 800);
drawLord.DrawImage(save_path, fang_kuang_path, 398, 673, 100, 100);
drawLord.DrawImage(save_path, er_wei_ma_path, 415, 690, 65, 65);
////画板画图片(#店铺头像,#横居中,50,70,70)
drawLord.DrawImage(save_path, dian_pu_path, 50, 70, 70);
//店铺标题 字体颜色(白色) 字体大小(25) 画板写字(居中,140,店铺名称)
Font font = new Font("微软雅黑", 25, FontStyle.Bold);
SolidBrush brush = new SolidBrush(Color.White);
drawLord.DrawString(save_path, "店铺标题"+ temp, font, brush, 140);
//商品原价(250,540,)
font = new Font("微软雅黑", 25, FontStyle.Bold);
brush = new SolidBrush(Color.Red);
drawLord.DrawString(save_path, "100", font, brush, 250, 540);
//商品现价(250,540,)
font = new Font("微软雅黑", 25, FontStyle.Bold);
brush = new SolidBrush(Color.Red);
drawLord.DrawString(save_path, "80", font, brush, 250, 600);
font = new Font("微软雅黑", 18, FontStyle.Bold);
brush = new SolidBrush(Color.Black);
drawLord.DrawStringLineBreak(save_path, "德佑迷你手口湿巾小包随身装婴儿成人8包48片 mini卫生便携湿纸巾", font, brush, 300, 300);
}