最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實現(xiàn)封面圖片生成器的示例代碼

 更新時間:2022年08月22日 15:58:53   作者:Csharp 小記  
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)簡單的封面圖片生成器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

這個東西我已經(jīng)用了有段時間了,從開始寫文章就在用這個,主要原因還是因為我比較懶。懶得去尋找圖片,同時又怕萬一惹來版權(quán)爭議。。。

跟我所有的文章的封面圖一樣,一個純色背景加上文字自動生成一個指定大小的圖片。

代碼實現(xiàn)也比較簡單,如果有興趣的話,可以自己擴展,比如自定義背景圖,自定義水印等。

實現(xiàn)功能

利用C#做一個簡單的封面圖片生成器

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

開發(fā)工具: Visual Studio 2013

.NET Framework版本:4.5

實現(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("請先預覽", "提示");
                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;
        }

實現(xiàn)效果

到此這篇關(guān)于C#實現(xiàn)封面圖片生成器的示例代碼的文章就介紹到這了,更多相關(guān)C#封面圖片生成器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#中把FastReport.Net報表控件的數(shù)據(jù)保存到數(shù)據(jù)庫

    C#中把FastReport.Net報表控件的數(shù)據(jù)保存到數(shù)據(jù)庫

    這篇文章介紹了在數(shù)據(jù)庫中保存FastReport.Net報表的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C#基于Socket實現(xiàn)多人聊天功能

    C#基于Socket實現(xiàn)多人聊天功能

    這篇文章主要為大家詳細介紹了C#基于Socket實現(xiàn)多人聊天功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#灰度化圖像的實例代碼

    C#灰度化圖像的實例代碼

    灰度化一幅圖像就是將圖像的色彩信息全部丟掉,將24位的位圖信息,用8位來表示,灰度圖共有256級灰度等級,也就是將24位位圖的一點如(255,255,255)轉(zhuǎn)換成255,所以R,G,B三個值所乘的系數(shù)和為1
    2013-09-09
  • C#實現(xiàn)簡單的計算器功能

    C#實現(xiàn)簡單的計算器功能

    這篇文章主要為大家詳細介紹了C#實現(xiàn)簡單的計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C# 實現(xiàn)dataGridView選中一行右鍵出現(xiàn)菜單的示例代碼

    C# 實現(xiàn)dataGridView選中一行右鍵出現(xiàn)菜單的示例代碼

    這篇文章主要介紹了C# 實現(xiàn)dataGridView選中一行右鍵出現(xiàn)菜單,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • 最新評論

    定结县| 宕昌县| 宝坻区| 商南县| 定结县| 顺平县| 恩施市| 绥中县| 合川市| 安福县| 平顶山市| 隆子县| 平罗县| 泰兴市| 厦门市| 常熟市| 达拉特旗| 新巴尔虎右旗| 华池县| 阳泉市| 广汉市| 秦皇岛市| 六枝特区| 高州市| 青岛市| 余庆县| 昌黎县| 呼和浩特市| 夏邑县| 平乐县| 广河县| 辉南县| 扎鲁特旗| 永寿县| 团风县| 宜春市| 平利县| 石首市| 新兴县| 大埔县| 林州市|