Changing image path on the fly

Last post 08-29-2008 2:37 PM by ASQ. 4 replies.

Sort Posts:

  • Changing image path on the fly

    08-28-2008, 11:29 PM
    • Member
      187 point Member
    • ASQ
    • Member since 10-26-2006, 5:07 AM
    • Modimolle, SA
    • Posts 101

    Hi,

    This seem like a fairly basic thing to do, but I just can not get it right!?

    I want to allow the visitors to my web site the option to decide if they want to see images at a lower resolution and thus increase the speed of my web site. To do this, I have a 3 different directories with the same image in it, just the size (800x600, 640x480) of the images differ in each directory. Depending on the users preference, I want to show the picture, but change the directory to appropriate directory in the page_load. My code looks something like this:

    In the web form:

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <asp:Image ID="Image1" runat="server" ImageUrl="<%=graphicsDir %>" />
    </asp:Content>

    In the code behind:

    string graphicsDir = "~/images/"; // variable with path to the directory where the graphics are

    if (width = 640)
    {
    //which graphics directory should be used?
    graphicsDir = graphicsDir + "640/myPicture.jpg";
    }
    if (width = 800)
    {
    //which graphics directory should be used?
    graphicsDir = graphicsDir + "800/myPicture.jpg";
    }

    I do not get an error, but the picture does not show at all. If I "right click" on the empty place holder, then in the picture properties there are no information regarding the picture - so the variable in the HTML was not updated:

    Not Available
    Protocol: Not Available
    Type: Not Available
    Address(URL): Not Available
    Size: Not Available

    But, if I display the value of "graphicsDir" in a Label, then it shows the correct path, for example: "~/images/800/arch.jpg"

    How can I solve this, or is it not possible to do this with a asp:image?

    Filed under: ,
  • Re: Changing image path on the fly

    08-29-2008, 8:09 AM
    • Participant
      1,328 point Participant
    • NecroxPy
    • Member since 08-17-2008, 1:37 PM
    • Asunción - Paraguay
    • Posts 244

    Change this line:

    string graphicsDir = "~/images/";

    To this line:

    string graphicsDir = Request.ApplicationPath + "images/";

    Regards Stick out tongue

    Christian Manuel Amado Silva
    [MCITP] Windows Vista Consumer Support Technician
    [MCTS] Windows Vista, Configuration
    [MCTS] .NET Framework 2.0: Web Applications
    [MCTS] Microsoft Windows Mobile 5.0 Application Development
    [MCTS] Microsoft SQL Server 2005
    [MOS] Microsoft Office Master Specialist

    Mark a post as an answer when it is
  • Re: Changing image path on the fly

    08-29-2008, 2:33 PM
    • Member
      187 point Member
    • ASQ
    • Member since 10-26-2006, 5:07 AM
    • Modimolle, SA
    • Posts 101

    Hi,

    Ok, I have made this change, so now I have the following code to get the main image directory:

    string graphicsDir = Request.ApplicationPath + "/images/";

    But the result is still the same - unfortunately no change. 

    The problem does not seem to be the actual path, but more that the path does not resolve into the variable "graphicsDir":  <asp:Image ID="Image1" runat="server" ImageUrl="<%=graphicsDir %>" />

    If I manually enter the path and file name, then the picture displays correctly, for example:

    <asp:Image ID="Image1" runat="server" ImageUrl="~/images/800/myPicture.jpg" /> or <asp:Image ID="Image1" runat="server" ImageUrl="/testProject/images/800/myPicture.jpg" />

     How do I get the variable "graphicsDir" that is embedded inside the HTML to be replaced with the correct path and file information, so that the image is displayed?

  • Re: Changing image path on the fly

    08-29-2008, 2:35 PM
    • Member
      187 point Member
    • ASQ
    • Member since 10-26-2006, 5:07 AM
    • Modimolle, SA
    • Posts 101

    Hi,

    Ok, I have made this change, so now I have the following code to get the main image directory:

    string graphicsDir = Request.ApplicationPath + "/images/";

    But the result is still the same - unfortunately no change. 

    The problem does not seem to be the actual path, but more that the path does not resolve into the variable "graphicsDir":  <asp:Image ID="Image1" runat="server" ImageUrl="<%=graphicsDir %>" />

    If I manually enter the path and file name, then the picture displays correctly, for example:

    <asp:Image ID="Image1" runat="server" ImageUrl="~/images/800/myPicture.jpg" /> or <asp:Image ID="Image1" runat="server" ImageUrl="/testProject/images/800/myPicture.jpg" />

     How do I get the variable "graphicsDir" that is embedded inside the HTML to be replaced with the correct path and file information, so that the image is displayed?

  • Re: Changing image path on the fly

    08-29-2008, 2:37 PM
    Answer
    • Member
      187 point Member
    • ASQ
    • Member since 10-26-2006, 5:07 AM
    • Modimolle, SA
    • Posts 101

    To give feedback, should anyone ever experience the same problem:

    I managed to get this working, by using the either of the following two options:

    <img src="<%=Page.ResolveUrl(graphicsDir) %>" alt="My Picture" /> or

    <img src="<%=graphicsDir %>" alt="My Picture" />

    I could not get it to work with the standard "asp:Image" control. If anyone reading this know why, then please let us know so that we understand it for future reference.

Page 1 of 1 (5 items)
Microsoft Communities