CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 1999
    Posts
    68

    How to create Image control at run time?

    hello,
    how do i use createobject to create an image control in vb? i cant find the type name of the object

    thanks,
    p


  2. #2
    Join Date
    Sep 1999
    Posts
    202

    Re: How to create Image control at run time?

    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





  3. #3
    Join Date
    Apr 1999
    Posts
    68

    Re: How to create Image control at run time?

    will this work inside a COM object? my goal is to get the width and height of .gifs.


  4. #4
    Join Date
    Sep 1999
    Posts
    202

    Re: How to create Image control at run time?

    >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.


  5. #5
    Join Date
    Sep 1999
    Posts
    202

    Re: How to create Image control at run time?

    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"





  6. #6
    Join Date
    Apr 1999
    Posts
    68

    Re: How to create Image control at run time?

    kick ***! thanks a lot!!!!


  7. #7
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: How to create Image control at run time?

    ... 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

  8. #8
    Join Date
    Sep 1999
    Posts
    202

    Re: How to create Image control at run time?

    Reading first 10 bytes from file is certainly much faster than using StdPicture.
    I didn't expect that simple header. Thanks for great info!


  9. #9
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: How to create Image control at run time?

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured