Solved

# Color Depth and Sent Bitmap to array (Picture Box)

Posted on 2005-05-12
340 Views
Dear Expert
I Need to Decrease Color depth of Picture in pictureBox (240*128) to black & white color and sent to array in memory.
Pls Help me.
this very urgent.....
0
Question by:knun

Author Comment

I have Picture in Picturebox already i just need to change to black and white color depth (1 bit) , and after change to 1 bit depth sent this picture (240*128 Pixel) to array .
for read data inside and sent to LCD by my module.
0

LVL 13

Accepted Solution

This is a very simple way to do it: paste a picture box in a form, load a 240 * 128 pixels image on it and run this code:

Option Explicit

Private Sub Command1_Click()

Dim x As Long, y As Long
Dim Clr As Long
Dim Pixels(0 To 239, 0 To 127) As Boolean

Picture1.ScaleMode = vbPixels

For x = 0 To 240 - 1
For y = 0 To 128 - 1
Clr = Picture1.Point(x, y)
Clr = To1Bit(Clr)
Picture1.PSet (x, y), Clr
Pixels(x, y) = IIf(Clr = vbWhite, True, False)
Next
Next

End Sub

Private Function To1Bit(Clr As Long) as Long

Dim r As Long, g As Long, b As Long

Const HARDNESS = 150

'Debug.Print Hex(Clr)
r = (Clr And &HFF0000) / 2 ^ 16
g = (Clr And &HFF00&) / 2 ^ 8
b = (Clr And &HFF&)

To1Bit = IIf((r + g + b) / 3 > HARDNESS, vbWhite, vbBlack)

End Function
0

Author Comment

Very Thank rettiseert.
i will accept your answer but another question ^_^ do you know how to resize image to 240*128 ^_^
0

LVL 13

Expert Comment

Dim FileName As String

FileName = "C:\1.jpg"  '<---- This is the file you want to show in a 240*128 picture box

Picture1.AutoRedraw = True

Picture1.PaintPicture LoadPicture(FileName), 0, 0, Me.ScaleX(240, vbPixels, Me.ScaleMode), Me.ScaleX(128, vbPixels, Me.ScaleMode)
0

Author Comment

thank rettiseert ,  but i don't want to operate image to file (Harddisk) just play with it in memory
0

LVL 13

Expert Comment

ok, in that case you can load your picture at design time in a hidden picturebox and then, in your visible picturebox use something like:

Picture1.AutoRedraw = True

Picture1.PaintPicture HiddenPictureBox.Picture, 0, 0, Me.ScaleX(240, vbPixels, Me.ScaleMode), Me.ScaleX(128, vbPixels, Me.ScaleMode)
0

Author Comment

Very thank rettiseert.
you help me so much.
0

## Featured Post

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…