C#實(shí)現(xiàn)封面圖片生成器的示例代碼
這個(gè)東西我已經(jīng)用了有段時(shí)間了,從開始寫文章就在用這個(gè),主要原因還是因?yàn)槲冶容^懶。懶得去尋找圖片,同時(shí)又怕萬(wàn)一惹來(lái)版權(quán)爭(zhēng)議。。。
跟我所有的文章的封面圖一樣,一個(gè)純色背景加上文字自動(dòng)生成一個(gè)指定大小的圖片。
代碼實(shí)現(xiàn)也比較簡(jiǎn)單,如果有興趣的話,可以自己擴(kuò)展,比如自定義背景圖,自定義水印等。
實(shí)現(xiàn)功能
利用C#做一個(gè)簡(jiǎn)單的封面圖片生成器
開發(fā)環(huán)境
開發(fā)工具: Visual Studio 2013
.NET Framework版本:4.5
實(shí)現(xiàn)代碼
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("請(qǐng)先設(shè)置文字", "提示"); 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("請(qǐng)先預(yù)覽", "提示"); 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); //設(shè)置高質(zhì)量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度 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)); //設(shè)置文本呈現(xiàn)方式 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(); //高質(zhì)量保存 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; }
實(shí)現(xiàn)效果
到此這篇關(guān)于C#實(shí)現(xiàn)封面圖片生成器的示例代碼的文章就介紹到這了,更多相關(guān)C#封面圖片生成器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C#實(shí)現(xiàn)文字視頻生成器的示例代碼
- C#設(shè)計(jì)模式之建造者模式生成器模式示例詳解
- Python實(shí)現(xiàn)C#代碼生成器應(yīng)用服務(wù)于Unity示例解析
- C#設(shè)計(jì)模式實(shí)現(xiàn)之生成器模式和責(zé)任鏈模式
- C# Guid長(zhǎng)度雪花簡(jiǎn)單生成器的示例代碼
- c# 如何實(shí)現(xiàn)代碼生成器
- C#設(shè)計(jì)模式之Builder生成器模式解決帶老婆配置電腦問題實(shí)例
- 詳解C#設(shè)計(jì)模式編程中生成器模式的使用
- 詳解C#中有趣的 SourceGenerator生成器
相關(guān)文章
C# OpenCvSharp利用白平衡技術(shù)實(shí)現(xiàn)圖像修復(fù)功能
這篇文章主要為大家詳細(xì)介紹了C# OpenCvSharp如何利用白平衡技術(shù)實(shí)現(xiàn)圖像修復(fù)功能,文中的示例代碼講解詳細(xì),希望對(duì)大家有一定的幫助2024-02-02Unity Shader實(shí)現(xiàn)2D游戲迷霧
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)2D游戲迷霧,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C#數(shù)組中List, Dictionary的相互轉(zhuǎn)換問題
這篇文章主要介紹了C#數(shù)組中List, Dictionary的相互轉(zhuǎn)換問題,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12C#使用WebClient實(shí)現(xiàn)上傳下載
這篇文章介紹了C#使用WebClient實(shí)現(xiàn)上傳下載的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05c#實(shí)現(xiàn)簡(jiǎn)單控制臺(tái)udp異步通信程序示例
這篇文章主要介紹了c#實(shí)現(xiàn)簡(jiǎn)單控制臺(tái)udp異步通信程序示例,需要的朋友可以參考下2014-04-04C#中利用斷點(diǎn)操作調(diào)試程序的步驟詳解
所謂斷點(diǎn)調(diào)試就是檢測(cè)執(zhí)行路徑和數(shù)據(jù)是否正確,中斷游戲運(yùn)行在線調(diào)試,下面這篇文章主要給大家介紹了關(guān)于C#中利用斷點(diǎn)操作調(diào)試程序的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12