民间花鸟字的一种实现

先上一张效果图

这里花鸟字每一个字都是一张图片,实现过程即通过输入的文字去获取对应的图片,然后平铺绘制到一张图片上,格式可自己设置三五个一行都可以,把输入的文字合成到一张图片上,之后把图片居中显示出来就是界面上的效果


以下附上使用C++的一种实现方式,理论上只要所用语言支持图片绘制就可以移植或者自行实现

// 绘制代码
void CArt::Paint(Graphics& v_graphics)
{    
    v_graphics.TranslateTransform(m_nFrameWidth / 2.0f, m_nFrameHeight / 2.0f);
    if (m_pBgImage && m_vetFileList.size() < 4) v_graphics.DrawImage(m_pBgImage, Rect(-m_nBgWidth / 2.0f, -m_nBgHeight / 2.0f, m_nBgWidth + 0.0f, m_nBgHeight + 0.0f), 0, 0, m_nBgWidth, m_nBgHeight, UnitPixel);
    if (m_pFontImage) v_graphics.DrawImage(m_pFontImage, Rect(-m_nFontWidth / 2.0f, -m_nFontHeight / 2.0f, m_nFontWidth + 0.0f, m_nFontHeight + 0.0f), 0, 0, m_nFontWidth, m_nFontHeight, UnitPixel);
}

// 生成花鸟字图片
bool CArt::LoadImage(string strURI)
{
    bool bRet = false;
    int length = strURI.length();
    if (length && length % 6 == 0) {
        m_vetFileList.clear();
        SAFE_DELETE(m_pFontImage);
        for (int i = 0; i < length; i += 6) {
            string str = strURI.substr(i, 6);
            str += ".jpg";
            m_vetFileList.emplace_back(str);
        }
        int size = m_vetFileList.size();
        if (size > 0) {
            m_nFontWidth = FONT_WIDTH * size;
            m_nFontHeight = FONT_HEIGHT;
            m_pFontImage = new Bitmap(m_nFontWidth, m_nFontHeight);
            Graphics gs(m_pFontImage);
            for (int i = 0; i < size; i++) {
                string strFileName = g_strWorkDir + "/art/data/" + m_vetFileList[i];
                CString strImage(strFileName.c_str());
                Bitmap image(strImage);
                gs.DrawImage(&image, Rect(i * FONT_WIDTH, 0, FONT_WIDTH, FONT_HEIGHT), 0, 0, FONT_WIDTH, FONT_HEIGHT, UnitPixel);
            }
            Update();
            bRet = true;
        }
    }
    return bRet;
}

// 按钮点击事件
void OnBtnAdd() {
    CDuiString strText = m_pEditText->GetText();
    string strUtf8;
    bool bRet = tools::UnicodeToUtf8(strText.GetData(), strUtf8);
    if (bRet) {
        string strURI = encodeURIComponent(strUtf8, false);
        CArt* pArt = new CArt();
        if (pArt) {
            bool bRet = pArt->LoadImage(strURI);
        }
    }
}

代码参考:https://download.csdn.net/download/dragonwarrior_95/20211368

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pixi0212

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值