Click to See Complete Forum and Search --> : How to create Image control at run time?
Pipe
December 1st, 2000, 01:48 PM
hello,
how do i use createobject to create an image control in vb? i cant find the type name of the object
thanks,
p
Bruno
December 1st, 2000, 02:36 PM
With controls you should use Controls.Add, not createobject. Most VB controls cannot be created like other objects, because they require some container (e.g. form).
private Sub Form_Load()
Dim i as Integer, ctl as Image
for i = 0 to 10
set ctl = Controls.Add("VB.Image", "ctl" & i)
ctl.Move i * 500 + 60, 60 * i
set ctl.Picture = me.Icon
ctl.Visible = true
next
End Sub
Pipe
December 1st, 2000, 02:44 PM
will this work inside a COM object? my goal is to get the width and height of .gifs.
Bruno
December 1st, 2000, 03:00 PM
>will this work inside a COM object?
Yes, if your COM object has some control container - form, user control.
You don't need a control to measure picture.
Dim pic As StdPicture
Set pic = LoadPicture("c:\picture.gif")
MsgBox pic.Height & " " & pic.Width
I think dimensions are not in pixels ... I'll check this later.
Bruno
December 1st, 2000, 03:17 PM
Dimensions in pixels, without image control:
Dim pic as StdPicture
set pic = LoadPicture("c:\aper6.bmp")
MsgBox "width=" & ScaleX(pic.Width, vbHimetric, vbPixels) & " pixels" & _
vbCrLf & "height=" & ScaleX(pic.Height, vbHimetric, vbPixels) & " pixels"
Pipe
December 1st, 2000, 03:25 PM
kick ***! thanks a lot!!!!
Chris Eastwood
December 1st, 2000, 05:44 PM
... or, if you don't want to use an image control to capture this information ....
option Explicit
'
' Copy this into a separate BAS Module - or change the 'publics' to
' their relevant types
'
public Type BITMAPINFO ' used simply to identify the size of the picture
Width as Long
Height as Long
End Type
'
public Function GetGIFInfo(byval FileName as string) as BITMAPINFO
Dim bChar as Byte
Dim i as Integer
Dim DotPos as Integer
Dim Header as string
Dim blExit as Boolean
Dim a as string, b as string
Dim ImgWidth as Integer
Dim ImgHeight as Integer
Dim ImgSize as string
Dim fnum as Integer
Dim ImageInfo as BITMAPINFO
'
on error resume next
fnum = FreeFile
Open FileName for binary as #fnum
'
ImgSize = LOF(fnum) / 1024
'
DotPos = InStr(ImgSize, ",")
ImgSize = Left(ImgSize, DotPos - 1)
'
for i = 0 to 5
get #fnum, , bChar
Header = Header + Chr(bChar)
next i
'
If Left(Header, 3) <> "GIF" then
MsgBox FileName & ": not a GIF file"
Close #fnum
Exit Function
End If
'
get #fnum, , bChar
a = a + Chr(bChar)
get #fnum, , bChar
a = a + Chr(bChar)
'
ImgWidth = CInt(Asc(Left(a, 1)) + 256 * Asc(Right(a, 1)))
'
get #fnum, , bChar
b = b + Chr(bChar)
get #fnum, , bChar
b = b + Chr(bChar)
'
ImgHeight = CInt(Asc(Left(b, 1)) + 256 * Asc(Right(b, 1)))
'
Close #fnum
'
With ImageInfo
.Width = ImgWidth
.Height = ImgHeight
End With
'
GetGIFInfo = ImageInfo
End Function
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
Bruno
December 1st, 2000, 06:12 PM
Reading first 10 bytes from file is certainly much faster than using StdPicture.
I didn't expect that simple header. Thanks for great info!
Chris Eastwood
December 1st, 2000, 06:14 PM
I completely forgot about that piece of code (it's not mine) - I found it on the VB newsgroups about 2 years ago !
Very useful information though - I've got some similar code that does the same for BMP files - probably worth posting to the site at some point.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.