Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image Size

Status
Not open for further replies.

Nebooaw

Programmer
Jun 1, 2001
142
GB
Hi, i am trying to find some code that will return me the size of a given image on ie.. \images\car.gif

Can anyone help?


Kindest Regards.
 
Use something like this

<image name=img src=&quot; \images\car.gif&quot; onclick=&quot;alert(this.width+' - '+this.height)&quot;>

I dont knw any non visual solution. Unless you are using some component.

________
George, M
 
Hello,

It is obviously possible to construct a poor-man version in standalone vbs for checking image size according to Shaddow's line. Here is a possible throw-away realization.

'---------
'Fix your absolute path & image name---it is necessary
Const strWrkDir=&quot;C:\winnt\system32\&quot;
Const strImgName = &quot;snapin.gif&quot;

Const strTmpFileName = &quot;tmp.htm&quot;

intBuild=0
BuildHTML strWrkDir&strImgName,strWrkDir&strTmpFileName,intBuild
Select Case intBuild
Case 0 WScript.Echo &quot;Unidentified failure. Operation aborted.&quot;
Case 1 WScript.Echo &quot;Image file cannot be found.&quot;
Case 2 WScript.Echo &quot;Check disk space & file system. Operation aborted.&quot;
End Select
If intBuild <3 Then
WScript.Quit
End If
Set oIE=WScript.CreateObject(&quot;InternetExplorer.Application&quot;,&quot;IE_&quot;)

oIE.Visible=false
'oIE.Visible=true 'An acceptable alternative

oIE.navigate strWrkDir&strTmpFileName

Do while oIE.busy
WScript.Sleep 500
Loop

ToDeleteFile strWrkDir&strTmpFileName

If oIE.visible Then
WScript.Sleep 5000 '5 sec visual inspection
End If

oIE.Quit
WScript.Quit

Sub IE_onQuit
'WScript.Echo &quot;User manually quitted the program.&quot;
Set oIE=Nothing
WScript.Quit
End Sub

Sub BuildHTML(ImgFile, HTMLFile, intReturn)

On Error Resume Next
intReturn=0
Set fso=WScript.CreateObject(&quot;Scripting.FileSystemObject&quot;)
If Not fso.FileExists(ImgFile) Then
Set fso=Nothing
Exit Sub
End If
intReturn=1
strTag=&quot;<image name=&quot;&quot;&quot;&ImgFile&&quot;&quot;&quot; src=&quot;&quot;&quot; & ImgFile & &quot;&quot;&quot;&quot; & _
&quot; onLoad=&quot;&quot;alert(this.name+&quot;&chr(39)&&quot;:&quot;&chr(39)&&quot;+ this.width+&quot;& _
chr(39)&&quot;x&quot;&chr(39)&&quot;+this.height)&quot;&quot;>&quot;
Set oHTML=fso.CreateTextFile(HTMLFile,True)
oHTML.WriteLine(&quot;<HTML><HEAD></HEAD><BODY>&quot;)
oHTML.WriteLine(strTag)
oHTML.WriteLine(&quot;</BODY></HTML>&quot;)
oHTML.close
If err.number<>0 Then
intReturn=2 'Failed to create HTMLFile
err.clear
Else
intReturn=3 'completely successful
End If
On Error GoTo 0
Set oHTML=Nothing
Set fso=Nothing

End Sub

Sub ToDeleteFile (strFileName)
On Error Resume Next
Set fso=WScript.CreateObject(&quot;Scripting.FileSystemObject&quot;)
'wscript.echo strFileName
If fso.FileExists(strFileName) Then
Do
err.clear
fso.DeleteFile(strFileName)
Loop While err.number<>0
End If
On Error GoTo 0
Set fso=Nothing
End Sub
'---------

A few notes for improving:
[1] I use instead onLoad to avoid to many interactives. But, then if the system is excessively slow, it might pop up a wrong 0x0 pixcel size.
[2] From this sketch, you can develop a drag-&-drop to input the specific image file to use.
[3] The tmp.html file can be created in the system specific tmp folder if it looks better.
[4] The output is visual & need a click. It is inconvenient and can obviously be improved.
[5] Input in batch.

I had not used Option Explicit, I like to and should have. But I was being lazy as I wrote those lines on the fly. I have tested it though.

This is just an exercise for my own pleasure. If you find it useful in one or two tricks used, that is an anonymous bonus to me.

regards - tsuji
 
dim myPicture

set myPicture=loadpicture(&quot;c:\word.bmp&quot;)
msgbox myPicture.width
msgbox myPicture.height
 
Hello again,

[1] A reason for me coming back is to point out some pretty obvious careless mistakes in my throw-away script related to the intReturn.

Sub BuildHTML(ImgFile, HTMLFile, intReturn)
On Error Resume Next
intReturn=0
Set fso=WScript.CreateObject(&quot;Scripting.FileSystemObject&quot;)
If err.number<>0 Then
Set fso=Nothing
Exit Sub
End If
intReturn=1
If Not fso.FileExists(ImgFile) Then
Set fso=Nothing
Exit Sub
End If
intReturn=2
...etc
End Sub

[2] Then in the main, Select Case is corrected accordingly,

...etc
Select Case intBuild
Case 0 WScript.Echo &quot;Runtime Scripting failure. Operation aborted.&quot;
Case 1 WScript.Echo &quot;Image file cannot be found.&quot;
Case 2 WScript.Echo &quot;Check disk space & file system. Operation aborted.&quot;
End Select
...etc

[3] Here, the main reason of my coming back is to resolve the time problem of using onLoad event. Below is the minimal way to implement a check of image.complete test in an HTML page I would use:

<HTML>
<HEAD>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function letLoad() {
if (!document.images[0].complete) {
setTimeout(&quot;letLoad()&quot;,5);
} else {
alert(document.images[0].complete+&quot; &quot;+document.images[0].name+':'+document.images[0].width+'x'+document.images[0].height);
}
}
</SCRIPT>
</HEAD>
<BODY>
<img name=&quot;Shed in Field.jpg&quot; src=&quot;Shed in Field.jpg&quot; onLoad=&quot;letLoad()&quot;>
</BODY>
</HTML>

with Shed in Field.jpg is the image file.
So, the obvious thing is to implement this in the strTag. This is obvious, I won't elaborate.

[3] I want just repeat that complete loading does pose a problem for a standalone script. People may not observe that without revision I made. But, I have a slow system to work with which show acutely the problem running the script.

[4] Drag-and-drop or reading a list of file remain a good exercise to extend the script.

[5] I must say I seem unnecessary insistent not because I think the script is of any value, but, because I just want to at least make it deliver a use.

[6] strongm, yours is a good piece of info. Not familiar with the use of it. But loadpicture does have a place in the vbscript documentation. I have learned something, as usual, thanks.

regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top