Color Depth and Sent Bitmap to array (Picture Box)

Posted on 2005-05-12
Last Modified: 2008-02-01
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.....
Question by:knun

    Author Comment

    For more information
    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.
    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)
    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

    Author Comment

    Very Thank rettiseert.
    i will accept your answer but another question ^_^ do you know how to resize image to 240*128 ^_^
    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)

    Author Comment


    thank rettiseert ,  but i don't want to operate image to file (Harddisk) just play with it in memory
    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)

    Author Comment

    Very thank rettiseert.
    you help me so much.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Article by: Martin
    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…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now