Please help me to correct Excel VBA code!

Minh Tuan VN

New Member
Joined
Jul 31, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi friends,

I am a newbe. I would like your help in editing the Excel VBA code to do my job as follows:

- Step 1: I insert the image into the Excel file (in my sample the image is too small with dimensions of 21.75 width and 7.5 height by 7.5, unit is pixels).

- Step 2: Assign images to macros.

- Step 3: After clicking on the image, the macro runs and selects each pixel of the image and prints the color value of that pixel as R, G, B in columns A, B, C and prints the size of the image in cell E1 and F1.

The result of my VBA code is wrong: The image has many colors but the R, G, B values are the same R=255, G=0, B=0.

Please help me fix it. Thanks so much in advance!

Below is my code:

#If VBA7 Then
Private Declare PtrSafe Function GetPixel Lib "gdi32" (ByVal hdc As LongPtr, ByVal x As Long, ByVal y As Long) As Long
Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hdc As LongPtr) As Long
#Else
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
#End If

Private Type POINT
x As Long
y As Long
End Type

Private Type rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Sub ExtractImageRGB()
Dim shp As Shape
Dim imgTop As Single, imgLeft As Single
Dim imgWidth As Single, imgHeight As Single
Dim x As Long, y As Long
Dim lColour As Long
Dim r As Integer, g As Integer, b As Integer
Dim cellRow As Long
Dim hdc As LongPtr
Dim rect As rect

' Get the shape (picture) from the worksheet
Set shp = ActiveSheet.Shapes("Picture 1") ' Change to your picture's name

' Get image bounds
imgTop = shp.Top
imgLeft = shp.Left
imgWidth = shp.Width
imgHeight = shp.Height

' Get the device context of the Excel window
hdc = GetDC(ActiveWindow.hwnd)

' Find the next empty row in column A
cellRow = 1

' Loop through each pixel within the image bounds
For y = 0 To imgHeight - 1
For x = 0 To imgWidth - 1
' Calculate pixel coordinates relative to the top-left corner of the image
lColour = GetPixel(hdc, imgLeft + x, imgTop + y)

' Extract RGB components from the color
r = lColour And &HFF
g = (lColour \ &H100) And &HFF
b = (lColour \ &H10000) And &HFF

' Output RGB values to cells
Cells(cellRow, 1).Value = r
Cells(cellRow, 2).Value = g
Cells(cellRow, 3).Value = b

' Fill cell ben canh bang picked color
Cells(cellRow, 4).Interior.Color = lColour


Range("E1").Value = imgWidth
Range("F1").Value = imgHeight

cellRow = cellRow + 1
Next x
Next y

' Release the device context
ReleaseDC ActiveWindow.hwnd, hdc

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Sample of scanning every pixels of a picture_30-7-2024_redownload_31-7-2024.xlsm
ABCDEFG
125500227.5
225500
325500
425500
525500
625500
725500
825500
925500
1025500
1125500
1225500
1325500
1425500
1525500
1625500
1725500
1825500
1925500
2025500
2125500
2225500
2325500
2425500
2525500
2625500
2725500
2825500
2925500
3025500
3125500
3225500
3325500
3425500
3525500
3625500
3725500
3825500
3925500
4025500
4125500
4225500
4325500
4425500
4525500
4625500
4725500
4825500
4925500
5025500
5125500
5225500
5325500
5425500
5525500
5625500
5725500
5825500
5925500
6025500
6125500
6225500
6325500
6425500
6525500
6625500
6725500
6825500
6925500
7025500
7125500
7225500
7325500
7425500
7525500
7625500
7725500
7825500
7925500
8025500
8125500
8225500
8325500
8425500
8525500
8625500
8725500
8825500
8925500
9025500
9125500
9225500
9325500
9425500
9525500
9625500
9725500
9825500
9925500
10025500
10125500
10225500
10325500
10425500
10525500
10625500
10725500
10825500
10925500
11025500
11125500
11225500
11325500
11425500
11525500
11625500
11725500
11825500
11925500
12025500
12125500
12225500
12325500
12425500
12525500
12625500
12725500
12825500
12925500
13025500
13125500
13225500
13325500
13425500
13525500
13625500
13725500
13825500
13925500
14025500
14125500
14225500
14325500
14425500
14525500
14625500
14725500
14825500
14925500
15025500
15125500
15225500
15325500
15425500
Sheet1
 

Attachments

  • screenshot_results.png
    screenshot_results.png
    56.3 KB · Views: 13
Upvote 0

Forum statistics

Threads
1,224,710
Messages
6,180,487
Members
452,987
Latest member
johnsonlily7890

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top