Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using picurebox in class without a form 1

Status
Not open for further replies.
Oct 5, 1999
105
GB
I am writing a class, and I want to be able to generate a simple picture filled with a certain colour and save it as a .bmp using SavePicture.

However, as my class does not contain any forms, how do I create a PictureBox object.

I'm sure it must be very simple, if is is possble, but my brain can't think of a way!

Thanks
 
A Form is a special kind of Class which includes a UI. So consider using a Hidden/ invisible Form instead of a 'normal' Class.
 
If all you want is one color, you could constructed a text file as a 'bmp with the color bytes set to the required color.

My unsophisticated method:(others might have a better idea)
Using Paint, save a trial .bmp that has a mixed color of say Red 64, Green 70, Blue 80

Then examine the file contents by loading it into a byte array to see how the header bits are structured including the size of the picture.

The remaining bytes represent the color value of the pixels in the bmp. By observing the values you can see which one is blue etc,
For a=0 to 100:print a,MyArray(a):Next

To create a different colored bmp, just replace the bytes that represent the amount of color you want

Then save the new byte array and rename it as a .bmp

The following is the first bytes of a .bmp seen by wordpad.
BMf9 6 ( O =   09 PF@PF@PF@PF@PF@PF@PF@PF@PF@PF@PF@PF@PF@PF@PF@PF@PF@PF@PF@PF@PF@
You can see the ASC values of the color pixels P=80(Blue), F=70(Green), @=64(Red)
Unfortunately the range of byte values of a .bmp is from 0 to 255 so you cant really see everything properly with a text editor so you have to manipulate it in bytes.
 
Oh could be wrong, but I believe there are a set of API's that can handle all of your requirements... let me see... where is the vb6 and the api viewer...


Okay, there it is...(wow it still works!)


In help, on the index tab, type in createbitmap, at the bottom of that help page you should see the link to Bitmap Functions. Click on that and you will have what you need. However, you will need to do a little research on how to save it to disk.

Good Luck
 
Long time no see...?

Nice to have you back, vb5prgrmr, after a very long break.:)
 
If you don't need support on old systems this is short and easy, though it can be slow for large BMPs:

Code:
'Requires WIA 2.0 reference.

Private Sub MakeBMPFile( _
    ByVal SaveAsName As String, _
    ByVal Width As Long, _
    ByVal Height As Long, _
    Optional ByVal ARGBFill As Long = &HFF000000)
    
    Dim PixelX As Long
    
    With New WIA.Vector
        For PixelX = 1 To Width * Height
            .Add ARGBFill
        Next
        .ImageFile(Width, Height).SaveFile SaveAsName
    End With
End Sub
 
I have not been able to get WIA to work in Windows 7 (in particular the function that captures a webcam)
Have you and ideas on this?
 
Seems off-topic, but...

Windows Image Acquisition
WikiPedia said:
Windows Vista has the WIA Automation library built-in. Also, WIA supports push scanning and multi-image scanning. Push scanning allows initiating scans and adjusting scanning parameters directly from the scanner control panel. Multi-image scanning allows you to scan several images at once and save them directly as separate files. However, video content support is removed from WIA for Windows Vista. Microsoft recommends using the newer Windows Portable Devices API.
Windows 7 is really Vista R2. Neither support webcams as far as I can tell, capture seems to be limited to still cameras and scanners.
 
And sadly the WPD API (both the plain one and the automation wrapper) still doesn't play nice with VB6.
 
USB Web cameras still work fine in Vb6 using The old AVICap32.dll in Windows 7

The only problem I have so far is if I pull out the USB plug while the app is working, it freezes the computer (but not if I disconnectcam first)

The main problem is getting a suitable driver for Windows 7 for old cameras. Luckily some XP drivers will still work for some cameras Eg Logitec Lifecam.

If you can get the camera to work on the test app supplied with the camera, it will work in vb6.
 
>they're killing VS Express

Really? Oh dear ... ah, I see. Metro apps only. Dear oh dear. That certainly sends a very specific message: that, as far as Microsoft are concerned the desktop is history ...
 
Coming back to the original question, you can follow the structure of a bitmap file and write the file contents in binary format by yourself. Its not that hard to do that.

See the code below. As we need the target bitmap to have only one color, I have chosen to write a monochrome (2-color) bitmap. This reduces the size of the file to minimum (without compression) and still maintains its compatibility with VB.
___
[tt]
Option Explicit
Private Type BITMAPFILEHEADER
bfType As String * 2 'BM
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type
Private Type BITMAPINFOHEADER
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Private Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type

Private Sub WriteBitmap(Width As Long, Height As Long, Color As Long, File As String)
Dim FF As Integer, bfh As BITMAPFILEHEADER, bih As BITMAPINFOHEADER, Colors(1) As RGBQUAD
'parepare the bitmap info header
bih.biSize = Len(bih)
bih.biBitCount = 1
bih.biPlanes = 1
bih.biWidth = Width
bih.biHeight = Height
bih.biSizeImage = ((Width + 31) \ 32) * 4 * Height

'prepare the bitmap file header
bfh.bfType = "BM"
bfh.bfOffBits = Len(bfh) + Len(bih) + 8
bfh.bfSize = bfh.bfOffBits + bih.biSizeImage

'prepare the color table
Colors(0).rgbRed = Color And vbRed
Colors(0).rgbGreen = (Color And vbGreen) \ 256&
Colors(0).rgbBlue = (Color And vbBlue) \ 65536

'kill the file if already exists
If Dir$(File) <> vbNullString Then Kill File

'write file
FF = FreeFile
Open File For Binary As #FF
Put #FF, , bfh 'Bitmap file header
Put #FF, , bih 'Bitmap info header
Put #FF, , Colors 'Color table
'write pixel data (all pixels point to 0 index in color table)
Put #FF, , String$(bih.biSizeImage, 0)
Close #FF
End Sub

Private Sub Form_Load()
WriteBitmap 123, 456, 7890, "D:\test.bmp"
DoEvents
Set Picture = LoadPicture("D:\test.bmp")
End Sub[/tt]
 
Thanks all for all the useful suggestions, and Hypetia for such a simple solution. Have a star!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top