Setting the imageurl of an image dynamically

cpopham

Junior Contributor
Joined
Feb 18, 2004
Messages
273
I want to be able to show images using a random number generator. I have that working fine and the Select Case working fine, but how do I go about assigning the imageurl?

My images are located in the same bin folder for my project.

I have tried things like:

Me.imgMain.ImageUrl = "\1.jpg"

Me.imgMain.ImageUrl = "/1.jpg"

Does anyone know how to do this?

Also is there away to code the image so that it will stay the same ration when the images are shown to prevent distortions?

Thanks, Chester
 
cpopham said:
Me.imgMain.ImageUrl = "\1.jpg"

Backslashes are used for physical paths and I think you'd either get unpredictable or erroneous results using them in a web context.

cpopham said:
Me.imgMain.ImageUrl = "/1.jpg"

The leading slash makes a URL path be relative to the virtual root of the web server - so, saying "/1.jpg" would be the same as saying "http://www.yourdomain.com/1.jpg" - which doesn't include your project name (or project folder) in the path and probably isn't what you want.

cpopham said:
My images are located in the same bin folder for my project.

Does anyone know how to do this?

You'd want to set the path including the subfolder name (in relation to your project folder), such as:

Visual Basic:
Me.imgMain.ImageUrl = "bin/1.jpg"

...if you are, in fact, using your project's bin folder to store you images (which I wouldn't consider to be a very good idea - make a seperate images folder or something instead and do the path similarly).

cpopham said:
Also is there away to code the image so that it will stay the same ratio when the images are shown to prevent distortions?

Images should maintain the proper size unless you are adding height and width attributes to the image tag (in the ASPX) to "presize" the images. The simple solution would be to not add height and width attributes to your image tag. A more complex solution would be to load and analyze each image server-side to be able to set the height and width properties of the asp image object appropriately. Personally - I'd just ditch height and width from your ASPX and let the client's browser do the work.

Paul
 
Okay thanks, for the help. I put the images in a seperate folder. Now when I put this out on the live web server with ftp, what files and folders do I need to put out? The bin and images folders I know for sure, but what else do I nned?

Chester
 
This kind of depends on what all you have involved in your project/site.

For the basics, at the root level of your project you'd want all your .aspx files. Then of course you'd want your .jpg files in your images folder and your assembly(ies) (.dll) in your bin folder. Ideally, when you are set to publish you build and post 'release' version(s) of your assembly(ies).

If you use any linked stylesheets, linked javascript files, or include files you'd also want to post those to the appropriate spot.

You may or may not need to post your global.asax and web.config files - it depends on whether you ever did anything with them. If you don't use them and haven't edited them you don't need to post them - but it wouldn't hurt to include them even if you don't use them. So, optionally put the 'global.asax' and 'web.config' files in your main project folder (same folder level as your main .aspx files).

You don't need to put your class files (.cs or .vb depending on language) or your debug files (.pdb) on the live server.

You need to build the proper folder structure for your resources - like you images and bin folders - on the web server - but I think you already have that under control.

So, if you had a project called 'myproject' you might have a structure like this on your server for "www.somedomain.com/myproject":

default.aspx
mypage2.aspx
mypage3.aspx
mystyles.css
mycommonjavascriptstuff.js
images/whatever.jpg
images/blah.jpg
images/etc.jpg
bin/myproject.dll
bin/mycommonobjects.dll

Some of this stuff you might not have - it's just there to illustrate (like the .css, .js, and extra .dll).

Paul
 
Back
Top