Click here to Skip to main content
15,746,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi people.
I used System.Drawing to import an image to my project.
I need to import the image and find all black pixels of 10th row of it.
But it gets me error on loading the picture.
I mean on line 4.

Any help please?

P.S: I have ticked both System.drawing.dll and Diagnostic Content Profile Library in references.

What I have tried:

Bellow is my code:

Private sampleImage As Bitmap
Public Sub ImageLoading()
    Dim sampleFileName As String
    sampleFileName = ThisWorkbook.Path & "\Sample.bmp"
    sampleImage = New Bitmap(Image.FromFile(sampleFileName))

    Dim c1
    For x1 = 1 To 1080
        c1 = sampleImage.GetPixel(x1, 10)
        If c1 = 0 Then debug.print ("Pixel " & x1 & " is Black.")
    Next x1
end sub
Updated 3-Apr-23 5:22am
Richard MacCutchan 3-Apr-23 10:51am    
What error?
Sh.H. 3-Apr-23 11:01am    
The error is on line 4.
Syntax Error.

Richard MacCutchan 3-Apr-23 11:14am    
Most likely (since you have not provided the actual error message details) caused by the single backslash in "\Sample.bmp". Either remove it, or replace it with two thus: "\\Sample.bmp"

Without access to your system, we probably can't help that much, but at a guess it's the location of the macro that is causing the problem.

If the macro is located in (say) personal.xls rather than in the workbook itself, ThisWorkbook.Path will return the folder that personal.xls is contained in, not the active workbook folder that contains the image.

I'd start by using the debugger to examine the actual folder path in sampleFileName immediately before the attempt to load the image, and compare that with the "real world" folder path to the image.
Share this answer
You can't use the Bitmap from System.Drawing in VBA. If you right-click on the work "Bitmap" in
sampleImage = New Bitmap(Image.FromFile(sampleFileName))
and choose "Definition" you'll get "Indentifier under cursor is not recognized". Alternatively, click on the Object Browser (or hit F2) and search for Bitmap in "All Libraries" - it will find it, but there is nothing there to use or call - no properties, no methods.

You could try the techniques listed here Extracting Colors from Image loaded in userform | PC Review[^] - or, as you have tagged your question "VB" use VB.NET and the System.Drawing DLL
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900