dcsimg
Results 1 to 10 of 10

Thread: How to get size of image in VB6

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2009
    Posts
    53

    How to get size of image in VB6

    hey. For the first time I am using VB6 instead of VB.NET since there is an open source project that was made in VB6. I want to be able to find the height/width of a picture in a folder but I'm not sure how to do it. I dont want to display the picture just find the height of it in pixels. Thanks!

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,450

    Re: How to get size of image in VB6

    Here are two ways.

    1. Load the picture into a VB .Picture property or stdPicture object. Then use this function on the .Picture/stdPicture's .Width & .Height properties:
    Code:
    Height = ScaleY(.Height, vbHimetric, vbPixels)
    Width = ScaleX(.Width, vbHimetric, vbPixels)
    The above would work for bmp, jpg, most gifs, wmf, emf, many icons/cursors.

    2. Manually parse the image format to get the size. Requires a parsing routine for every image type you need sizes from.

    Tip: For PNG and TIFF images and the supported VB image formats, GDI+ would be useful.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2009
    Posts
    53

    Re: How to get size of image in VB6

    thanks for the reply. I didn't know how to load a .picture or stdpicture so i just used the height property on LoadPicture for the initial height before the scaling

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,450

    Re: How to get size of image in VB6

    Sample
    Code:
    Dim myPic As stdPicture, picWidth As Long, picHeight As Long
    Set myPic = LoadPicture(filename) ' supply valid path/filename
    picWidth = ScaleX(myPic.Width, vbHimetric, vbPixels)
    picHeight = ScaleY(myPic.Height, vbHimetric, vbPixels)
    ' handle errors should loading the picture cause an error
    Notes:

    1. Not very efficient because image must be loaded, if a very large file, may take a second or two to load it.

    2. Parsing routines would be tremendously faster but requires research to find them for each image type you want to support. Though you don't need to parse the entire image because the sizes are generally in the first 80 bytes or so.

    Edited: The reason why I said it would support most gifs and many icons/cursors are because
    - You will get the first frame of an animated GIF, not the entire GIF which may not be the same size as the 1st frame
    - LoadPicture does not load XP-style, alphablended icons or cursors
    - LoadPicture cannot load PNGs or TIFFs (plus TIFFs can contain multiple images)
    Last edited by LaVolpe; Oct 7th, 2009 at 06:05 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2009
    Posts
    53

    Re: How to get size of image in VB6

    thanks. One more question about images in VB6. Im working with sprites and i was wondering how I can display part of an image in a pictureBox, like how to load a specified area of an image, like only a 75*75 square

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,450

    Re: How to get size of image in VB6

    Here are 2 ways to do that

    1. Load the picture into a image control or picturebox. Use VB's PaintPicture function
    Example:
    Code:
    ' with picturebox, load an image into it
        Me.ScaleMode = vbPixels
        Me.PaintPicture Picture1.Picture, 0, 0, 75, 75, 0, 0, 75, 75
    Note. I changed the form's scalemode to pixels which is easier to use IMO when messing with graphics. If not, then the 75's above would need to be converted to from pixels to the target's scalemode.

    2. Load the picture into a stdPicture object, use the stdPicture object's Render function. I can show you an example, it is far more complicated, and only really applies if you use a stdPicture object to load the image. Else if loaded into a image control, picture box or any other VB .Picture property, the PaintPicture method above is so much simpler.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,450

    Re: How to get size of image in VB6

    Here's another option

    1. Use an image control, set its properites:
    AutoSize = False
    Stretch = False
    Width = 75 pixels or 1125 twips
    Height = 75 pixels or 1125 twips

    2. Place that control where you want the 75x75 image to be seen

    3. Load the picture into that control
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    Addicted Member
    Join Date
    May 2011
    Posts
    230

    Re: How to get size of image in VB6

    Quote Originally Posted by LaVolpe View Post
    Tip: For PNG and TIFF images and the supported VB image formats, GDI+ would be useful.
    Hi Lavolpe!

    once again, I seek your wisdom and knowledge! for the same related problem!!

    I got a IPicture that I loaded with GDI function.
    I know I can get the "real" image size of the IPicture when the "img" ptr is open in GDI.
    but once everything is close, ("img" no longuer valid) I'm left only with the IPicture.

    I tried access the .width and .height property of IPicture but that make IDE crash.
    (and when it doesn't crash, the value are not the real width, not in twip, not in pixel...)

    so my question (is about your comment up there)

    *how with GDI can I get the real size of an image from an IPicture ?

    thanks for sharing.

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,450

    Re: How to get size of image in VB6

    Quote Originally Posted by VbNetMatrix View Post
    Hi Lavolpe!

    once again, I seek your wisdom and knowledge! for the same related problem!!

    I got a IPicture that I loaded with GDI function.
    I know I can get the "real" image size of the IPicture when the "img" ptr is open in GDI.
    but once everything is close, ("img" no longuer valid) I'm left only with the IPicture.
    Probably should start your own thread, especially if the following doesn't answer your question.

    If IPicture Is Nothing, then trying to access its properties will generate an error, as expected. If you unloaded the image, then how could you possibly retrieve dimensions from "Nothing"?

    If IPicture is not nothing, then you would retrieve the dimensions the same as from a stdPicture... it's .Height & .Width properties and scale them from himetric to pixels as needed.

    In any case, simply accessing IPicture when it is nothing shouldn't crash the IDE, it should just raise an error.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10
    Addicted Member
    Join Date
    May 2011
    Posts
    230

    Re: How to get size of image in VB6

    you're right about the thread.... I thought it was ok since it was same question...

    sorry.... here the new thread:

    http://www.vbforums.com/showthread.p...99#post5262799
    Last edited by VbNetMatrix; Feb 13th, 2018 at 02:48 PM.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width