溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何用C#代碼實(shí)現(xiàn)封面圖片生成器

發(fā)布時(shí)間:2022-10-21 09:37:26 來(lái)源:億速云 閱讀:175 作者:iii 欄目:編程語(yǔ)言

這篇文章主要講解了“如何用C#代碼實(shí)現(xiàn)封面圖片生成器”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“如何用C#代碼實(shí)現(xiàn)封面圖片生成器”吧!

實(shí)現(xiàn)功能

利用C#做一個(gè)簡(jiǎn)單的封面圖片生成器

開(kāi)發(fā)環(huán)境

開(kāi)發(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);
 
            //漸變畫(huà)筆
            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)效果

如何用C#代碼實(shí)現(xiàn)封面圖片生成器

感謝各位的閱讀,以上就是“如何用C#代碼實(shí)現(xiàn)封面圖片生成器”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)如何用C#代碼實(shí)現(xiàn)封面圖片生成器這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI