如何用C#代码实现封面图片生成器
这篇文章主要讲解了“如何用C#代码实现封面图片生成器”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何用C#代码实现封面图片生成器”吧!
站在用户的角度思考问题,与客户深入沟通,找到涧西网站设计与涧西网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、网站建设、企业官网、英文网站、手机端网站、网站推广、空间域名、网络空间、企业邮箱。业务覆盖涧西地区。
实现功能
利用C#做一个简单的封面图片生成器
开发环境
开发工具: Visual Studio 2013
.NET Framework版本:4.5
实现代码
private void Img_Load(object sender, EventArgs e) { init(); } private void btnBg_Click(object sender, EventArgs e) { ColorDialog cd = new ColorDialog(); if (cd.ShowDialog() == DialogResult.OK) { panelBgColor.BackColor = cd.Color; } } private void btnFontColor1_Click(object sender, EventArgs e) { ColorDialog cd = new ColorDialog(); if (cd.ShowDialog() == DialogResult.OK) { panelFontColor1.BackColor = cd.Color; } } private void btnFontColor2_Click(object sender, EventArgs e) { ColorDialog cd = new ColorDialog(); if (cd.ShowDialog() == DialogResult.OK) { panelFontColor2.BackColor = cd.Color; } } private void btnFont_Click(object sender, EventArgs e) { FontDialog fd = new FontDialog(); fd.Font = txtFont.Font; if (fd.ShowDialog() == DialogResult.OK) { txtFont.Font = fd.Font; } } private void btnPre_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txtWord.Text)) { MessageBox.Show("请先设置文字", "提示"); return; } MemoryStream stream = new MemoryStream(CreateIamge(txtWord.Text)); pictureBox1.Image = Image.FromStream(stream, true); } private void btnSave_Click(object sender, EventArgs e) { if (pictureBox1.Image == null) { MessageBox.Show("请先预览", "提示"); return; } SaveFileDialog sfd = new SaveFileDialog(); sfd.SupportMultiDottedExtensions = true; sfd.Filter = "PNG格式|*.png|JPG格式|*.jpg"; sfd.AddExtension = true; if (sfd.ShowDialog() == DialogResult.OK) { EncoderParameters myEncoderParameters = new EncoderParameters(1); myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L); pictureBox1.Image.Save(sfd.FileName, GetEncoderInfo("image/png"), myEncoderParameters); } } private void init() { Font font = new Font("华文行楷", 36, FontStyle.Bold); txtFont.Font = font; panelFontColor1.BackColor = ColorTranslator.FromHtml("#ff0080ff"); panelFontColor2.BackColor = ColorTranslator.FromHtml("#ffff80c0"); } public byte[] CreateIamge(string str) { int w = Convert.ToInt32(txtWidth.Text); int h = Convert.ToInt32(txtHeight.Text); //渐变画笔 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, w, h), panelFontColor1.BackColor, panelFontColor2.BackColor, 25f, true); Bitmap image = new Bitmap(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(new SolidBrush(panelBgColor.BackColor), new Rectangle(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 = new MemoryStream(); //高质量保存 EncoderParameters myEncoderParameters = new EncoderParameters(1); myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L); image.Save(stream, GetEncoderInfo("image/png"), myEncoderParameters); byte[] buffer = stream.ToArray(); g.Dispose(); image.Dispose(); return buffer; } private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for (j = 0; j < encoders.Length; ++j) { if (encoders[j].MimeType == mimeType) return encoders[j]; } return null; }
实现效果
感谢各位的阅读,以上就是“如何用C#代码实现封面图片生成器”的内容了,经过本文的学习后,相信大家对如何用C#代码实现封面图片生成器这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
文章题目:如何用C#代码实现封面图片生成器
转载源于:http://ybzwz.com/article/ihcjip.html