#1
  1. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2922

    Post Get remote image width and height using pure ASP


    the code below is all you need to read remote image (using URL)
    and get its height, width and size.

    credit is given in the code itself to the proper people, I only
    combined it all to neat functions.

    for any questions or problems, reply here.

    Code:
    <% Option Explicit %>
    <html>
    <head>
    <title>Remote Image Analyze Test</title>
    </head>
    <body>
    <%
    If Request.QueryString("URL")<>"" Then Call AnalyzeImageURL
    
    Sub AnalyzeImageURL
    	Dim strURL, objXmlHTTP, strBinaryData
    	Dim strAsciiData, iImageWidth, iImageHeight
    	Dim strColors, strType
    	strURL = Request.QueryString("URL")
    	If (Not(BeginsWith(strURL, "http://"))) And (Not(BeginsWith(strURL, "https://"))) Then
    		Response.Write("invalid URL! please provide full URL<br />") : Exit Sub
    	End If
    	Set objXmlHTTP = Server.CreateObject("Microsoft.XMLHTTP")
    	On Error Resume Next
    		objXmlHTTP.Open "GET", strURL, False
    		objXmlHTTP.Send
    		If Err.Number<>0 Then
    			Response.Write("URL does not exist or can't load the page<br />")
     			Set objXmlHTTP=Nothing
     			Exit Sub
     		 End If
    	On Error Goto 0
    	strBinaryData = objXmlHTTP.ResponseBody
    	Set objXmlHTTP=Nothing
    	strAsciiData = RSBinaryToString(strBinaryData)
    	If gfxSpex(strAsciiData, iImageWidth, iImageHeight, strColors, strType) = True then
    		Response.Write("image file size: " & LenB(strBinaryData) & " bytes<br />")
    		Response.Write("image width is: " & iImageWidth & "<br />")
    		Response.Write("image height is: " & iImageHeight & "<br />")
    	Else  
    		Response.Write("URL is not valid image.<br />")
    	End If
    End Sub
    
    Function BeginsWith(strMain, strSub)
    	BeginsWith = LCase(Left(strMain, Len(strSub)))=LCase(strSub)
    End Function
    
    Function RSBinaryToString(xBinary)
    	'Antonin Foller, http://www.motobit.com
    	'RSBinaryToString converts binary data (VT_UI1 | VT_ARRAY Or MultiByte string)
    	'to a string (BSTR) using ADO recordset
    	
    	Dim Binary
    	Dim RS, LBinary
    	Const adLongVarChar = 201
    	
    	'MultiByte data must be converted To VT_UI1 | VT_ARRAY first.
    	If vartype(xBinary)=8 Then Binary = MultiByteToBinary(xBinary) Else Binary = xBinary
    		Set RS = CreateObject("ADODB.Recordset")
    		LBinary = LenB(Binary)
    		
    		If LBinary>0 Then
    			RS.Fields.Append "mBinary", adLongVarChar, LBinary
    			RS.Open
    			RS.AddNew
    			RS("mBinary").AppendChunk Binary 
    			RS.Update
    			RSBinaryToString = RS("mBinary")
    		Else  
    			RSBinaryToString = ""
    	End If
    End Function
    
    Function MultiByteToBinary(MultiByte)
    	' 2000 Antonin Foller, http://www.motobit.com
    	' MultiByteToBinary converts multibyte string To real binary data (VT_UI1 | VT_ARRAY)
    	' Using recordset
    	Dim RS, LMultiByte, Binary
    	Const adLongVarBinary = 205
    	Set RS = CreateObject("ADODB.Recordset")
    	LMultiByte = LenB(MultiByte)
    	If LMultiByte>0 Then
    		RS.Fields.Append "mBinary", adLongVarBinary, LMultiByte
    		RS.Open
    		RS.AddNew
    		RS("mBinary").AppendChunk MultiByte & ChrB(0)
    		RS.Update
    		Binary = RS("mBinary").GetChunk(LMultiByte)
    	End If
    	MultiByteToBinary = Binary
    End Function
    
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ':::                                                             :::
    ':::  This routine will attempt to identify any filespec passed  :::
    ':::  as a graphic file (regardless of the extension). This will :::
    ':::  work with BMP, GIF, JPG and PNG files.                     :::
    ':::                                                             :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ':::          Based on ideas presented by David Crowell          :::
    ':::                   (credit where due)                        :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    '::: blah blah blah blah blah blah blah blah blah blah blah blah :::
    '::: blah blah blah blah blah blah blah blah blah blah blah blah :::
    '::: blah blah     Copyright *c* MM,  Mike Shaffer     blah blah :::
    '::: bh blah      ALL RIGHTS RESERVED WORLDWIDE      blah blah :::
    '::: blah blah  Permission is granted to use this code blah blah :::
    '::: blah blah   in your projects, as long as this     blah blah :::
    '::: blah blah      copyright notice is included       blah blah :::
    '::: blah blah blah blah blah blah blah blah blah blah blah blah :::
    '::: blah blah blah blah blah blah blah blah blah blah blah blah :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ':::                                                             :::
    ':::  This function gets a specified number of bytes from any    :::
    ':::  file, starting at the offset (base 1)                      :::
    ':::                                                             :::
    ':::  Passed:                                                    :::
    ':::       flnm        => Filespec of file to read               :::
    ':::       offset      => Offset at which to start reading       :::
    ':::       bytes       => How many bytes to read                 :::
    ':::                                                             :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    Private Function GetBytes(flnm, offset, bytes)
    	Dim startPos
    	If offset=0 Then
    		startPos = 1
    	Else  
    		startPos = offset
    	End If
    	if bytes = -1 then		' Get All!
    		GetBytes = flnm
    	else
    		GetBytes = Mid(flnm, startPos, bytes)
    	end if
    End Function
    
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ':::                                                             :::
    ':::  Functions to convert two bytes to a numeric value (long)   :::
    ':::  (both little-endian and big-endian)                        :::
    ':::                                                             :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    Private Function lngConvert(strTemp)
    	lngConvert = clng(asc(left(strTemp, 1)) + ((asc(right(strTemp, 1)) * 256)))
    end function
    
    Private Function lngConvert2(strTemp)
    	lngConvert2 = clng(asc(right(strTemp, 1)) + ((asc(left(strTemp, 1)) * 256)))
    end function
    
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ':::                                                             :::
    ':::  This function does most of the real work. It will attempt  :::
    ':::  to read any file, regardless of the extension, and will    :::
    ':::  identify if it is a graphical image.                       :::
    ':::                                                             :::
    ':::  Passed:                                                    :::
    ':::       flnm        => Filespec of file to read               :::
    ':::       width       => width of image                         :::
    ':::       height      => height of image                        :::
    ':::       depth       => color depth (in number of colors)      :::
    ':::       strImageType=> type of image (e.g. GIF, BMP, etc.)    :::
    ':::                                                             :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    function gfxSpex(flnm, width, height, depth, strImageType)
    	dim strPNG 
    	dim strGIF
    	dim strBMP
    	dim strType
    	dim strBuff
    	dim lngSize
    	dim flgFound
    	dim strTarget
    	dim lngPos
    	dim ExitLoop
    	dim lngMarkerSize
    	
    	strType = ""
    	strImageType = "(unknown)"
    	
    	gfxSpex = False
    	
    	strPNG = chr(137) & chr(80) & chr(78)
    	strGIF = "GIF"
    	strBMP = chr(66) & chr(77)
    	
    	strType = GetBytes(flnm, 0, 3)
    	
    	if strType = strGIF then				' is GIF
    		strImageType = "GIF"
    		Width = lngConvert(GetBytes(flnm, 7, 2))
    		Height = lngConvert(GetBytes(flnm, 9, 2))
    		Depth = 2 ^ ((asc(GetBytes(flnm, 11, 1)) and 7) + 1)
    		gfxSpex = True
    	elseif left(strType, 2) = strBMP then		' is BMP
    		strImageType = "BMP"
    		Width = lngConvert(GetBytes(flnm, 19, 2))
    		Height = lngConvert(GetBytes(flnm, 23, 2))
    		Depth = 2 ^ (asc(GetBytes(flnm, 29, 1)))
    		gfxSpex = True
    	elseif strType = strPNG then			' Is PNG
    		strImageType = "PNG"
    		Width = lngConvert2(GetBytes(flnm, 19, 2))
    		Height = lngConvert2(GetBytes(flnm, 23, 2))
    		Depth = getBytes(flnm, 25, 2)
    		select case asc(right(Depth,1))
    			case 0
    				Depth = 2 ^ (asc(left(Depth, 1)))
    				gfxSpex = True
    			case 2
    				Depth = 2 ^ (asc(left(Depth, 1)) * 3)
    				gfxSpex = True
    			case 3
    				Depth = 2 ^ (asc(left(Depth, 1)))  '8
    				gfxSpex = True
    			case 4
    				Depth = 2 ^ (asc(left(Depth, 1)) * 2)
    				gfxSpex = True
    			case 6
    				Depth = 2 ^ (asc(left(Depth, 1)) * 4)
    				gfxSpex = True
    			case else
    				Depth = -1
    		end select
    	else
    		strBuff = GetBytes(flnm, 0, -1)		' Get all bytes from file
    		lngSize = len(strBuff)
    		flgFound = 0
    		
    		strTarget = chr(255) & chr(216) & chr(255)
    		flgFound = instr(strBuff, strTarget)
    		
    		if flgFound = 0 then
    			exit function
    		end if
    		
    		strImageType = "JPG"
    		lngPos = flgFound + 2
    		ExitLoop = false
    		
    		do while ExitLoop = False and lngPos < lngSize
    			do while asc(mid(strBuff, lngPos, 1)) = 255 and lngPos < lngSize
    				lngPos = lngPos + 1
    			loop
    			
    			if asc(mid(strBuff, lngPos, 1)) < 192 or asc(mid(strBuff, lngPos, 1)) > 195 then
    				lngMarkerSize = lngConvert2(mid(strBuff, lngPos + 1, 2))
    				lngPos = lngPos + lngMarkerSize  + 1
    			else
    				ExitLoop = True
    			end if
    		loop
    		
    		if ExitLoop = False then
    			Width = -1
    			Height = -1
    			Depth = -1
    		else
    			Height = lngConvert2(mid(strBuff, lngPos + 4, 2))
    			Width = lngConvert2(mid(strBuff, lngPos + 6, 2))
    			Depth = 2 ^ (asc(mid(strBuff, lngPos + 8, 1)) * 8)
    			gfxSpex = True
    		end if
    	end if
    End Function
    %>
    <form>
    URL: <input type="text" name="URL" value="<%=Request("URL")%>" /><br /><button type="submit">Submit</button>
    </form>
    </body>
    </html>
    Attached Files
  2. #2
  3. Expert Learner
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Feb 2005
    Location
    Wisconsin
    Posts
    1,909
    Rep Power
    97
    Hey Yahav, is there any way to take this binary data and save it to a file as an image? Might be a new tutorial for ya
  4. #3
  5. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2922
    Originally Posted by baseballdude_
    Hey Yahav, is there any way to take this binary data and save it to a file as an image? Might be a new tutorial for ya
    that's what you're looking for?
    LoadRemoteSite: loading remote website contents with XMLHTTP
  6. #4
  7. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0
    Hello

    I wrote the code bellow. I hope it helps:

    vb.net Code:
     
    Dim url As String = "image url here"
     
    Dim ImageStream As System.IO.Stream = New System.Net.WebClient().OpenRead(url)
    Dim limgAux As System.Drawing.Image = System.Drawing.Image.FromStream(ImageStream)
     
    Label1.Text &= "Height = " & limgAux.Height & "<br />" & _
             "Width = " & limgAux.Width & "<br />"


    tks
    Fabio
  8. #5
  9. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2922
    thanks Fabio, your code is missing Close and Dispose of things and is also
    missing error handling but basically it will work.

Similar Threads

  1. getting image width & height in ASP
    By mriz81 in forum ASP Development
    Replies: 7
    Last Post: September 24th, 2005, 10:24 AM
  2. Determine width and height of tiff image
    By Muff in forum ASP Development
    Replies: 3
    Last Post: September 19th, 2005, 03:29 AM
  3. vb: image resizing from stream to SQL Server
    By timandkids in forum .NET Development
    Replies: 0
    Last Post: November 10th, 2004, 11:54 AM
  4. get image information (Width, Height) from client
    By adr001db in forum .NET Development
    Replies: 0
    Last Post: May 28th, 2004, 04:46 AM
  5. Controlling the width and height of the Image.
    By Steve Schofield in forum ASP Development
    Replies: 1
    Last Post: June 15th, 2001, 03:21 AM

IMN logo majestic logo threadwatch logo seochat tools logo