欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)自動填充文字內(nèi)容到指定圖片

 更新時間:2024年04月18日 14:03:35   作者:初九之潛龍勿用  
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)自動填充文字內(nèi)容到指定圖片,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

需求

在我們的一些發(fā)布系統(tǒng)項(xiàng)目應(yīng)用中,會經(jīng)常發(fā)布一些鏈接圖標(biāo),該圖標(biāo)基本上以模板背景為主,并填充項(xiàng)目文字內(nèi)容。解決方式一般會讓美工進(jìn)行制作處理,但當(dāng)模板化以后,問題的焦點(diǎn)則集中在文字的顯示上,因些利用程序控制文字自動填充模板背景圖片,可以自動化的解決需求。

比如有如下模板:

(1)純色模板

(2)圖片模板

如以上的模板,我們需要在指定的區(qū)域填充文字(比如項(xiàng)目名稱、課程標(biāo)題等等),簡單的描述,就是隨著文字的增多而將字體變小和折行。

如上圖中標(biāo)題文字增加,則顯示如下:

開發(fā)運(yùn)行環(huán)境

操作系統(tǒng): Windows Server 2019 DataCenter

.net版本: .netFramework4.0 或以上

開發(fā)工具:VS2019  C#

方法設(shè)計(jì)

設(shè)計(jì) AddText 方法,返回 System.Drawing.Bitmap 對象,設(shè)計(jì)如下表:

序號參數(shù)類型說明
1imgPathstring模板圖片文件路徑
2saveImgPathstring可導(dǎo)出的成品圖片文件路徑
3baselenint標(biāo)題基礎(chǔ)計(jì)算長度,一般傳遞標(biāo)題的總長度(.Length)
4locationLeftTopstring文字輸出區(qū)域的左上角坐標(biāo) Left: x1 ,Top: y1
參數(shù)形式以逗號分隔,如:20,100
5locationRightBottomstring文字輸出區(qū)域的右下角坐標(biāo) Right: x2 ,Bottom: y2
參數(shù)形式以逗號分隔,如:120,200
6textstring要寫入的文字內(nèi)容
7fontNamestring字體,非必傳項(xiàng),默認(rèn)為 "華文行楷"

請注意前6個參數(shù)為必填寫項(xiàng),且 locationLeftTop 和 locationRightBottom 請傳遞合理的數(shù)值。

實(shí)現(xiàn)代碼

AddText方法

public  System.Drawing.Bitmap AddText(string imgPath,string saveImgPath,int baselen, string locationLeftTop, string locationRightBottom, string text, string fontName = "華文行楷")
{
    System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath);
 
    int width = img.Width;
    int height = img.Height;
    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
    System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
 
    // 計(jì)算文字區(qū)域
    // 左上角
    string[] location = locationLeftTop.Split(',');
    float x1 = float.Parse(location[0]);
    float y1 = float.Parse(location[1]);
    // 右下角
    location = locationRightBottom.Split(',');
    float x2 = float.Parse(location[0]);
    float y2 = float.Parse(location[1]);
    // 區(qū)域?qū)捀?
    float fontWidth = x2 - x1;
    float fontHeight = y2 - y1;
 
    float fontSize = fontHeight;  // 初次估計(jì)先用文字區(qū)域高度作為文字字體大小,后面再做調(diào)整,單位為px
 
    System.Drawing.Font font = new System.Drawing.Font(fontName,18, System.Drawing.GraphicsUnit.Pixel);
    System.Drawing.SizeF sf = graph.MeasureString(text, font);
 
 
    // 最終的得出的字體所占區(qū)域一般不會剛好等于實(shí)際區(qū)域
    // 所以根據(jù)兩個區(qū)域的相差之處再把文字開始位置(左上角定位)稍微調(diào)整一下
    string title = text;
    text = "";
    int gs = title.Length / baselen;
    if (title.Length % baselen != 0)
    {
        gs++;
    }
    string[] lines = new string[gs];
    int startpos = 0;
    
    for (int i = 0; i < gs; i++)
    {
        int len = title.Length < baselen ? title.Length : baselen;
        lines[i] = title.Substring(0, len);
        startpos += len;
        title = title.Substring(len);
        text += lines[i] + "\r\n";
    }
 
    x1 += (fontWidth - sf.Width) / 2;
    y1 += (fontHeight - sf.Height) / 2;
    x1 = (width - baselen * 18) / 2;
    y1 = (height - lines.Length * 18) / 2;
    graph.DrawImage(img, 0, 0, width, height);
 
    graph.DrawString(text, font, new System.Drawing.SolidBrush(System.Drawing.Color.White), x1, y1);
    
    graph.Dispose();
    img.Dispose();
    bmp.Save(saveImgPath,System.Drawing.Imaging.ImageFormat.Jpeg);
    return bmp;
}

圖片轉(zhuǎn)Base64

            public string ImgToBase64String(string Imagefilename,bool outFullString=false)
            {
                try
                {
                    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Imagefilename);
 
                    MemoryStream ms = new MemoryStream();
                    //            bmp.Save(ms,ImageFormat.Jpeg)
                    System.Drawing.Imaging.ImageFormat iformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    string extension = System.IO.Path.GetExtension(Imagefilename).Replace(".", "").ToLower();
                    if (extension == "bmp")
                    {
                        iformat = System.Drawing.Imaging.ImageFormat.Bmp;
                    }
                    else if (extension == "emf")
                    {
                        iformat = System.Drawing.Imaging.ImageFormat.Emf;
                    }
                    else if (extension == "exif")
                    {
                        iformat = System.Drawing.Imaging.ImageFormat.Exif;
                    }
                    else if (extension == "gif")
                    {
                        iformat = System.Drawing.Imaging.ImageFormat.Gif;
                    }
                    else if (extension == "icon")
                    {
                        iformat = System.Drawing.Imaging.ImageFormat.Icon;
                    }
                    else if (extension == "png")
                    {
                        iformat = System.Drawing.Imaging.ImageFormat.Png;
                    }
                    else if (extension == "tiff")
                    {
                        iformat = System.Drawing.Imaging.ImageFormat.Tiff;
                    }
                    else if (extension == "wmf")
                    {
                        iformat = System.Drawing.Imaging.ImageFormat.Wmf;
                    }
 
                    bmp.Save(ms, iformat);
                    byte[] arr = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(arr, 0, (int)ms.Length);
                    ms.Close();
                    bmp.Dispose();
                    string rv=Convert.ToBase64String(arr);
                    if (outFullString == true)
                    {
                        rv = "data:image/" + extension + ";base64," + rv;
                    }
                    return rv;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }

 請注意 bool outFullString=false,默認(rèn)為false,表示輸出純Base64編碼。

 如果直接作用于Image對象的 ImageUrl,則需要設(shè)置為true。即在生成結(jié)果前加上 "data:image/jpeg;base64," + base64 字樣。

調(diào)用示例 

void Page_load(Object sender, EventArgs e){
 
    string path = "D:\\website\\test\\";
    string title="數(shù)據(jù)庫存儲過程從入門到精通";
    int baselen = title.Length;
    string x1_y1="0,0";
    string x2_y2="240,80";
 
    AddText(path + "bg.bmp", path + "bg2.jpg", baselen, x1_y1, x2_y2, title);
 
    Image1.ImageUrl = ImgToBase64String(path + "bg2.jpg", true);
 
}

其中 Image1 為 Asp.net WebUI 中的 Image 對象。 

小結(jié)

本方法同時輸出 saveImgPath 目標(biāo)成品文件路徑和返回Bitmap對象,saveImgPath 為必填參數(shù)。我們可以根據(jù)實(shí)際需要進(jìn)行后續(xù)處理和改造。

方法理論上可以無限填充,但考慮實(shí)際效果,對文本內(nèi)容的長度還是要有一些限制,以達(dá)到比較理想的顯示效果。

到此這篇關(guān)于C#實(shí)現(xiàn)自動填充文字內(nèi)容到指定圖片的文章就介紹到這了,更多相關(guān)C#自動填充文字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • c# richtextbox更新大量數(shù)據(jù)不卡死的實(shí)現(xiàn)方式

    c# richtextbox更新大量數(shù)據(jù)不卡死的實(shí)現(xiàn)方式

    這篇文章主要介紹了c# richtextbox更新大量數(shù)據(jù)不卡死的實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • C#實(shí)現(xiàn)IDisposable接口釋放非托管資源

    C#實(shí)現(xiàn)IDisposable接口釋放非托管資源

    這篇文章主要為大家介紹了C#實(shí)現(xiàn)IDisposable接口釋放非托管資源,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • WPF實(shí)現(xiàn)繪制折線圖的示例代碼

    WPF實(shí)現(xiàn)繪制折線圖的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何使用WPF實(shí)現(xiàn)繪制簡單的折線圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-04-04
  • unity3d?對接?workerman?實(shí)現(xiàn)聯(lián)機(jī)游戲功能

    unity3d?對接?workerman?實(shí)現(xiàn)聯(lián)機(jī)游戲功能

    workerman?是一款開源高性能?PHP?應(yīng)用容器,他除了用于互聯(lián)網(wǎng)、即時通訊、APP?開發(fā)、硬件通訊、智能家居、物聯(lián)網(wǎng)等領(lǐng)域的開發(fā)外,這篇文章主要介紹了unity3d?對接?workerman?實(shí)現(xiàn)聯(lián)機(jī)游戲,需要的朋友可以參考下
    2022-10-10
  • 淺析C#中的AsnycLocal與ThreadLocal

    淺析C#中的AsnycLocal與ThreadLocal

    這篇文章主要給大家介紹了關(guān)于C#中AsnycLocal與ThreadLocal的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-09-09
  • C#使用鉤子獲得按鍵信息的方法

    C#使用鉤子獲得按鍵信息的方法

    這篇文章主要介紹了C#使用鉤子獲得按鍵信息的方法,實(shí)例分析了C#中鉤子的相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-05-05
  • C#中接口(Interface)的深入詳解

    C#中接口(Interface)的深入詳解

    這篇文章主要給大家介紹了關(guān)于C#中接口(Interface)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • C#中32位浮點(diǎn)數(shù)Float(Real)一步步按位Bit進(jìn)行分析

    C#中32位浮點(diǎn)數(shù)Float(Real)一步步按位Bit進(jìn)行分析

    這篇文章主要介紹了C#中32位浮點(diǎn)數(shù)Float(Real)一步步按位Bit進(jìn)行分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 高效C#編碼優(yōu)化原則

    高效C#編碼優(yōu)化原則

    這篇文章主要介紹了高效C#編碼優(yōu)化原則,非常實(shí)用,需要的朋友可以參考下
    2014-08-08
  • C#圖書管理系統(tǒng) 附源碼下載

    C#圖書管理系統(tǒng) 附源碼下載

    這篇文章主要為大家詳細(xì)介紹了C#圖書管理系統(tǒng),文章中附源碼下載,示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09

最新評論