GetPixel retrieves the pixel from the bitmap image's (hdc) X and Y position specified in the API and returns it's color
Public Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Private Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Dim c As Long
c = GetPixel(Picture1.hdc, 0, 0)
c will contain the long color value of the upper left corner (X = 0, Y = 0) of Picture1.Picture
This is for VB6 and earlier. If you are using VB.NET you are in the wrong Forum
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Judging from the screenshot, I think rpool wants to retrieve each bitmap file's image dimensions. If so, then the GetDIBits function might help achieve that.
Originally Posted by MSDN
If lpvBits is NULL and the bit count member of BITMAPINFO is initialized to zero, GetDIBits fills in a BITMAPINFOHEADER structure or BITMAPCOREHEADER without the color table. This technique can be used to query bitmap attributes.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
I am sorry, not clearly my question,
yes, I need get that bitmap.picture.dimensions (result: need use "pixel" value)
but where to start ?
please!
EDIT: Width / 26.45 and Height/26.45
but not understand why use 26.45?
Code:
myPic_W = myPic.Width / 26.45 'changed to pixel
myPic_H = myPic.Height / 26.45 'changed to pixel
Code:
Option Explicit
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Sub Command1_Click()
Dim myPic As StdPicture
Dim myPic_W As Long
Dim myPic_H As Long
Form1.Picture1.ScaleMode = 3 'pixel
Form1.Picture1.AutoRedraw = True
Form1.Picture1.AutoSize = True
Set myPic = LoadPicture("R:\st.bmp")
Picture1.Picture = myPic
myPic_W = myPic.Width
myPic_H = myPic.Height
Debug.Print Time, myPic_W, myPic_H
Dim c As Long
c = GetPixel(Picture1.hdc, 0, 0)
Debug.Print c
Set myPic = Nothing
End Sub
EDIT: Width / 26.45 and Height/26.45
but not understand why use 26.45?
As you can see, you don't understand what that magic number means. Therefore, you should avoid magic numbers whenever possible. Use constants instead!
Code:
Option Explicit
Private Sub Command1_Click()
Dim myPic_W As Long
Dim myPic_H As Long
With Form1.Picture1
.ScaleMode = vbPixels
.AutoRedraw = True
.AutoSize = True
Set .Picture = LoadPicture("R:\st.bmp")
myPic_W = ScaleX(.Picture.Width, vbHimetric, vbPixels)
myPic_H = ScaleY(.Picture.Height, vbHimetric, vbPixels)
Debug.Print Time, myPic_W & "x" & myPic_H
End With
End Sub
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Why don't you just use Picture1.Width and Picture1.Height only? Why the other stuff?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Why don't you just use Picture1.Width and Picture1.Height only? Why the other stuff?
Because Picture1.Width and Picture1.Height returns the size of the PictureBox control itself while the dimensions of the PictureBox's Picture property are obtained through the similarly named Width and Height properties of the StdPicture class. The Picture property's dimensions are usually independent from the size of the PictureBox, but in this case, since the AutoSize property was set to True, both dimensions of the PictureBox and its Picture property coincide with each other. However, I decided not to rely on this fact, but instead I used the ScaleX/ScaleY methods of the Form to convert the true dimensions of the Bitmap Picture from HiMetric to Pixels.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Well, they don't always correspond with the dimensions of the Picture property, so they're also unreliable. A PictureBox can be bigger or smaller than the Picture assigned to it, thus its Width/Height or ScaleWidth/ScaleHeight won't always agree with the dimensions of its Picture property.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Interesting. Never ran into that problem in all the years I have used pictureboxes in VB programming. I'll take your work for it however
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
There is a Text Color drop-down button on the editor's toolbar.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
BTW, how did you get the keywords to go blue in your post #7.
It's brilliant.
Spoo
Spoo, How did you get "keywords to go blue" in your post?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Here is another way of doing it, very easy.
You will need 'Microsoft Window Image Acquisition Library v2.0' installed on the system (which I'm sure you do).
Code:
Private Function GetImageDimension(FileName As String) As String
Dim ImageFile As Object
Set ImageFile = CreateObject("WIA.ImageFile")
With ImageFile
.LoadFile (FileName)
GetImageDimension = .Width & " x " & .Height
End With
End Function
Private Sub Command1_Click()
MsgBox GetImageDimension("C:\1.bmp")
End Sub
You could do much more with WIA 2.0, ask dilettante
Just to add, Bonnie West's example works very good, but is a bit slow because it loads picture into picturebox.
Bonnie West is a great teacher and knows a lot more than I do, but testing Bonnie's example with image folder (image list) it was very slow!
Using the WIA 2.0 looking for 10 images' dimension takes about under 1 second .
Using picturebox (Bonnie's example) took about 4 seconds!
Depending what you need it for, I would go with WIA 2.0.
If you only need the one dimension and that's it, then you could go with Bonnie's example.
There is another approach: ask Windows Explorer (a.k.a. Shell32).
This can be quick or a littler slower depending on whether it already has the info cached from a previous viewing. But at worst it might still be much quicker than completely loading each image file and decoding it far enough to extract the dimensions the way previous suggestions do.
Another approach to consider anyway. Here I request an image folder to list, then pull out the files (not traversing subfolders though you could do that too). Then I extract a few properties and extended properties and dump them into a flexgrid.
Last edited by dilettante; Jun 15th, 2013 at 07:57 PM.
Reason: attachment updated
Note that results might vary from version to version of Windows. Sometimes the extended properties have different "friendly names" and then you'd want to fetch them by FMTID and PID (combined as a SCID), which you'd need to look up.
ExtendedProperty Method
sPropName argument:
Required. String value that specifies the property. There are two ways to specify a property. The first is to assign the property's name, such as "Author" or "Date", to sPropName. However, each property is a member of a COM property set and can also be identified by specifying its format ID (FMTID) and property ID (PID). An FMTID is a GUID that identifies the property set, and a PID is an integer that identifies a particular property within the property set. Specifying a property by its FMTID/PID values is usually more efficient than using its name. To use a property's FMTID/PID values with ExtendedProperty, they must be combined into an SCID. An SCID is a string that contains the FMTID/PID values in the form "FMTID,PID", where the FMTID is the string form of the property set's GUID. For example, the SCID of the summary information property set's author property is "{F29F85E0-4FF9-1068-AB91-08002B27B3D9},4". For a list of FMTIDs and PIDs that are currently supported by the Shell, see SHCOLUMNID.
Ok, went back and got the info to make SCIDs for the extended properties I've used here. Updating attachment above.
Your attachment worked for me dilettate (just to let you know).
It would be nice if you did fix that issue for other versions of windows, I am using Winows 7 Home Premium (x64).
Just to add, Bonnie West's example works very good, but is a bit slow because it loads picture into picturebox.
... but testing Bonnie's example with image folder (image list) it was very slow!
Actually, I just modified the OP's code a bit, but you are right, of course! Accessing the Width and Height property of the Picture property of the PictureBox control will definitely be slower than the other methods presented here.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0