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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with image resize script 1

Status
Not open for further replies.

xStrongTowerx

Programmer
Apr 7, 2003
11
US
Dear Forum,

I'm trying to adapt a script I found on 4guysfromrolla.com, but I'm having some trouble. I've tried posting on their own messageboard, but to no avail, so I thought I'd post the question here.

I must confess I'm brand new at ASP.NET, although I've been programming classic ASP for 4 years.

I'm trying to do image resizing as shown in
I've modified the script to use the <img src=&quot;ShowImage.aspx?img=RT+Angle.jpg&h=100&w=100&quot;> tag to call ShowImage.aspx, and altered ShowImage.aspx to do a proportional resize as follows:

<%@Import Namespace=&quot;System.Drawing.Imaging&quot; %>
<script language=&quot;VB&quot; runat=&quot;server&quot;>
Function ThumbnailCallback() as Boolean
Return False
End Function

Sub Page_Load(sender as Object, e as EventArgs)

'Read in the image filename to create a thumbnail of
Dim imageUrl as String = Request.QueryString(&quot;img&quot;)

'Read in the width and height
Dim maxHeight as Integer = Request.QueryString(&quot;h&quot;)
Dim maxWidth as Integer = Request.QueryString(&quot;w&quot;)

'Make sure that the image URL doesn't contain any /'s or \'s
If imageUrl.IndexOf(&quot;/&quot;) >= 0 Or imageUrl.IndexOf(&quot;\&quot;) >= 0 then
'We found a / or \
Response.End()
End If

'Add on the appropriate directory
imageUrl = &quot;/images/&quot; & imageUrl

Dim fullSizeImg as System.Drawing.Image
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imageUrl))

imgHeight = fullSizeImg.Height
imgWidth = fullSizeImg.Width
If imgWidth > maxWidth OR imgHeight > maxHeight then
'Determine what dimension is off by more
Dim deltaWidth as Integer = imgWidth - maxWidth
Dim deltaHeight as Integer = imgHeight - maxHeight
Dim scaleFactor as Double

If deltaHeight > deltaWidth then
'Scale by the height
scaleFactor = maxHeight / imgHeight
Else
'Scale by the Width
scaleFactor = maxWidth / imgWidth
End If

imgWidth *= scaleFactor
imgHeight *= scaleFactor
End If

'Do we need to create a thumbnail?
Response.ContentType = &quot;image/gif&quot;
If imageHeight > 0 and imageWidth > 0 then
Dim dummyCallBack as System.Drawing.Image.GetThumbNailImageAbort
dummyCallBack = New _
System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)

Dim thumbNailImg as System.Drawing.Image
thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, _
dummyCallBack, IntPtr.Zero)

thumbNailImg.Save(Response.OutputStream, ImageFormat.Gif)
Else
fullSizeImg.Save(Response.OutputStream, ImageFormat.Gif)
End If

End Sub
</script>

Now my problem is that whenever I try to call the script, the browser asks me to enter a username and password. I've tried my ftp user/pass for the Windows 2000 server, but it didn't work. It says I don't have permissions.

What can this mean? Does it mean that I don't have permission to access the System.Drawing.Imaging namespace, or does it mean that I'm screwing up the paths somehow to point to a directory outside my domain?

Thanks for your assistance,

xSTx
 
xStrong: Nice technique. Are you importing the System.IO class? Is it needed in conjuntion with System.Drawing.Imaging?
 
Hmmm. I tried it, but it didn't make a difference. I see no precedence for importing IO on so I'm kind of confused. It still doesn't work, though.

Do you have any idea why it would be asking me for a user name and password? Does it look like I'm accidentally pointing this script to a directory I don't have access to?
 
x: I briefly read the link and had thought that perhaps the system.IO was needed. Does have the appearence of pointing to a restricted directory. I'll copy the code - getting ready to set up some thumbnails myself - and see if I have any luck with it - its a good technique and one that would be useful for others here at Tek-Tips.
 
No, I don't think I'm pointing to a bad directory, because even when I reduce the script to what's below, and then call it directly, I'm still asked to log in and then told I'm not authorized to access the page.

<%@ Import Namespace=&quot;System.IO&quot; %>
<%@Import Namespace=&quot;System.Drawing.Imaging&quot; %>
<script language=&quot;VB&quot; runat=&quot;server&quot;>
Function ThumbnailCallback() as Boolean
Return False
End Function

Sub Page_Load(sender as Object, e as EventArgs)

'Read in the image filename to create a thumbnail of
Dim imageUrl as String = Request.QueryString(&quot;img&quot;)

'Read in the width and height
Dim maxHeight as Integer = Request.QueryString(&quot;h&quot;)
Dim maxWidth as Integer = Request.QueryString(&quot;w&quot;)

'Make sure that the image URL doesn't contain any /'s or \'s
If imageUrl.IndexOf(&quot;/&quot;) >= 0 Or imageUrl.IndexOf(&quot;\&quot;) >= 0 then
'We found a / or Response.End()
End If

'Add on the appropriate directory
imageUrl = &quot;/dev/images/&quot; & imageUrl

'debug
Response.write(imageURL)

End Sub
</script>
 
sStrong: I went ahead and built both pages; first retrieving the images then secondly resizing the images. Both of the following pages worked without incident with the following exception:

In the 4Guys article they had:

Const IMAGE_DIRECTORY as String = &quot;/images2/&quot;

which caused a &quot;directory error&quot;, however when I changed it to:

Const IMAGE_DIRECTORY as String = &quot;.\images2\&quot;

...it worked perfectly. I'll attached the two pages of code below. Now I am going to revisit your code and see if I can reproduce your error.

************** ASPX PAGE GETFILES ***************
Code:
<%@ Page Language=&quot;VB&quot; Debug=&quot;true&quot; %>
<%@ Import Namespace = &quot;Microsoft.VisualBasic&quot;%>
<%@ Import Namespace=&quot;System.IO&quot; %> 
<script language=&quot;vb&quot; runat=&quot;server&quot;>
  Sub Page_Load(sender as Object, e as EventArgs)
    Const IMAGE_DIRECTORY as String = &quot;.\Images2\&quot;
    Dim pics as ArrayList = new ArrayList()
    Dim s as String, html as String    
    For Each s in Directory.GetFiles(Server.MapPath(IMAGE_DIRECTORY), &quot;*.png&quot;)    
      html = &quot;<a href=&quot;&quot;&quot; & IMAGE_DIRECTORY & Path.GetFileName(s) & &quot;&quot;&quot;>&quot; & _
             &quot;<img src=&quot;&quot;&quot; & IMAGE_DIRECTORY & Path.GetFileName(s) & &quot;&quot;&quot;>&quot; & _
             &quot;</a>&quot;             
      pics.Add(html)
    Next
    dlPictures.DataSource = pics
    dlPictures.DataBind()
  End Sub
</script>
<HTML>
<HEAD>
<title>Test Page</title>		
</HEAD>
<body>
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<p></p>
<asp:DataList runat=&quot;server&quot; id=&quot;dlPictures&quot; RepeatColumns=&quot;3&quot; ItemStyle-HorizontalAlign=&quot;Center&quot;>    
  <ItemTemplate>
    <%# Container.DataItem %>
  </ItemTemplate>
</asp:DataList>
</form>
</body>
</HTML>
************** ASPX PAGE SIZEFILES ***************
Code:
<%@ Page Language=&quot;VB&quot; Debug=&quot;true&quot; %>
<%@Import Namespace = &quot;Microsoft.VisualBasic&quot;%>
<%@ Import Namespace=&quot;System.IO&quot; %>
<script language=&quot;vb&quot; runat=&quot;server&quot;>

  Sub Page_Load(sender as Object, e as EventArgs)
    Const IMAGE_DIRECTORY as String = &quot;.\images2\&quot;
    Const maxWidth as Integer = 100
    Const maxHeight as Integer = 100    
    Dim pics as ArrayList = new ArrayList()
    Dim s as String, html as String
    Dim imgHeight, imgWidth as Integer
    
    For Each s in Directory.GetFiles(Server.MapPath(IMAGE_DIRECTORY), &quot;*.png&quot;)    
      'Get information about the image
      Dim currentImage as System.Drawing.Image = System.Drawing.Image.FromFile(s)      
      imgHeight = currentImage.Height
      imgWidth = currentImage.Width
      If imgWidth > maxWidth OR imgHeight > maxHeight then
        'Determine what dimension is off by more
        Dim deltaWidth as Integer = imgWidth - maxWidth
        Dim deltaHeight as Integer = imgHeight - maxHeight
        Dim scaleFactor as Double        
        If deltaHeight > deltaWidth then
          'Scale by the height
          scaleFactor = maxHeight / imgHeight
        Else
          'Scale by the Width
          scaleFactor = maxWidth / imgWidth
        End If
        
        imgWidth *= scaleFactor
        imgHeight *= scaleFactor          
      End If    
      html = &quot;<a href=&quot;&quot;&quot; & IMAGE_DIRECTORY & Path.GetFileName(s) & &quot;&quot;&quot;>&quot; & _
             &quot;<img src=&quot;&quot;&quot; & IMAGE_DIRECTORY & Path.GetFileName(s) & &quot;&quot;&quot;&quot; & _
                  &quot;height=&quot;&quot;&quot; & imgHeight & &quot;&quot;&quot; width=&quot;&quot;&quot; & imgWidth & &quot;&quot;&quot;>&quot; & _
             &quot;</a>&quot;
      pics.Add(html)
    Next
    dlPictures.DataSource = pics
    dlPictures.DataBind()
  End Sub
</script>
<HTML>
<HEAD>
<title>Test Page</title>		
</HEAD>
<body>
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
  <asp:DataList runat=&quot;server&quot; id=&quot;dlPictures&quot;  RepeatColumns=&quot;3&quot;
        Width=&quot;600&quot; CellPadding=&quot;5&quot; ItemStyle-HorizontalAlign=&quot;Center&quot;>
    <ItemTemplate>
      <%# Container.DataItem %>
    </ItemTemplate>
  </asp:DataList>
</form>
</body>
</HTML>
...will repost after further analysis (these two pages are submitted here for others who may drop by - I realize the second page is not the code you submitted - just serves as a foundation to move ahead).
 
So, is it that I'm not declaring the @ Page language as VB? I've tried commenting out both @ namespace lines, and it still doesn't work. I'm thinking ASP.NET isn't set up correctly on the server, because if I change the extension to .asp It works, except I get ASP errors such as: The specified 'Import Namespace=&quot;System.Drawing.Imaging&quot; ' option is unknown or invalid.

Now, whether that option is indeed invalid, or if it's only for .NET, it doesn't matter, because if I comment it out completely, I'm still unable to access .aspx files.
 
xStrong: After testing your code I am going to create a 2 column table that will look like:
Code:
Thumbnail1   Hyperlink1    Thumbnail3   Hyperlink3
.....        Comments1     .....        Comments3
.....        Comments1     .....        Comments3
Thumbnail1   Comments1     Tumbnail3    Comments3

Thumbnail2   Hyperlink2    Thumbnail4   Hyperlink4
.....        Comments2     .....        Comments4
.....        Comments2     .....        Comments4
Thumbnail2   Comments2     Thumbnail4   Comments4
Where the hyperlink will open an aspx page with a datagrid related to the image and the Comment area will return a string attached to the image. When finished I'll post this page also.
 
If you look at my original script, all I'm trying to do is resize one image for now, although the directory thumbnailing would be fun in the future, I don't need it at the moment.

However, I have a more fundamental problem, which is that even if I change a simple HTML file to an .aspx extension, I am still asked to log in and then told that I'm not authorized to view the page. So, it appears that ASP.NET is not set up correctly on the server, even though my host's home page at clearly states that they support ASP.NET.

What am I to do?
 
Ok x. I hear ya. I'm no expert in that area -- all I know is that IIS and Frontpage extensions need to be working correctly, and permissions assigned on the server, etc... After looking at your script I see what you're doing now.

There are quite a few experts on this site (I'm relatively new at all this and just try to help on occassion) so perhaps one may drop by and give you some advice in this area. If I come across any information that may help I'll post back.
 
I thought I had already posted this before, but it's not on here.

I got it all fixed, and here's my final code:

<%@ Page Language=&quot;VB&quot; Debug=&quot;true&quot; %>
<%@ Import Namespace=&quot;System.IO&quot; %>
<%@ Import Namespace=&quot;System.Drawing.Imaging&quot; %>
<script language=&quot;VB&quot; runat=&quot;server&quot;>
Function ThumbnailCallback() as Boolean
Return False
End Function

Sub Page_Load(sender as Object, e as EventArgs)

'Read in the image filename to create a thumbnail of
Dim imageUrl as String = Request.QueryString(&quot;img&quot;)

'Read in the width and height
Dim maxHeight as Integer = Request.QueryString(&quot;h&quot;)
Dim maxWidth as Integer = Request.QueryString(&quot;w&quot;)

''Make sure that the image URL doesn't contain any /'s or \'s
'If imageUrl.IndexOf(&quot;/&quot;) >= 0 Or imageUrl.IndexOf(&quot;\&quot;) >= 0 then
''We found a / or 'Response.End()
'End If

'Add on the appropriate directory
'imageUrl = &quot;../images/&quot; & imageUrl
imageURL = &quot;../&quot; & imageURL

'debug
'Response.write(imageURL)

Dim fullSizeImg as System.Drawing.Image
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imageUrl))

Dim imgHeight as Integer
imgHeight = fullSizeImg.Height
Dim imgWidth as Integer
imgWidth = fullSizeImg.Width
If imgWidth > maxWidth OR imgHeight > maxHeight then
'Determine what dimension is off by more
Dim deltaWidth as Integer = imgWidth - maxWidth
Dim deltaHeight as Integer = imgHeight - maxHeight
Dim scaleFactor as Double

If deltaHeight > deltaWidth then
'Scale by the height
scaleFactor = maxHeight / imgHeight
Else
'Scale by the Width
scaleFactor = maxWidth / imgWidth
End If

imgWidth *= scaleFactor
imgHeight *= scaleFactor
End If

'Do we need to create a thumbnail?
Response.ContentType = &quot;image/jpeg&quot;
If imgHeight > 0 and imgWidth > 0 then
Dim dummyCallBack as System.Drawing.Image.GetThumbNailImageAbort
dummyCallBack = New _
System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)

Dim thumbNailImg as System.Drawing.Image
thumbNailImg = fullSizeImg.GetThumbnailImage(imgWidth, imgHeight, _
dummyCallBack, IntPtr.Zero)

thumbNailImg.Save(Response.OutputStream, ImageFormat.Jpeg)
Else
fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg)
End If

End Sub
</script>

Now, I don't know whether System.IO is necessary, but it's a really nifty script. Uses JPEG format instead of 4Guys' GIF to get more clarity on image resize. The image dimensions are specified in the QueryString, but it keeps the image proportions the same - i.e. if the original is a 150x200 img, and you specify h=100&w=100, it will give you a 75x100 img.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top