I'm trying to write some code which will allow me to do some simple image analysis, manipulation, and display in Excel. I think I have an issue with version compatibility though. Below is an example of some simple code which demonstrates the problem. It is currently set up (just for debug purposes) to allow the user to specify a file after right-clicking a userform. It loads the file to the form's picture property, then uses Getobject() to populate the bitmap header information associated with the picture. I would normally then go on to get the picture's bits, analyse it, make some changes etc. However, that is not included in the following, because the problem seems to be in the Getobject() implementation. Here is the code:
If I put a breakpoint after the Getobject call, and use the Watch window to look at the PicInfo variable (a bitmap), it should be populated with the values from the loaded picture, i.e. height, width, bits per pixel etc. When I do this at work, that is exactly what happens - i.e. everything is behaving as I'd expect. When I run exactly the same code, in exactly the same workbook at home, the PicInfo variable has zero for all of the bitmap parameters, and the result of the Getobject call (the rslt variable) is also zero.
At work I'm using Office 2010 32 bit, running on Windows 7. At home I'm using Office 2013 64 bit, running on Windows 8. As far as I know, 2010 and 2013 both run VBA7, so I assume the difference is down to the 64 bit issue, but I'm not sure how the declaration needs to change. I've had a look at what the Microsoft website says about the topic, but it all seemed pretty general, and I was not clear what, if any, changes there needed to be the declaration. I'd really appreciate any suggestions. Thanks.
Code:
Private Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Dim ImDatLoaded As Boolean
#If VBA7 Then ' VBA7
Private Declare PtrSafe Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As LongPtr, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare PtrSafe Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As LongPtr, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare PtrSafe Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As LongPtr, ByVal nCount As Long, lpObject As Any) As Long
#Else
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
#End If
Dim PicBits() As Byte, PicInfo As BITMAP
Dim Cnt As Long, BytesPerLine As Long
Private Sub UserForm_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim rslt As Long
If Button = 2 Then
On Error Resume Next
Dim fname As String
fname = Application.GetOpenFilename(, , "Select an image file", , False)
If VarType(fname) = vbString Then
If fname <> "" Then Ufrm_Canvas.Picture = LoadPicture(fname)
End If
If Err.Number = 0 Then
'GetImData
rslt = GetObject(Ufrm_Canvas.Picture, Len(PicInfo), PicInfo)
'in here would be the getbitmapbits and the image analysis etc.
'
Else
Err.Clear
End If
End If
End Sub
At work I'm using Office 2010 32 bit, running on Windows 7. At home I'm using Office 2013 64 bit, running on Windows 8. As far as I know, 2010 and 2013 both run VBA7, so I assume the difference is down to the 64 bit issue, but I'm not sure how the declaration needs to change. I've had a look at what the Microsoft website says about the topic, but it all seemed pretty general, and I was not clear what, if any, changes there needed to be the declaration. I'd really appreciate any suggestions. Thanks.