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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extraction of Picture box point / pixel colors. 3

Status
Not open for further replies.

Ken01

Programmer
Aug 8, 2014
60
GB
I am using the following code to examine the color of each point on a picture box and then changing it before
redisplaying as a different color on a 2nd picturebox. Although it works I was wondering if other colors outside the
RGB range could be utilised ? Also I have been unable to find any documention on the bTable command
(the original code was down-loaded from a shareware website).

-----------------------------------------------------------------------------------------------------------------------------------

vmin = 60: vmax = Picture3.ScaleHeight - 6: sx = 14
Picture1.Width = Picture1.Width - 130
Picture3.Left = Picture2.Left + 130: Picture3.Width = Picture3.Width + 240
For x1 = 7400 To 60 Step (sx * -1)
X2 = X2 + sx
For y1 = vmin To vmax Step sx
color = Picture1.Point(x1 - jx, y1 - jx)
color1 = Picture1.Point(X2, y1 - jx)

Rem Color = GetPixel(Picture1.hdc, x1, y1)
wx = wx + 1
R = ExtractR(color)
G = ExtractG(color)
B = ExtractB(color)
R = bTable(R)
G = bTable(G)
B = bTable(B)
R1 = ExtractR(color1)
G1 = ExtractG(color1)
B1 = ExtractB(color1)
R1 = bTable(R1): G1 = bTable(G1): B1 = bTable(B1)
G = G - 30: B = B - 30: R = R + 20
Picture3.PSet (Picture3.ScaleWidth - x1, y1), RGB(B, R, G)

rem End If
Rem SetPixelV Picture2.hdc, x1, y1, RGB(B, G, R)
List1.AddItem (color & " : " & x1 & " : " & y1 & " : " & " : " & R & " : " & G & " : " & B)
Next y1
Next x1
 
>colors outside the RGB range could be utilised

Perhaps you can clarify what you mean by this

>the bTable command

I suspect that bTable is an array; it certainly is not a native VB function
 
Dependant on your response to the above it is quite possible we can do this without peeking at every pixel individually via Windows' GDI+ API
 
>colors outside the RGB range could be utilised

Perhaps you can clarify what you mean by this

>the bTable command

I suspect that bTable is an array; it certainly is not a native VB function
Thankyou for the quick response. Yes, you are off course correct about BTable not being a native VB function. I posted this close to midnight UK time when I was dropping off to sleep.
What I meant re RGB was if there was a way of distinguishing a light red from a dark red (or in between shade) and ditto for blue and green ? The purpose of the program was to analyse a BMP or JPG loaded into a picture-box and produce a 2nd version with each color of pixel being replaced (e.g red with green etc).
 
>The purpose of the program was to analyse a BMP or JPG loaded into a picture-box and produce a 2nd version with each color of pixel being replaced

Well, I think we can do the replacement without making VB do the pixel by pixel analysis. Although that may depend on how complex this recolouring is. Is there a specific set of recolouring you need/want to do?
 
To illustrate.

You'll need a form with two pictureboxes and a command button. First Picturebox should contain an image.

Rich (BB code):
' Requires a reference to Dana Seaman's GDIPlus Wrapper: https://github.com/Planet-Source-Code/dana-seaman-gdi-typelib-tlb-unicode-logo-generator__1-42861/releases/tag/original-zip
Private Sub GDI_Click()
    Dim GpInput As GdiplusStartupInput
    Dim token As Long
    Dim graphics As Long
    Dim img As Long
    Dim imgatt As Long
    Dim lp As Long
    Dim CMatrix As ColorMatrix
     
    'quickly create the identity matrix
    For lp = 0 To 4
        CMatrix.m(lp, lp) = 1
    Next
    
    ' Two examples below, only 1 enabled
    
    ' 1. Halve the red component of every single pixel, effectively tints image
    ' CMatrix.m(0, 0) = 0.5
    
    ' 2. Applies G = G - 30: B = B - 30: R = R + 20 to every single pixel. This the colour manipulation visible in OP's post
    ' Note that colormatrix values are floats from 0 to 1 rather than
    ' 0 to 255 integer values, so we have converted
    CMatrix.m(0, 4) = 0.078125
    CMatrix.m(1, 4) = -0.1171875
    CMatrix.m(2, 4) = -0.1171875
    
    ' get ready to persist our image
    Picture2.AutoRedraw = True
    Picture2.Cls
   
    GpInput.GdiplusVersion = 1
    If GdiplusStartup(token, GpInput) <> Ok Then
        MsgBox "Error loading GDI+!", vbCritical
    Else
        GdipCreateFromHDC Picture2.hDC, graphics '  use Picture2 as our drawing surface
        GdipCreateBitmapFromHBITMAP Picture1.Picture.Handle, Picture1.Picture.hPal, img
        GdipCreateImageAttributes imgatt
        GdipSetImageAttributesColorMatrix imgatt, ColorAdjustTypeBitmap, 1, CMatrix, CMatrix, ColorMatrixFlagsDefault
        GdipDrawImageRectRect graphics, img, 0, 0, 600, 600, 0, 0, 600, 600, UnitPixel, imgatt ' just an example, size is hardcoded

        'Clean up
        GdipDeleteGraphics graphics
        GdiplusShutdown token
   End If
   
    ' ok, persist the image
    Picture2.AutoRedraw = False
    Picture2.Cls
   
End Sub
 
To illustrate.

You'll need a form with two pictureboxes and a command button. First Picturebox should contain an image.

Rich (BB code):
' Requires a reference to Dana Seaman's GDIPlus Wrapper: https://github.com/Planet-Source-Code/dana-seaman-gdi-typelib-tlb-unicode-logo-generator__1-42861/releases/tag/original-zip
Private Sub GDI_Click()
    Dim GpInput As GdiplusStartupInput
    Dim token As Long
    Dim graphics As Long
    Dim img As Long
    Dim imgatt As Long
    Dim lp As Long
    Dim CMatrix As ColorMatrix
     
    'quickly create the identity matrix
    For lp = 0 To 4
        CMatrix.m(lp, lp) = 1
    Next
    
    ' Two examples below, only 1 enabled
    
    ' 1. Halve the red component of every single pixel, effectively tints image
    ' CMatrix.m(0, 0) = 0.5
    
    ' 2. Applies G = G - 30: B = B - 30: R = R + 20 to every single pixel. This the colour manipulation visible in OP's post
    ' Note that colormatrix values are floats from 0 to 1 rather than
    ' 0 to 255 integer values, so we have converted
    CMatrix.m(0, 4) = 0.078125
    CMatrix.m(1, 4) = -0.1171875
    CMatrix.m(2, 4) = -0.1171875
    
    ' get ready to persist our image
    Picture2.AutoRedraw = True
    Picture2.Cls
   
    GpInput.GdiplusVersion = 1
    If GdiplusStartup(token, GpInput) <> Ok Then
        MsgBox "Error loading GDI+!", vbCritical
    Else
        GdipCreateFromHDC Picture2.hDC, graphics '  use Picture2 as our drawing surface
        GdipCreateBitmapFromHBITMAP Picture1.Picture.Handle, Picture1.Picture.hPal, img
        GdipCreateImageAttributes imgatt
        GdipSetImageAttributesColorMatrix imgatt, ColorAdjustTypeBitmap, 1, CMatrix, CMatrix, ColorMatrixFlagsDefault
        GdipDrawImageRectRect graphics, img, 0, 0, 600, 600, 0, 0, 600, 600, UnitPixel, imgatt ' just an example, size is hardcoded

        'Clean up
        GdipDeleteGraphics graphics
        GdiplusShutdown token
   End If
   
    ' ok, persist the image
    Picture2.AutoRedraw = False
    Picture2.Cls
   
End Sub
Thank you. I will try this out later and let you know the outcome (it may be a few days. The original code was written by the legendary Tanner Helland from Australia and I made a few adaptions.
 
>Tanner Helland

Not legendary to me :) Never heard of him in my over 30 years of playing with VB!. But, now that you have provided a name I was able to find his code, and what you have looks like a very hacked about version of his Pure VB6 Brightness code. And that brightness code can indeed be done using the method I've illustrated, but dramatically faster
 
Thank you for all your efforts.

I did try some of Tanner's other examples when I worked on this several years ago but the only one I got to work was the one
which went through one pixel at a time.

Unfortunately I had the same problem with the code you provided. I think the problem is that some libraries are missing
from my setup of VB, particularly the GDI plus library. It may even be a problem relating to Windows 7. This was even after
unzipping the Dana Seaman files. I will continue to tinker about as this is for hobby rather than business use.
 
>some libraries are missing from my setup of VB, particularly the GDI plus library

There is no MS-provided GDI+ Activex library. The GDI+ library is not a part of VB, it is a Win32 system library that has shipped with the OS since XP.

>the Dana Seaman files

The Dana Seaman file is a tool library, not an activex dll, that provides a wrapper for the flat GDI+ API. You should just need to extract the GDIplus.tlb file to wherever you like, then in VB:

Project > References > Browse (to gdiplus.tlb)

and open it.

Should work on all platforms that VB6 is supported on - so XP thru to Windows 11 ...


So when you say it doesn't work, what actually happens? Error messages? If so, what?
 
Last edited:
I have actually made some progress. I dug out an old HP laptop which has XP and transferred the Dana Seaman files.
I did have to select GDI+Type library in the VB6 References and make a few other minor changes in order to get
the program to work. It does look very professional but there was a downside with the screen of the laptop being
too small.
The option to set the GDI+Type reference was not available on the W7 computer and showed an error message.
Possibly some TLB or DLL file is in the wrong location as I have had this problem before with other graphics programs ?

I will continue to tinker with it.

Thank you very much for going the "extra mile".
 
>The option to set the GDI+Type reference was not available on the W7 computer

No, it won't be. You need to tell VB that the type library is available (you only have to do this once; after that it will appear in the Available References list in the References dialog as expected). You do this as I outlined above, but I'll be more explicit (and forgive me if, as a result, I appear to be teaching grandma to suck eggs)

In the IDE click Project to drop down the Project menu
On that menu, click References to bring up the References dialog
In that dialog, rather than browse Available References list, click the Browse button to bring up the Add Reference dialog, and browse to the location that you placed the gdiplus.tlb.
Click the gdiplus.tlb file to select it, and then click Open button
 
I have now succeeded in getting the program to work in a W7 environment.
Also another graphics program which I had previously only been able to use in XP.

I also tried running it through a VB 2008 converter but it yielded 102 errors which
I wasn't going to try to resolve.

Thank you again for all your help.
 
>Tanner Helland

Not legendary to me :) Never heard of him in my over 30 years of playing with VB!. But, now that you have provided a name I was able to find his code, and what you have looks like a very hacked about version of his Pure VB6 Brightness code. And that brightness code can indeed be done using the method I've illustrated, but dramatically faster
I tried his "Even faster DIBs" code, never thought speed with VB。
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top