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#封面圖片生成器內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: