How can I save contents of a TEXT BOX to a BITMAP?

Posted on 2006-04-27
Last Modified: 2010-04-07
I am having a user enter text into a TEXTBOX control.
I then want to have them press a button and the contents will be saved to a BITMAP'd version of the contents, same dimentions as the text box.
Can someone show me code that will do that?
Question by:yyuubum2
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Project has a Textbox, CommandButton and a PictureBox:

    Option Explicit

    Private Declare Function GetWindowDC Lib "user32" _
        (ByVal hwnd As Long) As Long
    Private Declare Function ReleaseDC Lib "user32" _
        (ByVal hwnd As Long, ByVal hdc As Long) As Long
    Private Declare Function BitBlt Lib "gdi32" _
        (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, _
        ByVal nWidth As Long, ByVal nHeight As Long, _
        ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
        ByVal dwRop As Long) As Long

    Private Sub Command1_Click()
        Me.ScaleMode = vbPixels
        Picture1.Width = Text1.Width
        Picture1.Height = Text1.Height
        Picture1.BorderStyle = 0 ' none
        Picture1.Appearance = 0 ' flat
        Picture1.AutoRedraw = True
        Dim textDC As Long
        textDC = GetWindowDC(Text1.hwnd)
        BitBlt Picture1.hdc, 0, 0, Picture1.Width, Picture1.Height, textDC, 0, 0, vbSrcCopy
        ReleaseDC Text1.hwnd, textDC
        SavePicture Picture1.Image, "c:\someImage.bmp"
    End Sub

    Author Comment

    that is sweet...very nice.
    thanks !

    Author Comment

    Just another question...would there be a lot for you to ammend it to work
    with a Rich Text Box?
    LVL 85

    Accepted Solution

    Just change "Text1.hwnd" to "RichTextBox1.hWnd":

        Dim myDC As Long
        myDC = GetWindowDC(RichTextBox1.hwnd)
        BitBlt Picture1.hdc, 0, 0, Picture1.Width, Picture1.Height, myDC, 0, 0, vbSrcCopy
        ReleaseDC RichTextBox1.hwnd, myDC

    (or whatever the name or your richtextbox is...)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now