Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  HOME    TRACKER    Visual Basic

Saving Picturebox Image As Black & White (1-bit) Bitmap File

The GetBitmapBits function is giving me trouble. I have a program that generates reports and sends the output to a Picturebox for viewing and/or the Printer for printing (duh). So the customer can save a copy of the report, I use SavePicture on the Picturebox's Image property. A report of about 8 pages results in a large (up to 600 x 7000 pixel) bitmap. This file would be 10 megs when created with the SavePicture function. I hate wasting that much disk space (even if it is the customer's disk)! A 1-bit bitmap results in a file about 400KB, almost as compact as a gif image. Everything works, except the byte array returned by GetBitmapBits is not always Long-aligned. I have to adjust the bitmap width to get the scan lines padded on the 4-byte boundary. I think that it is Integer-aligning, but I can't seem to make a valid bitmap file unless the scan lines are multiples of 4 bytes. The code is below. I hope this thread posts.

'' General declarations section (omitted) has
'' BITMAP type and API functions declared

Private Sub SavePictureBW()
Dim hdcMono As Long, hbmpMono As Long, hbmpOld As Long, dxBlt As Long, dyBlt As Long, success As Long
Dim num As Integer
Dim bmpsrc As BITMAP
Dim bitmaparray() As Byte

' Picturebox's ScaleMode is set to pixels
' Get the dimensions
dxBlt = pbSrc.ScaleWidth
dyBlt = pbSrc.ScaleHeight

' Create memory device context
hdcMono = CreateCompatibleDC(0)

' Create monochrome bitmap
hbmpMono = CreateCompatibleBitmap(hdcMono, dxBlt, dyBlt)

' Use GetObject to fill bmpsrc with bitmap info
success = GetBitmapObject(hbmpMono, LenB(bmpsrc), bmpsrc)

' This code makes sure scan lines get padded correctly
' I would like to avoid changing the bitmap's width, but
' I can't figure out a better way

Do While (bmpsrc.bmWidthBytes Mod 4)

' Delete bitmap before creating another
Call DeleteObject(hbmpMono)

' Reduce bitmap width until it is Dword aligned
dxBlt = dxBlt - 1

' Create new mono bitmap
hbmpMono = CreateCompatibleBitmap(hdcMono, dxBlt, dyBlt)

' Use GetObject to fill bmpsrc with bitmap info
success = GetBitmapObject(hbmpMono, LenB(bmpsrc), bmpsrc)

hbmpOld = SelectObject(hdcMono, hbmpMono)

' Copy picturebox's bitmap to DC to create mono mask
' Bitmap must be flipped vertically with StretchBlt or byte array will be "upside-down"
success = StretchBlt(hdcMono, 0, 0, dxBlt, dyBlt, pbSrc.hdc, 0, dyBlt - 1, dxBlt, -dyBlt, SRCCOPY)

ReDim bitmaparray(1 To (bmpsrc.bmWidthBytes * bmpsrc.bmHeight))

' Fill the byte array with the bit data
success = GetBitmapBits(hbmpMono, UBound(bitmaparray), bitmaparray(1))

' Here you would create the bitmap's file header data
' and save it all to disk as the finished black & white bitmap

' Clean up
' Select old bitmap back into dc first
Call SelectObject(hdcMono, hbmpOld)
Call DeleteDC(hdcMono)
Call DeleteObject(hbmpMono)
MsgBox "done"
End Sub

Thanks for any help.

View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Faster Method Of Converting Picturebox To Black And White?
I've been trying to convert a (relatively small) image stored in a picture black to two bits -- Black and white.

This cord works:

For i = 0 To card1.ScaleWidth
For j = 0 To card1.ScaleHeight
If card1.Point(i, j) <> "16777215" Then card1.PSet (i, j), 0
Next j
Next i

However, it's slow. It takes 1-2 seconds for a tiny image.

I've tried

SetPixel card1.hDC, i, j, 0

But it turns the whole image black.

Any thoughts?

How To Convert Black And White Image To RGB?
How to convert black and white image to RGB?

Saving Graphics Into 1, 2 Bite Black&amp;white Format
Hi all. i really need to know, how to save my picture.its plain text on white background.i can create 24bite BMP but it is too large. so i would like to convert it into another format (W98 must suppose this format !!! ). i think the best format will be black and white bmp. but its up to U. please help. thanx

Saving Bitmap Info To File W/o Picturebox
I am working with large images in my VB application. I have all of the data stored in a User-defined array (i.e. the RGB values).
The picture box control cannot handle these large images too well so I am looking for a way to save it from the array to a binary file directly. (.bmp)
Is there a way to do this?

Saving Image From An OLE Control To A Bitmap File
What is the best way of saving an (bitmap) image in an OLE control to a bitmap (.bmp) file. This is the method I'm using but I get a "No object" error at the line,
"olePicture.SaveToFile (FileNumber)".

Dim FileNumber As Integer
Dim LocationAndFileName As String
FileNumber = FreeFile()
LocationAndFileName = App.Path & "Logo.bmp"

Open LocationAndFileName For Binary As #FileNumber

olePicture.SaveToFile (FileNumber)

Close #FileNumber

Saving Picturebox Image To File (eVB)

Does anyone know how to save a picturebox image to file using Microsoft eMbedded Visual Basic (VB for handheld's).

I've tried the common

VB Code:
SavePicture picture1, " emp.bmp"   ' is used rather than drive letters in eVB  

but I get the following error message....

"An error was encountered while running this program: Variable is undefined: 'SavePicture' "


How To Make White To Transparent In Bitmap File?
Hello, Everyone:
I tried to make white to transparent in Bitmap file. I would like the output transparent file still a Bitmap file? How can I achieve it? Would someone be kind to help me out? I have photoshop, Illustrator... However, I don't have Corel Draw... Is there any free software to do it?
Thank you very much!

Saving Picturebox As Bitmap
I am trying to save a picture box as a bitmap.

I am using the following code that was given to me, but it is saving an entirely black bitmap, and gets rid of the picture that was drawn in the picbox.

picDraw.Picture = picDraw.Image

With CommonDialog1
.filename = ""
.DialogTitle = "Save Image"
.Filter = "Save Image As Bitmap|*.bmp"
If Len(.filename) = 0 Then
Exit Sub
End If
filename = .filename
End With
SavePicture picDraw.Image, filename

any ideas?

Saving Bitmap In Picturebox As Gif Or Jpeg
how would i save a bitmap in picturebox as gif or jpeg??

Make Half A Form Fade From White To Black And The Other Half All Black
I've been looking all over the place for a way to fade the top half of a form fade from white to black and the bottom half all black. I guess I'm really tired or something but I can't seem to do it. Can someone please give me the code to do this? Thanks

PictureBox Saving Blank BITMAP...this Is A Tough One!
My problem is the same as mentioned here: thread222-98810 .  When the picturebox is saved, it saves a blank image.

I have a form containing a picturebox which has other objects (textboxes, a frame, labels, and lines) contained within it.  I want users to move the textboxes to any location within the picturebox (this works perfectly).  I want the users to be able to save the contents in the picturebox as an image (it saves, but as a blank image and all objects are left off).

I am using this command: SavePicture picturebox.Image, "c:myTestPicture.bmp"

What get saves is a blank image...the objects mentioned above do not get saved within the image.

I did a test and I placed code on the form allowing me to draw a line on the picturebox.  When I save it using the SavePicture command, the lines drawn at runtime actually get saved within the image...but the objects (textboxes and so on) do not.

I completed a search within this forum, and researched all entries about pictureboxes and bitmaps, and there is no answer to my problem.

Please let me know what I am doing wrong....

Thanks in advance!

Trouble Saving Picturebox Contents To Bitmap
I have a picturebox that I've filled in using PSet, Line and Circle. Everything looks fine. When I try to save the image to a bitmap file using
Code:SavePicture picMain.Image...
I end up with a nice bitmap full of background color (blank picturebox). After the save, the picture box is blank also. What am I missing? Thanks

Reading Bitmap Files In Memory, Resizing And Saving Bitmap File
I'm trying to cread a Visual Basic program to read a bitmap file into memory, Once the bitmap is in memeory I would like to resize the bitmap and write it out to a file.

PS. I do not wish to see the bitmap at this time.

PictureBox Troubles: Targa Format And Editing, Saving As Bitmap
Ooooohkay, here we go: I have discovered through trial and error the .tga truevision targa file format. I am currently loading .tga files into a picturebox with a simple loop and a picture1.pset (x,y),rgb(r,g,b) function. It is agonizingly slow. I've noticed that picture1 has a currentX and currentY function... should I be using that instead?

Secondly, once the picture is done, it doesn't stay on the picture box. When the window is moved off the screen, the pixels that go off the screen or are under another window are cleared. How can I make them stay? When i invoke the savepicture command it saves a blank bitmap. What can I do to:

1. Load faster
2. Save as a bitmap
3. Fix the picturebox pixel clearing problem


Black And White
i have a pic of 2 colors and i want to change it into black and white im using the following code

For x = 0 To Picture1.ScaleWidth
For y = 0 To Picture1.ScaleHeight
color1 = Picture1.Point(0, 0)
If Picture1.Point(x, y) = color1 Then
Picture1.PSet (x, y), RGB(0 , 0 , 0 )
Picture1.PSet (x, y), RGB(256, 256, 256)
End If


and the whole screen goes white with glitches of no change everyso often could someone tell me some possible causes why my if statements are not working in the code above??

Black And White .cur
Does anybody know why my .cur is appearing black and white when i run my program. The cursor is meant to be red.

Many thanks chris

Black Or White?
Is there any way to tell if a colour is light or dark?
For instance, if i have the LONG value of a colour and it is a dark colour, it will turn it black , or if it is light it will make it white.
Sorry if that explanation sucked lol i wasnt sure how to do it
If anyone can understand that, id really be grateful of some help

Black And White
how would i make the bckcolor af a command button blck and the forecolor white.....i c the back color but when i put it to black it just stays the same an i ont een c he fore color

Black Or White
I need to decide what font color to use: black or white. The decision should be based on the background color. If back color is light - print in black, otherwise - print in white. The back color can be any combination of red, blue, and green. I know how to get the "opposite" color.



Saves As Black And White
im creating a graph and using one of the tutorials for how to make a line graph and save it to a file. It is going to be a Com object to use with ASP so I can't use PictureBox.
Ive worked around this but it saves as a Black & White image, im guessing something to do with the palette.
So I need to know how to get colour, it is in colour if I put it straight to a picturebox

Im doing this
Dim picy As StdPicture

mem_dc = CreateCompatibleDC(hdc)
mem_bm = CreateCompatibleBitmap(mem_dc, wid, hgt)
orig_bm = SelectObject(mem_dc, mem_bm)

Set picy = hDCToPicture(mem_dc, 0, 0, wid, hgt)

SavePicture picy, "c:my.bmp"

I got hDCToPicture from

Any help would be greate, maybe im going around this the wrong way?


Printing In Black And White Only
Ive created a program and want to print in black and white only, otherwise my printer cannot print because i have no colour ink.

here's the coding for the print part so far: = "Script"
Printer.Font.Size = 14
Printer.Print name
Printer.Print FormatCurrency(amount, 2)
Printer.Print FormatDateTime(d, vbLongDate)

White Turns Black
I have a program that works fine on windows NT 4.0. When I placed the program on a windows XP system (Home, not professional), the parts of the program that are white (the backdrop) become black. I have no idea why this is happening, does anyone else?


How Do I Print In Black And White?
question's above!!

thanks every1

White &amp; Black DOS !!! **RESOLVED**
Does anyone know how to change the colors of DOS using VB6?

I know how to get its handle, but how do you change its color using VB6 code. I do not want to change it manually

look at picture

Detect Black On White

is there some way i can detect areas in a bmp file?

white background black foreground......i need to detect regions of blackon the image (after painting it into a picture box)......and put theresults in an array.


Black/white To Color Not Working
There are 2 things wrong: i get scattered spots of red dots and it saves in color even after I convert it to black and white.

Please look at the zip file. I included a file for you to test it on if you don't have one.

Icon Turns Black And White :(
I use VC++ to make an icon, under 256 colors and 32 X 32 in size. When I load it into my vb program, it comes out black and white.
Anybody know why? Thanks!

Converting Textures To Black-white
Does someone know how to black-white a texture? I must do this for the transparency... if I use the convert to alpha filter i get a gray texture but he must everythig that isn't white draw black..

Icons Problem (they Come Out All Black And White!)
I am having a problem with icons. I made it with ArtCursors. in explorer and all that, it has it's colors, but when i load it into my vb app, it is all black adn white (2 colors+transparency). I also tried using pixel toolbox to make it, and it was the same. ive even tried various pallete color amounts, nothing makes a difference in it. has anybody else had this problema nd know a way to fix it?

Black And White R-B-G Values For Bitblt
whats the black and white red blue green value that bitblt looks for?

Why Is Color Cursor Black And White?
In the zipped file attached, you see a color cursor as a seperate file. It is set as the MousePointer of my form. Yet, then the program runs and you move the mouse over the form, it's in Black and White instead of color! Any idea why? How can I fix this?

Differentia Black & White Pixels
anyone knows how to differentia the numbers of black & whites pixels in a black & white image?
then plot a histogram according to pixels?


Black&amp;white Cursor/icon
is there a max colour or size limit for icons/cursors in VB6? because most custom icons/cursors i use just come up in black and white...

Print Color In Black And White
Looking for quick way to change various light colors so they show up as black when printed.

Currently I change the background from LtGray to White prior to printing to save ink. This change then cause all other lighter colors to not show up or show poorly.

This process needs to be automated -- that is have code recognize color and change accordingly. That is anything other than background would become black -- however, this would cause background color labels NOT to show their text.

Was also hoping not to do this pixel by pixel.


Fading From Color To Black&amp;White
How can I fade a image in a picturebox from color to black&White?

Converting Jpg Files To Black And White
I have about jpg files to convert to black and white and i was wondering if i could find some code to do it for me.

Why Is My Mouse Pointer Black And White?
Why is my mouse pointer black and white? No matter what i change it to, it does not have color to it, can someone tell me how to fix t

Why Is My Mouse Pointer Black And White?
Why is my mouse pointer black and white? No matter what i change it to, it does not have color to it, can someone tell me how to fix t

Coverting Color BMP To Black & White BMP
I am using picture control to draw something at runtime. When I save the picture from picture box it always store the BMP using colors (I don't how many colors it uses). I want to save the BMP as Black & White image. But I am not getting the method to convrt it. As the file size is reduced drastically when I convert the image to Black and White (I checked it with Paint application in windows). Please somebody help me in this regard.
Thanks a lot

Prashant J.

Extracting Image From Picture Box And Saving As Bitmap
Is there a way to extract an image from a picture box and save it as a bitmap?

I am "fixing" a program written by someone else. They used picture boxes to store the background images for a user control. I don't have the actual bitmap files. I would like to get rid of the picture boxes and place the images in an image list control in order to save resources. How would I do this?

Thanks in advance,


How To Printout A Worddocument In Black-white Mode
Hi, everyone!!

Its possible to printout a document explicit in black-white or color mode?


How To Convert Colored Picture Into Black &amp; White
How Can I convert a colored picture into Black & white picture and also in Grayscale picture....

Is it possible using Visual Basic...?

Please Help ME

Printing White Text On A Black Background
I seem to have a very simple problem ..

How can I print a white text on a black background using the
printer object ?

I've tried almost everything...

Tanx for your reaction

How To Force A Print Job To Be Sent In Black/white To A Color Printer
Hi, gurus:

Please help me this. How to do it in VB or others?

How Can I Count The Number Of Black And White Pixels In A Picture.

Nr of black pixels = 12312
Nr of white pixels = 3453453

I hope this can be done, cause i really need it..

Saving A Bitmap,Gif Or JPEG Image To An Access 2000 Database
Can somebody help me with this one,

How do you save the contents of the Image Control as an embedded image in an Access 2000 database table, I've tried all weekend and just cannot work it out ?

help !

Picture Box Black/ Half Image With Rest As Black Part
I am using the picture box in one of my application in which i display image and after some time i unload the current image and load the next image and repeate this process continuesly, some times when i load the image the picturebox displays half image and he rest as the black or it shows entire images as black and to avoid this state i have to restart he os and then the image is loaded properly this problem is only witht Win 98 in W2K every thing is ok can any one help me out with a solution to avoid this problem the machine cnfiguraton is

P-3 533 MHz
180 MB Ram
Windows 98 SE

Mouse Movement Check (gestures) Like Opera Or Black&amp;white
Hi there,
I want to write a programm which checks the mouse movement (mouse gestures) like the webbrowser Opera or the game Black & White (AKA you press the right mouse button and move the mouse to the left and then a effect is started).
My question is whether anybody has an idea how to check complex objects like a circle or a square.


Icon In Color In Visual Basic Editor, Black And White When Running As Exe
I got a real weird one for you- simple but annoying. I have an icon for my program on the top left of the form- pretty straightforward. It looks great in the visual basic environment when I hit "play" and the program runs. When I compile it and run it as an exe, the icon is there but it is faded and NOT transparent?!?! I really don't want to start from scratch, strip out my code, and move all my controls to a new form. Any clues- see the attached image.

Edited by - qsir1 on 4/17/2004 3:32:36 PM

Copyright 2005-08, All rights reserved