这个东西我已经用了有段时间了,从开始写文章就在用这个,主要原因还是因为我比较懒。懒得去寻找图片,同时又怕万一惹来版权争议。。。
跟我所有的文章的封面图一样,一个纯色背景加上文字自动生成一个指定大小的图片。
代码实现也比较简单,如果有兴趣的话,可以自己扩展,比如自定义背景图,自定义水印等。
实现功能:
利用C做一个简单的封面图片生成器
开发环境:
开发工具: Visual Studio 2013
.NET Framework版本:4.5
实现代码:
privatevoidImg_Load(objectsender, EventArgs e){
init();
}privatevoidbtnBg_Click(objectsender, EventArgs e){
ColorDialog cd =newColorDialog();if(cd.ShowDialog() == DialogResult.OK)
{
panelBgColor.BackColor = cd.Color;
}
}privatevoidbtnFontColor1_Click(objectsender, EventArgs e){
ColorDialog cd =newColorDialog();if(cd.ShowDialog() == DialogResult.OK)
{
panelFontColor1.BackColor = cd.Color;
}
}privatevoidbtnFontColor2_Click(objectsender, EventArgs e){
ColorDialog cd =newColorDialog();if(cd.ShowDialog() == DialogResult.OK)
{
panelFontColor2.BackColor = cd.Color;
}
}privatevoidbtnFont_Click(objectsender, EventArgs e){
FontDialog fd =newFontDialog();
fd.Font = txtFont.Font;if(fd.ShowDialog() == DialogResult.OK)
{
txtFont.Font = fd.Font;
}
}privatevoidbtnPre_Click(objectsender, EventArgs e){if(string.IsNullOrWhiteSpace(txtWord.Text))
{
MessageBox.Show("请先设置文字","提示");return;
}
MemoryStream stream =newMemoryStream(CreateIamge(txtWord.Text));
pictureBox1.Image = Image.FromStream(stream,true);
}privatevoidbtnSave_Click(objectsender, EventArgs e){if(pictureBox1.Image ==null)
{
MessageBox.Show("请先预览","提示");return;
}
SaveFileDialog sfd =newSaveFileDialog();
sfd.SupportMultiDottedExtensions =true;
sfd.Filter ="PNG格式|*.png|JPG格式|*.jpg";
sfd.AddExtension =true;if(sfd.ShowDialog() == DialogResult.OK)
{
EncoderParameters myEncoderParameters =newEncoderParameters(1);
myEncoderParameters.Param[0] =newEncoderParameter(Encoder.Quality,100L);
pictureBox1.Image.Save(sfd.FileName, GetEncoderInfo("image/png"), myEncoderParameters);
}
}privatevoidinit(){
Font font =newFont("华文行楷",36, FontStyle.Bold);
txtFont.Font = font;
panelFontColor1.BackColor = ColorTranslator.FromHtml("ff0080ff");
panelFontColor2.BackColor = ColorTranslator.FromHtml("ffff80c0");
}publicbyte[]CreateIamge(stringstr){intw = Convert.ToInt32(txtWidth.Text);inth = Convert.ToInt32(txtHeight.Text);//渐变画笔LinearGradientBrush brush =newLinearGradientBrush(newRectangle(0,0, w, h), panelFontColor1.BackColor, panelFontColor2.BackColor,25f,true);
Bitmap image =newBitmap(w, h);
Graphics g = Graphics.FromImage(image);//设置高质量插值法g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;//设置高质量,低速度呈现平滑程度g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;//消除锯齿g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//填充背景色g.FillRectangle(newSolidBrush(panelBgColor.BackColor),newRectangle(0,0, w, h));//设置文本呈现方式g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;//获取文字大小SizeF sf = g.MeasureString(str, txtFont.Font);
g.DrawString(str, txtFont.Font, brush, (w - sf.Width) /2, (h - sf.Height) /2);
MemoryStream stream =newMemoryStream();//高质量保存EncoderParameters myEncoderParameters =newEncoderParameters(1);
myEncoderParameters.Param[0] =newEncoderParameter(Encoder.Quality,100L);
image.Save(stream, GetEncoderInfo("image/png"), myEncoderParameters);byte[] buffer = stream.ToArray();
g.Dispose();
image.Dispose();returnbuffer;
}privatestaticImageCodecInfoGetEncoderInfo(String mimeType){intj;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();for(j =0; j < encoders.Length; ++j)
{if(encoders[j].MimeType == mimeType)returnencoders[j];
}returnnull;
}
实现效果:
由简入繁,拿来即用
更多精彩,请持续关注
V:Csharp 小记
免责声明:内容来自用户上传并发布,站点仅提供信息存储空间服务,不拥有所有权,本网站所提供的信息只供参考之用。