C#實(shí)現(xiàn)封面圖片生成器的示例代碼
這個東西我已經(jīng)用了有段時間了,從開始寫文章就在用這個,主要原因還是因?yàn)槲冶容^懶。懶得去尋找圖片,同時又怕萬一惹來版權(quán)爭議。。。

跟我所有的文章的封面圖一樣,一個純色背景加上文字自動生成一個指定大小的圖片。
代碼實(shí)現(xiàn)也比較簡單,如果有興趣的話,可以自己擴(kuò)展,比如自定義背景圖,自定義水印等。
實(shí)現(xiàn)功能
利用C#做一個簡單的封面圖片生成器
開發(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("請先設(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("請先預(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# OpenCvSharp利用白平衡技術(shù)實(shí)現(xiàn)圖像修復(fù)功能
這篇文章主要為大家詳細(xì)介紹了C# OpenCvSharp如何利用白平衡技術(shù)實(shí)現(xiàn)圖像修復(fù)功能,文中的示例代碼講解詳細(xì),希望對大家有一定的幫助2024-02-02
Unity Shader實(shí)現(xiàn)2D游戲迷霧
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)2D游戲迷霧,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-04-04
C#數(shù)組中List, Dictionary的相互轉(zhuǎn)換問題
這篇文章主要介紹了C#數(shù)組中List, Dictionary的相互轉(zhuǎn)換問題,本文給大家介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友可以參考下2016-12-12
C#使用WebClient實(shí)現(xiàn)上傳下載
這篇文章介紹了C#使用WebClient實(shí)現(xiàn)上傳下載的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
c#實(shí)現(xiàn)簡單控制臺udp異步通信程序示例
這篇文章主要介紹了c#實(shí)現(xiàn)簡單控制臺udp異步通信程序示例,需要的朋友可以參考下2014-04-04
C#中利用斷點(diǎn)操作調(diào)試程序的步驟詳解
所謂斷點(diǎn)調(diào)試就是檢測執(zhí)行路徑和數(shù)據(jù)是否正確,中斷游戲運(yùn)行在線調(diào)試,下面這篇文章主要給大家介紹了關(guān)于C#中利用斷點(diǎn)操作調(diào)試程序的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12

