GDI+根据内容多少延申图片大小

//混合
        auto fun_mixture = [](wstring tmp, wstring head, float fontsize) {
            Gdiplus::Bitmap* img;
            //head=L"订单号:";
            tmp = head + tmp;

            //设置字体大小,
            FontFamily fontFamily(L"Arial");
            Font font(&fontFamily, fontsize, FontStyleBold, UnitPoint);
            StringFormat stringFormat;
            stringFormat.SetAlignment(StringAlignmentNear);
            stringFormat.SetLineAlignment(StringAlignmentNear);

            float descent = fontFamily.GetCellDescent(FontStyleRegular);
            float lineSpacing = fontFamily.GetLineSpacing(FontStyleRegular);
            //下降
            float descentPixel = font.GetSize() * descent / fontFamily.GetEmHeight(FontStyleRegular);
            //行高
            float lineSpacingPixel = font.GetSize() * lineSpacing / fontFamily.GetEmHeight(FontStyleRegular);
            设置总高度
            fontsize = descentPixel + lineSpacingPixel;
            //float b = (float)WIDTH / (fontsize * 2 / 3);
            float height = (((float)tmp.length() + head.length()) * (fontsize * 2 / 3)) / (float)WIDTH;
            int int_h = height; //float
            if (int_h < height) {
                int_h++;
            }
            RectF* rectF = new RectF(0.0f, 0.0f, WIDTH, int_h * fontsize);
            img = new Gdiplus::Bitmap(WIDTH, int_h * fontsize);

            SolidBrush solidBrush(Color(0, 0, 0));
            Graphics* graphics = new Graphics(img);

            const Gdiplus::SizeF layout_size(rectF->Width, font.GetHeight(graphics));
            Gdiplus::SizeF size;
            int kMaxLineCount = tmp.length();
            int kLineHeight = fontsize;
            std::wstring remain_text = tmp;
            for (unsigned int i = 0; i < kMaxLineCount; ++i) {
                int char_count = 0; // 当前行能显示的字符个数
                if (i >= int_h) {
                    Gdiplus::Bitmap* imgcp;
                    int_h++;
                    imgcp = new Gdiplus::Bitmap(WIDTH, (int_h)*fontsize);
                    delete graphics;
                    graphics = new Graphics(imgcp);
                    graphics->DrawImage(img, 0, 0, img->GetWidth(), img->GetHeight());
                    int a = imgcp->GetHeight();
                    delete img;
                    img = imgcp;
                    delete rectF;
                    rectF = new RectF(0.0f, 0.0f, WIDTH, int_h * fontsize);
                }
                Gdiplus::Status status = graphics->MeasureString(
                    remain_text.c_str(),
                    remain_text.length(),
                    &font,
                    layout_size,
                    &stringFormat,
                    &size,
                    &char_count,
                    NULL);

                if (char_count > 0) {
                    // 截取当前行可以显示的文本并显示
                    std::wstring substr = remain_text.substr(0, char_count);
                    Gdiplus::RectF subrc(rectF->X, rectF->Y + i * kLineHeight, layout_size.Width, layout_size.Height);
                    graphics->DrawString(substr.c_str(), substr.length(), &font, subrc, &stringFormat, &solidBrush);

                    // 更新剩余文本信息
                    remain_text.erase(0, char_count);

                    if (remain_text.empty())
                        break;
                } else {
                    break;
                }
            }
            delete rectF;
            delete graphics;
            return img;
        };

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值