第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在運行時動態(tài)地從文本生成圖像

如何在運行時動態(tài)地從文本生成圖像

C#
慕田峪9158850 2019-10-25 15:34:57
任何人都可以指導如何從輸入文本生成圖像。圖片可能有任何擴展名都沒關系。
查看完整描述

3 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

好的,假設您想在C#中的圖像上繪制字符串,則需要在此處使用System.Drawing命名空間:


private Image DrawText(String text, Font font, Color textColor, Color backColor)

{

    //first, create a dummy bitmap just to get a graphics object

    Image img = new Bitmap(1, 1);

    Graphics drawing = Graphics.FromImage(img);


    //measure the string to see how big the image needs to be

    SizeF textSize = drawing.MeasureString(text, font);


    //free up the dummy image and old graphics object

    img.Dispose();

    drawing.Dispose();


    //create a new image of the right size

    img = new Bitmap((int) textSize.Width, (int)textSize.Height);


    drawing = Graphics.FromImage(img);


    //paint the background

    drawing.Clear(backColor);


    //create a brush for the text

    Brush textBrush = new SolidBrush(textColor);


    drawing.DrawString(text, font, textBrush, 0, 0);


    drawing.Save();


    textBrush.Dispose();

    drawing.Dispose();


    return img;


}

此代碼將首先測量字符串,然后創(chuàng)建正確大小的圖像。


如果要保存此函數的返回,只需調用返回圖像的Save方法。


查看完整回答
反對 回復 2019-10-25
?
DIEA

TA貢獻1820條經驗 獲得超3個贊

謝謝卡扎爾。對以前使用USING處置圖像/圖形對象以及引入最小尺寸參數后的使用方法的回答略有改進


    private Image DrawTextImage(String currencyCode, Font font, Color textColor, Color backColor) {

        return DrawTextImage(currencyCode, font, textColor, backColor, Size.Empty);

    }

    private Image DrawTextImage(String currencyCode, Font font, Color textColor, Color backColor, Size minSize) {

        //first, create a dummy bitmap just to get a graphics object

        SizeF textSize;

        using (Image img = new Bitmap(1, 1)) {

            using (Graphics drawing = Graphics.FromImage(img)) {

                //measure the string to see how big the image needs to be

                textSize = drawing.MeasureString(currencyCode, font);

                if (!minSize.IsEmpty) {

                    textSize.Width = textSize.Width > minSize.Width ? textSize.Width : minSize.Width;

                    textSize.Height = textSize.Height > minSize.Height ? textSize.Height : minSize.Height;

                }

            }

        }


        //create a new image of the right size

        Image retImg = new Bitmap((int)textSize.Width, (int)textSize.Height);

        using (var drawing = Graphics.FromImage(retImg)) {

            //paint the background

            drawing.Clear(backColor);


            //create a brush for the text

            using (Brush textBrush = new SolidBrush(textColor)) {

                drawing.DrawString(currencyCode, font, textBrush, 0, 0);

                drawing.Save();

            }

        }

        return retImg;

    }


查看完整回答
反對 回復 2019-10-25
  • 3 回答
  • 0 關注
  • 429 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號