您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“怎么使用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#實現封面圖片生成器”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。