|
-
Apr 30th, 2008, 02:31 PM
#1
Thread Starter
Lively Member
[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.
-
May 1st, 2008, 09:55 AM
#2
Thread Starter
Lively Member
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 ????
-
May 1st, 2008, 10:14 AM
#3
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)?
-
May 1st, 2008, 01:54 PM
#4
Thread Starter
Lively Member
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 ????
-
May 27th, 2008, 09:32 PM
#5
Thread Starter
Lively Member
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 ????
-
May 28th, 2008, 05:55 AM
#6
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.
-
May 28th, 2008, 10:05 AM
#7
Thread Starter
Lively Member
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.
-
May 28th, 2008, 10:07 AM
#8
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.
-
May 28th, 2008, 10:09 AM
#9
Re: Extract Picturebox contents to a hex string
What's the big deal that you can't use the SavePicture command?
-
May 28th, 2008, 12:03 PM
#10
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:
Option Explicit
'GDI DIB Structures
Private Type BITMAP '24 bytes
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Private Type RGBQUAD
Blue As Byte
Green As Byte
Red As Byte
Alpha As Byte
End Type
Private Type BITMAPINFOHEADER '40 bytes
bmSize As Long
bmWidth As Long
bmHeight As Long
bmPlanes As Integer
bmBitCount As Integer
bmCompression As Long
bmSizeImage As Long
bmXPelsPerMeter As Long
bmYPelsPerMeter As Long
bmClrUsed As Long
bmClrImportant As Long
End Type
Private Type BITMAPINFO
bmHeader As BITMAPINFOHEADER
bmColors As RGBQUAD '(0 To 255) As RGBQUAD
End Type
'BMP file header
Private Type BMPFILEHEADER 'Not including the magic bytes (BM) in order to align
bmSizeFile As Long
bmReserved1 As Integer
bmReserved2 As Integer
bmDataOffset As Long
bmInfo As BITMAPINFO
End Type
'GDI Api
Private Declare Function GetObjectA Lib "gdi32" (ByVal hObject As Long, ByVal nCount As Long, ByRef lpObject As Any) As Long
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
'Memory Api
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Function GetImageFileBytes(ByVal Pic As PictureBox) As Byte()
Dim BM As BITMAP, BMP As BMPFILEHEADER, ImgBytes() As Byte
Const HEADERSZ As Long = 54
GetObjectA Pic.Image, 24, BM
With BMP.bmInfo.bmHeader
.bmWidth = BM.bmWidth
.bmHeight = BM.bmHeight
.bmSize = 40
.bmPlanes = 1
.bmBitCount = 24 'Using 24bpp (3 bytes per pixel), so must make sure the scanlines align to a DWord
End With
With BMP
'scanlines must align to DWords... (BM.bmWidth * 3 + 3) And &HFFFFFFFC gives the scanwidth in bytes
.bmSizeFile = ((BM.bmWidth * 3 + 3) And &HFFFFFFFC) * BM.bmHeight + HEADERSZ 'Total size of file
.bmDataOffset = HEADERSZ
ReDim ImgBytes(.bmSizeFile - 1)
End With
'Extract the pixel data
GetDIBits Pic.hDC, Pic.Image.Handle, 0, BM.bmHeight, ImgBytes(HEADERSZ), BMP.bmInfo, 0
'GetDIBits conveniently fills in the rest of BMP.bmInfo
CopyMemory ImgBytes(2), BMP, 52 'copy the header
'finally add the magic bytes
ImgBytes(0) = 66 'B
ImgBytes(1) = 77 'M
GetImageFileBytes = ImgBytes
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:
 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
-
May 28th, 2008, 01:36 PM
#11
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|