-
Oct 7th, 2009, 05:41 PM
#1
Thread Starter
Member
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!
-
Oct 7th, 2009, 05:47 PM
#2
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.
-
Oct 7th, 2009, 05:57 PM
#3
Thread Starter
Member
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
-
Oct 7th, 2009, 06:02 PM
#4
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.
-
Oct 7th, 2009, 06:08 PM
#5
Thread Starter
Member
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
-
Oct 7th, 2009, 06:18 PM
#6
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.
-
Oct 7th, 2009, 06:32 PM
#7
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
-
Feb 13th, 2018, 01:39 PM
#8
Addicted Member
Re: How to get size of image in VB6
Originally Posted by LaVolpe
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.
-
Feb 13th, 2018, 02:08 PM
#9
Re: How to get size of image in VB6
Originally Posted by VbNetMatrix
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.
-
Feb 13th, 2018, 02:16 PM
#10
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|