Jump to content
  • Advertisement
Sign in to follow this  
HellzGod

How to convert a string to a bitmap? C++

This topic is 3969 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a problem. I want to get the text entered by the user in an edit box control to a bitmap. I'm using Visual C++. How do I convert it? No MFCs, please. Thanks, M

Share this post


Link to post
Share on other sites
Advertisement
You can't convert an apple to an orange...
If the string contained bitmap data, then this would let you access that data as if it were a bitmap... But I'm guessing that's not what you meant?
std::string userInput = "foo";
class Bitmap;
Bitmap* = (Bitmap*)userInput.c_str();

Do you mean that you want to create a new bitmap, and then render the contents of the string into it?
If that's the case, you might find a 3rd-party library like Image Magick handy.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
Do you mean that you want to create a new bitmap, and then render the contents of the string into it?


That's EXACTLY what I want. I cannot use any third party libraries. Is there any other way?

Thanks,
M

Share this post


Link to post
Share on other sites
Quote:
Original post by HellzGod
Quote:
Original post by Hodgman
Do you mean that you want to create a new bitmap, and then render the contents of the string into it?


That's EXACTLY what I want.


In what colour, font and size? And why?

Quote:
I cannot use any third party libraries.


Why not?

Quote:
Is there any other way?


Yes; you can duplicate the work done by one of the third party libraries.

Hint: It's a lot of work, even for the small amount of functionality you're actually going to use.

Share this post


Link to post
Share on other sites
Quote:
Original post by HellzGod
I'll have to ask for permission to download libraries.
Then just ask. The worst that they can say is no. In which case you will have to duplicate the functionality provided in the other library. On the other hand they might say yes. In which case you have just saved yourself a ton of work.

Share this post


Link to post
Share on other sites
Quote:
Original post by HellzGod
Hi,
I'll have to ask for permission to download libraries.

From who? If it's your employer, then you can justify the downloads by the time they'll save you. If it's a teacher, then you can tell him that working with libraries is an important aspect of programming these days. Not to mention that it's often just the smarter approach.

Share this post


Link to post
Share on other sites
Isn't it just a case of:
  • Determine the size of the text (GetTextExtentPoint32)
  • Create a compatible DC (CreateCompatibleDC)
  • Create a bitmap (CreateBitmap)
  • Select the standard GUI font into the device context (SelectObject(GetStockObject(DEFAULT_GUI_FONT)))
  • Draw the text to it (DrawText)
  • Save the bitmap (You'll need to write the code for that, but it's pretty easy).

    It's not that much work really.

    Share this post


    Link to post
    Share on other sites
    I've limited the scope a bit. What if I use a Rich Edit Control (I've no idea of that either).

    Anyway, this is what I do:

    [Source]

    HBITMAP hBmp;

    void WriteTextToBMP ( HWND hRichEdit )
    {
    HDC hdc = GetDC(hRichEdit);
    HDC hMemDC = CreateCompatibleDC(hdc);
    hBmp = CreateCompatibleBitmap(hdc,MAXWIDTH,MAXHEIGHT);
    HBITMAP hBmpOld = SelectObject(hMemDC,hBmp);
    BitBlt(hdc,0,0,MAXWIDTH,MAXHEIGHT,hMemDC,0,0,SRCCOPY);
    SelectObject(hMemDC,hBmpOld);
    DeleteDC(hMemDC);
    ReleaseDC(hdc,NULL);


    }

    [/Source]


    I use hBmp elsewhere. I'm not getting any change. Is the above code correct?
    I have invoked this in WM_PAINT. Is that ok?

    Thanks,
    M

    Share this post


    Link to post
    Share on other sites
    Sign in to follow this  

    • Advertisement
    ×

    Important Information

    By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

    GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

    Sign me up!