Results 1 to 11 of 11

Thread: [RESOLVED] Extract Picturebox contents to a hex string

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    89

    Resolved [RESOLVED] Extract Picturebox contents to a hex string

    I have a picturebox on which I draw various lines, shapes and text.
    Now I need to get all that picture data into a hex string.

    I know, I can use "SavePicture" to save the picturebox to a .bmp,
    then I can read the .bmp file byte by byte, and convert each to hex.

    BUT......
    I do NOT want to use a file/temp storage..... I want to merely extract the
    data inside my app, without using a temp .bmp file.

    I can do this with code I found http://www.vbforums.com/showthread.p...ight=image+hex, IF the picturebox contains an image...
    but if it only contains graphics that code does not work.

    I know it must be do-able, but I am stymied.

    TIA

    Update:
    I suppose I could use a Bitmap instead of a Picturebox, and use the graphic methods on that.
    In reality the user does not see the results of what I am doing.

    So anyone with any help in extracting the contents of a Bitmap to a string ?
    Last edited by Antithesus; Apr 30th, 2008 at 06:10 PM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    89

    Re: Extract Picturebox contents to a hex string

    OK, let's look at this from a different perspective...

    Fundamentally what I want/need is the capability to execute the
    SavePicture statement, but to save the info to a string,
    rather than to a file.

    Any thoughts, ideas, or ????

  3. #3
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Extract Picturebox contents to a hex string

    Let's see if I get this straight.

    You want to be able to literally draw into a Picturebox control, wheather you draw directly onto the control or you draw additionally onto a bitmap already in the Picturebox. Then you want to be able to take the total results and save it in a binary string. Correct?

    Question:

    Do you want the string to be only the pixels from the Picturebox or do you want the string to be a bitmap so that when loaded into another Picturebox or even loaded into a image editor it will show everything you did to it?

    So do I get the idea you want to, say, make your own image editor like application (like MS Paint, for example)?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    89

    Re: Extract Picturebox contents to a hex string

    JMS,
    you are correct.

    In reality, I am building my own "PrintPreview" facility, with the ability to export the results to a PDF and/or RTF.

    I know, there is already one which exists, called "PrintPreview", and I have purchased it, and am using it in my application. All was fine till my users started to run the app in Vista... when the app ends, Vista throws an error message screen.. which is unacceptable to them and me.

    I have been in touch with the developer of PP since December about this, but he seems unable (unwilling ?) to fix it.

    Considering there is no other facility that I can find (I have spent MONTHS researching), I decided to build my own.

    So I have successfully been able to create a PDF and/or a RTF file.

    This facility will allow the app to "preview" any report, then allow for printing, or exporting to PDF or RTF. The report is capable of handling text (fonts, bold, italics, size, color, ROTATED, etc.), and shapes, and external picture files, and internal pictureboxes.

    The issue I have stems from the ability to handle ROTATED text for RTF.
    To do this, I have to place the rotated text onto a bitmap (picturebox),
    then do a "SavePicture" of that picturebox to a temporary file, then read that file as binary to get the hex representation into a string, then place that string into the RTF file.

    Hey... it works fine like this.... but I want to eliminate the need to write to a temp file, and read.... I want to go directly to a string, then do whatever I have to do to make it acceptable to RTF.

    So, whatever "SavePicture" does... except put to a string, rather than a file.

    Make sense ????

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    89

    Re: Extract Picturebox contents to a hex string

    I was mistaken in my first post.... I cannot find the logic I had earlier to extract the data from a picturebox, and put it into a string.
    And that link I have in my first post does NOT link to the logic I had found earlier. (My booboo)

    OK... I have spent days searching the web, and everywhere else I can think of.

    Can ANYONE point me in the direction to find code to read/extract the contents of a picturebox/image control (NOT a file), and get that data into a hex string ?

    ANYONE ?????

    PLEASE ????

  6. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Extract Picturebox contents to a hex string

    Can you confirm what you mean by Hex string. Do you mean raw data in string format or a hex string representation of the raw data. A hex string typically takes 6x the memory of the raw data it represents and requires decoding to get the true data, it's a pretty lousy way to store a bitmap.

    I'm assuming you want the raw data. You could do this with a DIBSection. You can get to the raw data of the PictureBox Image but it's format is dependent on the graphics card. If you copy to a DIBSection you can choose the format (eg 24bits per pixel, 32bpp, 16bpp etc) you will also have to emulate a bitmap header so whatever is reading the string knows what to do with it. Saving the picture to file is much easier. There might also be other options which are simpler than the DIB one.

    Can you post the Savepicture code that works.

    Edit: Here's a link about the Bmp file format
    Last edited by Milk; May 28th, 2008 at 06:00 AM.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    89

    Re: Extract Picturebox contents to a hex string

    I had found some code which actually is able to READ the contents of a picturebox.

    I can't remember how it did it... but it had something like looping through it,
    but I don't know if it was pixel by pixel, or whatever.

    Then for each byte returned/obtained, it would merely convert that byte to hex via a routine (which any of us could build).

    It then concatenated the result into a hex string.

    I need this, so I can insert that hex string into an RTF file which I am writing to, in the app.

    Fundamentally I am writing an RTF file from inside my app, including all the control strings, etc., AND the data.
    To be able to include an image/picture in the RTF, I am inserting the hex string of the image/picture.

    This all works..... but right now I am obtaining the hex info by:
    Saving the picturebox to a bmp file (SavePicture), and then reading that bmp file in binary mode, and converting those results to hex.
    This works. But I want to be able to eliminate the step of saving the picturebox to a bmp file, and then reading it in binary mode.
    I want to just get the picturebox contents DIRECTLY within my app.

    Like I said, I had found some code which does this, but I (stupid) lost it, and now, after spending DAYS searching, I can't find it.

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Extract Picturebox contents to a hex string

    What he wants is the same thing that he would get if he had used the SavePicture command but he wants it in a string without having to use the SavePicture command. In other words, he wants a string of hex codes that literally make up the complete bitmap picture just as if you opened up a bitmap from the hard drive and loaded it into a string.

    I just don't know yet how to go about doing that.

    Personally, I think he should just use the SavePicture command and then open it up into a string.

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Extract Picturebox contents to a hex string

    What's the big deal that you can't use the SavePicture command?

  10. #10
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Extract Picturebox contents to a hex string

    Well if you had posted the existing working code I would know what bpp the file is, I've chosen to use 24bpp which is pretty common. This should do what you want, you still have to convert to Hex. It includes the 54 byte file header (Windows Version 3) not just the pixel data.
    vb Code:
    1. Option Explicit
    2.  
    3. 'GDI DIB Structures
    4. Private Type BITMAP '24 bytes
    5.     bmType As Long
    6.     bmWidth As Long
    7.     bmHeight As Long
    8.     bmWidthBytes As Long
    9.     bmPlanes As Integer
    10.     bmBitsPixel As Integer
    11.     bmBits As Long
    12. End Type
    13.  
    14. Private Type RGBQUAD
    15.     Blue As Byte
    16.     Green As Byte
    17.     Red As Byte
    18.     Alpha As Byte
    19. End Type
    20.  
    21. Private Type BITMAPINFOHEADER '40 bytes
    22.     bmSize As Long
    23.     bmWidth As Long
    24.     bmHeight As Long
    25.     bmPlanes As Integer
    26.     bmBitCount As Integer
    27.     bmCompression As Long
    28.     bmSizeImage As Long
    29.     bmXPelsPerMeter As Long
    30.     bmYPelsPerMeter As Long
    31.     bmClrUsed As Long
    32.     bmClrImportant As Long
    33. End Type
    34.  
    35. Private Type BITMAPINFO
    36.     bmHeader As BITMAPINFOHEADER
    37.     bmColors As RGBQUAD '(0 To 255) As RGBQUAD
    38. End Type
    39.  
    40. 'BMP file header
    41. Private Type BMPFILEHEADER 'Not including the magic bytes (BM) in order to align
    42.    bmSizeFile As Long
    43.    bmReserved1 As Integer
    44.    bmReserved2 As Integer
    45.    bmDataOffset As Long
    46.    bmInfo As BITMAPINFO
    47. End Type
    48.  
    49. 'GDI Api
    50. Private Declare Function GetObjectA Lib "gdi32" (ByVal hObject As Long, ByVal nCount As Long, ByRef lpObject As Any) As Long
    51. Private Declare Function GetDIBits Lib "gdi32" (ByVal hDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
    52.  
    53. 'Memory Api
    54. Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    55.  
    56. Public Function GetImageFileBytes(ByVal Pic As PictureBox) As Byte()
    57. Dim BM As BITMAP, BMP As BMPFILEHEADER, ImgBytes() As Byte
    58. Const HEADERSZ As Long = 54
    59.  
    60.    GetObjectA Pic.Image, 24, BM
    61.    With BMP.bmInfo.bmHeader
    62.       .bmWidth = BM.bmWidth
    63.       .bmHeight = BM.bmHeight
    64.       .bmSize = 40
    65.       .bmPlanes = 1
    66.       .bmBitCount = 24 'Using 24bpp (3 bytes per pixel), so must make sure the scanlines align to a DWord
    67.    End With
    68.    With BMP
    69.       'scanlines must align to DWords... (BM.bmWidth * 3 + 3) And &HFFFFFFFC gives the scanwidth in bytes
    70.       .bmSizeFile = ((BM.bmWidth * 3 + 3) And &HFFFFFFFC) * BM.bmHeight + HEADERSZ  'Total size of file
    71.       .bmDataOffset = HEADERSZ
    72.       ReDim ImgBytes(.bmSizeFile - 1)
    73.    End With
    74.  
    75.    'Extract the pixel data
    76.    GetDIBits Pic.hDC, Pic.Image.Handle, 0, BM.bmHeight, ImgBytes(HEADERSZ), BMP.bmInfo, 0
    77.    'GetDIBits conveniently fills in the rest of BMP.bmInfo
    78.    CopyMemory ImgBytes(2), BMP, 52 'copy the header
    79.    'finally add the magic bytes
    80.    ImgBytes(0) = 66 'B
    81.    ImgBytes(1) = 77 'M
    82.    
    83.    GetImageFileBytes = ImgBytes
    84.  
    85. End Function
    There might well be an easier way than this but this is all I can think of. (To copy the code quote this post and copy the code from there)

    Edit:
    Quote Originally Posted by jmsrickland
    What's the big deal that you can't use the SavePicture command?
    I know what you mean, seems like a lot of effort to avoid writing to disk. It should be substantially faster for one reason, and it's always good to avoid writing to /reading from disk if it's not necessary.
    Last edited by Milk; May 28th, 2008 at 02:09 PM. Reason: spellage

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    89

    Re: Extract Picturebox contents to a hex string

    Milk,
    YES, YES, YES !!!!!!

    You are a lifesaver.... thankyou, thankyou, thankyou

    This looks very much (I think) like the code I had found/tried before (and lost).

    Yes... since I am building a "printpreview" facility, and need to be able to export the results to PDF and/or RTF, and need to be able to accommodate pictures in the report, I need to provide for the ability to convert pictures into RTF hex strings.

    In addition, to accommodate text rotation in RTF, I must assign the rotated text to a temporary (invisible) picturebox (there is no text rotation capability that I am aware of fir RTF), and then include the contents of that picturebox into the resultant RTF file.

    Yes, writing to a temporary bmp file, then reading it would be much simpler,
    but I do NOT want to impose this disk I/O on my users.
    And, I think this way (in memory) is much faster.

    Thanks again

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