CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    277

    writing text to bitmap

    Hello, I am playing around with the CDib class from Inside Visual C++ (also available here ), and I am having a slight problem. What I wish to do is write some simple text, and save it as a bitmap file.

    For example I want to write text "10", and just save it to a bitmap file.

    I have this:

    Code:
    // Allocate memory for the bitmap
        CDib* textDib = new CDib(CSize(20, 20), // size of the bitmap
                                        24);                   // bits per pixel
    
        CString text;
        int value = 10;
        text.Format("%d", value);
    
        this->writeTextOnBitmap(text, *textDib, 20, 20);
    
        CString pFileName;
        pFileName.Format(_T("C:\\btest.bmp"));
        CFile textureFile(pFileName, CFile::modeCreate | CFile::modeReadWrite);
        textDib->Write(&textureFile);
        textureFile.Close();
    and writeTextOnBitmap:

    Code:
    void CbtestDlg::writeTextOnBitmap(const CString& text, CDib& textDib, int width, int height)
    {
        CDC* pDC = GetDC();
        CDC dccomp;
        dccomp.CreateCompatibleDC(pDC);
        ReleaseDC(pDC);
    
        // allocate memory for bitmap
        HBITMAP textBitmap = textDib.CreateSection(&dccomp);
    
        RECT rct = {0, 0, width, height};
    
        LOGFONT lf;
        memset(&lf, 0, sizeof(lf));
        lf.lfHeight = -MulDiv(12, pDC->GetDeviceCaps(LOGPIXELSY), 72);
        lf.lfWeight = FW_NORMAL;
        lf.lfOutPrecision = OUT_TT_ONLY_PRECIS;
    
        // create and select it
        CFont newFont;
        if (!newFont.CreateFontIndirect(&lf))
            return;
        CFont* pOldFont = pDC->SelectObject(&newFont);
    
        //dccomp.DrawText(text, &rct, DT_INTERNAL | DT_LEFT);
        BOOL ret = dccomp.TextOut(0, 0, text);
    
        BYTE* textBits = textDib.m_lpImage;
    
        for(int xIndex = 0; xIndex < width; xIndex++)
        {
            for(int yIndex = 0; yIndex < height; yIndex++)
            {
                COLORREF ref = dccomp.GetPixel(xIndex, yIndex);
                BYTE red, green, blue;
                red = GetRValue(ref);
                blue = GetBValue(ref);
                green = GetGValue(ref);
    
                *(textBits++) = blue;
                *(textBits++) = green;
                *(textBits++) = red;
            }
        }
    
        pDC->SelectObject(pOldFont);
    	newFont.DeleteObject();
    }
    My problem is that the saved bitmap is just completely transparent (in explorer preview) or white (in the image viewer). I am not sure. The file is 1.22 kb big, and has 20x20 size.

    what am I doing wrong?

    Thanks for all your help,

    Latem
    Being a pessimist is wonderful; you are either proven right, or pleasantly surprised.

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: writing text to bitmap

    Did you set the text and background color?

    Read MSDN CDC::SetTextColor() and CDC::SetBkColor().
    Nobody cares how it works as long as it works

  3. #3
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    277

    Re: writing text to bitmap

    Thanks for the reply.
    Yes, I've tried adding for example
    Code:
    dccomp.SetTextColor(RGB(0, 125, 250));
    dccomp.SetBkColor(RGB(0, 255, 250));
    dccomp.SetBkMode(OPAQUE);
    before the textout line, but it made no difference. same result.

    EIDT: and I don't think that could be the issue because if I just do:

    CDC* pDC = GetDC();
    pDC->TextOut(10, 10, "10");

    It draws black text "10" on white background on my dialog. Which is what I would expect.
    Last edited by Latem; December 11th, 2006 at 01:00 PM.
    Being a pessimist is wonderful; you are either proven right, or pleasantly surprised.

  4. #4
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: writing text to bitmap

    Try this:

    Code:
    	CFont font;
    	LOGFONT LogFont;
    
    	LogFont.lfStrikeOut = 0;
    	LogFont.lfUnderline = 0;
    	LogFont.lfHeight = 42;
    	LogFont.lfEscapement = 0;
    	LogFont.lfItalic = TRUE;
    
    	font.CreateFontIndirect(&LogFont);
    	CFont *pFont = dccomp.SelectObject(&font);
    	dccomp.TextOut(20, 18, "Test", 14);
    
    	dccomp.SelectObject(pFont);
    	font.DeleteObject();
    Nobody cares how it works as long as it works

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: writing text to bitmap

    Quote Originally Posted by Latem
    ...what am I doing wrong?
    One thing jumps out: you are using pDC AFTER you've released it. Not sure if that is your only problem...
    You have a multi-step GDI operation here, it's hard to debug without proper tools. You could try my FeinViewer add-in to inspect each of your GDI objects on every step (available here )
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  6. #6
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    277

    Re: writing text to bitmap

    If I just use pDC, and dont create a new CDC using CreateCompatibleDC(), it draws on the screen, and the bitmap is saved, although the saved bitmap is rotated.

    All I wish to do is write a bitmap that is of certain size, and has text written on it. It seriously cant be (or at least it shouldn't be) really difficult? I've been hammering with this all day. I've tried CMetaFileDC and I couldn't get it to work. The problem with using GetDC, or any device context that you create from GetDC, is that its the full window.

    Cant I say (in generic terms) for example: I want drawing area or canvas, and i want to paint things on it, and then i want to output it? To me the "outputting" is the only device dependent part...

    But I can't find a way to specify that I would just like a 20x20 canvas anyway though the device context API. I thought CMetaFileDC seemed like it should work, but the saved metafile is always 0kb, and of course nothing ever happens when I play it. I use the same code as I do with the code that does work except instead of using a pDC that I get from GetDC(), I create a CMetaFileDC object. In the end I close it, which returns a HMETAFILE handle, that can be played. But its like the nothing was saved. The textout didn't do anything... The docs says with CMetaFileDC, you can only use GDI commands that create output, which I think textout is?

    Any help is much appreciated.

    Latem
    Last edited by Latem; December 11th, 2006 at 03:42 PM.
    Being a pessimist is wonderful; you are either proven right, or pleasantly surprised.

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: writing text to bitmap

    Quote Originally Posted by Latem
    But I can't find a way to specify that I would just like a 20x20 canvas anyway though the device context API.
    You could create compatible bitmap with the 20x20 size and select it into your compatible DC. That would effectively make your DC 20x20. All drawings could be later saved via that bitmap.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured