无法使用Gdiplus :: Graphics :: DrawText在黑色背景上绘制白色文本

时间:2011-03-31 17:13:54

标签: c++ graphics text gdi+ drawtext

我正在尝试找到一种方法来使用Gdiplus :: Graphics.DrawString方法在黑色背景上创建一个具有白色文本的位图。

(为什么?因为我需要能够旋转文本,而Gdiplus :: Graphics对象可以使用RotateTransform。)

我编写了一个示例控制台应用程序,演示了我在尝试在黑色矩形上绘制白色文本时看到的内容。 (代码粘贴在下面。)

任何人都有想法我做错了什么?提前感谢您的帮助。

////////////////////////

// DrawTextTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#pragma comment (lib,"Gdiplus.lib")
#include <Windows.h>
#include <GdiPlusEnums.h>
#include <GdiPlusTypes.h>
#include <GdiPlus.h>
#include <iostream>
#include <string>

using namespace Gdiplus;
using namespace std;

bool RunTheTest(string);
bool SaveBitmapToFile(HDC, HBITMAP, BITMAPINFO&, string);
bool WriteBitmapFile(BYTE*, BITMAPINFOHEADER&, LPCTSTR);
void ShowError();

#define BUFFER_SIZE 1024

int _tmain(int argc, _TCHAR* argv[])
{
    bool bRet(false);
    char szBuff[BUFFER_SIZE];
    size_t retVal;
    string sFileName, sUserInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartupInput gdiplusStartupInput;

    if (argc > 1)
    {
        for (int nArg = 1; nArg < argc; nArg++)
        {
            wcstombs_s(&retVal, szBuff, (size_t)BUFFER_SIZE,  argv[nArg], (size_t)BUFFER_SIZE );
            if (nArg > 1)
                sFileName += " ";
            sFileName += szBuff;
        }
    }
    else
    {
        cout << "Input the full path filename (do not enclose in quotes)" << endl;
        cin >> sUserInput;
        sFileName += sUserInput;
    }
    Status s = GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    bRet = RunTheTest(sFileName);
    GdiplusShutdown(gdiplusToken);
    if (bRet)
        return 0;
    return 1;
}

bool RunTheTest(string sfileName)
{
    BITMAPINFO bitmapInfo;
    bool bRet(false);
    Brush* pBlackBrush = new SolidBrush(Gdiplus::Color::Black);
    Brush* pWhiteBrush = new SolidBrush(Gdiplus::Color::White);
    double inchesPerMeter(39.3700787);
    float fontSize(30);
    Font* pFont(NULL);
    FontFamily* pFontFamily(NULL);
    FontStyle fs(FontStyleRegular);
    HBITMAP hMemoryBitmap(NULL);
    HDC hMemoryDC(NULL);
    HGDIOBJ hDefaultBitmap(NULL);
    int pelsPerMeter(0), resolution(240);
    int oldBkMode(0);
    LPTSTR pFileName(NULL);
    PointF origin(0, 0);
    SIZE sizeRect;
    Status s;

    // Initialize a memory device context compatible with the screen
    hMemoryDC = CreateCompatibleDC(NULL);

    // Prepare some values for creating a memory bitmap
    pelsPerMeter = (int)(resolution * inchesPerMeter);
    sizeRect.cx = sizeRect.cy = 400;

    // Create the memory bitmap
    bitmapInfo.bmiHeader.biBitCount = 24;
    bitmapInfo.bmiHeader.biCompression = BI_RGB;
    bitmapInfo.bmiHeader.biPlanes = 1;
    bitmapInfo.bmiHeader.biXPelsPerMeter = pelsPerMeter;
    bitmapInfo.bmiHeader.biYPelsPerMeter = pelsPerMeter;
    bitmapInfo.bmiHeader.biSize = sizeof(bitmapInfo.bmiHeader);
    bitmapInfo.bmiHeader.biWidth = sizeRect.cx;
    bitmapInfo.bmiHeader.biHeight = sizeRect.cy;
    hMemoryBitmap = CreateCompatibleBitmap(hMemoryDC, sizeRect.cx, sizeRect.cy);
    hDefaultBitmap = SelectObject(hMemoryDC, hMemoryBitmap);

    // Draw a white rectangle on the bitmap
    SelectObject(hMemoryDC, GetStockObject(WHITE_BRUSH));
    Rectangle(hMemoryDC, 0, 0, sizeRect.cx, sizeRect.cy);
    SelectObject(hMemoryDC, GetStockObject(NULL_BRUSH));

    // Set bitmap background mode to transparent mode
    oldBkMode = SetBkMode(hMemoryDC, TRANSPARENT);

    // Get a Graphics object from the memory device context
    Graphics graphics(hMemoryDC);

    // draw a black rectangle on the bitmap
    s = graphics.FillRectangle(pBlackBrush, 0, 0, 400, 400);
    if (s != Ok)
    {
        cout << "FillRectangle failed" << endl;
        return false;
    }

    // draw white text on the black rectangle using the font we created
    s = graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
    if (s != Ok)
    {
        cout << "SetTextRenderingHint failed" << endl;
        return false;
    }

    // Create a font object and draw the text on the bitmap
    pFontFamily = new FontFamily(L"Arial");
    if (pFontFamily == NULL)
    {
        cout << "new FontFamily failed" << endl;
        return false;
    }
    pFont = new Font(pFontFamily, fontSize, fs);
    if (pFont == NULL)
    {
        cout << "new Font failed" << endl;
        if (pFontFamily != NULL)
            delete pFontFamily;
        return false;
    }

    s = graphics.DrawString(L"TEST STRING", 11, pFont, origin, pWhiteBrush);
    if (s == Ok)
    {   // Save the bitmap to a file
        bRet = SaveBitmapToFile(hMemoryDC, hMemoryBitmap, bitmapInfo, sfileName);
    }

    // Clean up
    SetBkMode(hMemoryDC, oldBkMode);
    if (pFont != NULL)
        delete pFont;
    if (pFontFamily != NULL)
        delete pFontFamily;
    if (hDefaultBitmap != NULL && hMemoryBitmap != NULL)
        SelectObject(hMemoryDC, hDefaultBitmap);
    if (hMemoryBitmap != NULL)
        DeleteObject(hMemoryBitmap);
    if (hMemoryDC != NULL)
        DeleteDC(hMemoryDC);
    return bRet;
}

bool SaveBitmapToFile(HDC hMemoryDC, HBITMAP hMemoryBitmap, BITMAPINFO& bitmapInfo, string sFileName)
{
    bool bRet(false);
    DWORD dwBmpSize(0);
    BYTE* pBytes(NULL);
    ULONG ulcb(0);
    WCHAR wchBuff[BUFFER_SIZE];
    PSTR pTemp = (PSTR)wchBuff;

    for (unsigned int nChar = 0; nChar < sFileName.length(); nChar++)
    {
        *pTemp++ = sFileName[nChar];
        *pTemp++ = 0;
    }
    *pTemp++ = 0;
    *pTemp = 0;

    dwBmpSize = ((bitmapInfo.bmiHeader.biWidth * bitmapInfo.bmiHeader.biBitCount + 31) / 32) * 4 * bitmapInfo.bmiHeader.biHeight;
    pBytes = new BYTE[dwBmpSize];
    bRet = (GetDIBits(hMemoryDC, hMemoryBitmap, 0, bitmapInfo.bmiHeader.biHeight, &pBytes[0], &bitmapInfo, DIB_RGB_COLORS) != 0);
    if (bRet)
        bRet = WriteBitmapFile(pBytes, bitmapInfo.bmiHeader, wchBuff);
    if (pBytes != NULL)
    {
        delete[] pBytes;
        pBytes = NULL;
    }
    return bRet;
}

bool WriteBitmapFile(BYTE* pBitmapBits, BITMAPINFOHEADER& bmpInfoHeader, LPCTSTR lpszFileName)
{
    BITMAPFILEHEADER bfh = {0};
    bool bRet(false);
    DWORD dwcb2Write(0), dwcbWritten(0);

    // This value should be values of BM letters i.e 0×4D42
    // 0x4D = M 0x42 = B storing in reverse order to match with endian
    bfh.bfType=0x4D42;
    // or...
    // bfh.bfType = ‘B’+(’M’ << 8);
    // <<8 used to shift ‘M’ to end

    // Offset to the RGBQUAD
    bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
    bfh.bfSize = bfh.bfOffBits + bmpInfoHeader.biSizeImage;

    HANDLE hFile = CreateFile(lpszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        cout << "CreateFile failed" << endl;
        ShowError();
        return false;
    }
    dwcb2Write = sizeof(bfh);
    bRet = (WriteFile(hFile, &bfh, dwcb2Write, &dwcbWritten, NULL) != 0);
    if (!bRet || (dwcbWritten != dwcb2Write))
    {
        cout << "WriteFile failed" << endl;
        ShowError();
        CloseHandle(hFile);
        return false;
    }
    dwcb2Write = sizeof(bmpInfoHeader);
    bRet = (WriteFile(hFile, &bmpInfoHeader, dwcb2Write, &dwcbWritten, NULL) != 0);
    if (!bRet || (dwcbWritten != dwcb2Write))
    {
        cout << "WriteFile failed" << endl;
        ShowError();
        CloseHandle(hFile);
        return false;
    }
    dwcb2Write = bmpInfoHeader.biSizeImage;
    bRet = (WriteFile(hFile, pBitmapBits, dwcb2Write, &dwcbWritten, NULL) != 0);
    if (!bRet || (dwcbWritten != dwcb2Write))
    {
        cout << "WriteFile failed" << endl;
        ShowError();
        CloseHandle(hFile);
        return false;
    }
    CloseHandle(hFile);
    return true;
}

void ShowError()
{   // Retrieve the system error message for the last-error code
    char szBuff[BUFFER_SIZE];
    LPTSTR lpMsgBuf;
    size_t retVal;
    DWORD dw = GetLastError(), dwRet(0);

    dwRet = FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    wcstombs_s(&retVal, szBuff, (size_t)BUFFER_SIZE, lpMsgBuf, (size_t)BUFFER_SIZE );

    // Display the error message
    cout << szBuff << endl;

    LocalFree(lpMsgBuf);
}

2 个答案:

答案 0 :(得分:2)

首先使用Graphics :: FillRectangle()绘制矩形。从Graphics :: MeasureString()获取所需的矩形大小。

答案 1 :(得分:1)

不幸的是你没有描述你的结果是什么,但我可以从我自己的经验中看出,将Win32-Calls与GDI-Plus混合有时会出现问题。我有相反的效果:使用GDI-Plus Win32 DrawText绘制填充区域后有时没有效果。 所以我的建议是将GDIPlus用于所有呼叫而不是混合在Win32呼叫中。

GDIPLus是一团糟。它的C ++标题完全不完整,缺少一些有用的东西。