View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Shawn O'Donnell Shawn O'Donnell is offline
external usenet poster
 
Posts: 42
Default Open JPG and determine pixel width in VB?

"Joe HM" wrote:
I have the following problem: I want to be able to determine the width
of a JPG image from a VB6 script. The user specifies the JPG filename
in a cell and I want the script to somehow load that picture and
determine the width and height.


Could you could load the picture into an Image control on a hidden form,
then ask for the height and width of the Image control? Set the Image
control's AutoSize property to True and set its BorderStyle to
fmBorderStyleNone.

The Image control's dimensions are given in points, not pixels. You have to
do some work converting points to pixels, though, and I'm not sure how
accurate the results would be. Here's code based on an example in the new
book by Bullen, Bovey & Green, "Professional Excel Development." You have to
use a couple of Windows API calls to get your screen's resolution.

Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal
nIndex As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal
hDC As Long) As Long

Private Const POINTS_PER_INCH As Long = 72
Private Const LOGPIXELSX = 88 ' tell GetDeviceCaps to return horiz
pixels/inch

Public Function PixelsPerPoint() As Double
Dim deviceContextHandle As Long
Dim DotsPerInch As Long

deviceContextHandle = GetDC(0)
DotsPerInch = GetDeviceCaps(deviceContextHandle, LOGPIXELSX)
PixelsPerPoint = DotsPerInch / POINTS_PER_INCH
ReleaseDC 0, deviceContextHandle
End Function

Public Function GetGraphicswidth()
GetGraphicswidth = PixelsPerPoint * UserForm1.Image1.Width
End Function

Public Function GetGraphicsHeight()
GetGraphicsHeight = PixelsPerPoint * UserForm1.Image1.Height
End Function