0

Hi All,

I have a problem with this piece of code:

string fileName =  Label1.Text.Replace(" ","_") + ".jpg";
string localImageUrl = Server.MapPath(@"Images\" + fileName);

The image does not show. When I ommit @Images\ abn move the images to the root of the project the image does show.

any ideas?

Regards,
Frank

2
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by fradekie
0

Take a look in the debugger at the full path:

string localImageUrl1 = Server.MapPath(@"Images\" + fileName);
string localImageUrl2 = Server.MapPath(fileName);
System.Diagnostics.Debugger.Break();

Compare the two strings and see what the difference is. Also -- What does your directory structure look like where the image is housed?

0

there is no difference in the strings other than "Images" :

"C:\Users\frank\Documents\Visual Studio 2005\Projects\WebApplication3\WebApplication3\Images\Meetstation_Berkhout.jpg"
"C:\Users\frank\Documents\Visual Studio 2005\Projects\WebApplication3\WebApplication3\Meetstation_Berkhout.jpg"

I attached the structure of my project.

Thanks
Frank

Edited by happygeek: fixed formatting

Attachments structure.jpg 57.85 KB
0

Server.MapPath() gives you the local image path but you need to reference the virtual path in your web project. Can you post code for the entire page?

0

Here is the code enjoy:)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Reflection;


namespace WebApplication3
{
    public partial class _Default : System.Web.UI.Page
    {


        public string weerstation() 
        {
            string weerstation = "weerstation";
            return weerstation;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {


            weerstation();
            XmlNodeList list1 = null;
            loaddoc();
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(@"App_Data\br.xml"));
            
            list1 = doc.GetElementsByTagName(weerstation());

           
            createlistb(list1);
           
            
        }


        public void loaddoc() 
        {
            
            DateTime starttime = File.GetLastWriteTime(Server.MapPath(@"App_Data\br.xml"));
            DateTime endTime = DateTime.Now;

            TimeSpan hoursAgo = endTime.Subtract(starttime);

            if (hoursAgo.Hours > 1)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("http://xml.buienradar.nl");
                doc.Save(Server.MapPath(@"App_Data\br.xml"));
            }
            
            Label2.Text = "Hours Ago: " + hoursAgo.Hours.ToString() + " File TimeStamp: " 
                + File.GetLastWriteTime(Server.MapPath(@"App_Data\br.xml")).ToString() + " Nu is het: " 
                + DateTime.Now.ToString(); 
                 
                 //File.GetLastWriteTime(Server.MapPath(@"App_Data\br.xml")).ToString();
        }
        public void loaddocForced()
        {
            
                XmlDocument doc = new XmlDocument();
                doc.Load("http://xml.buienradar.nl");
                doc.Save(Server.MapPath(@"App_Data\br.xml"));
            

            Label2.Text = "File TimeStamp: "
                + File.GetLastWriteTime(Server.MapPath(@"App_Data\br.xml")).ToString() + " Nu is het: "
                + DateTime.Now.ToString();

            
        }

        protected void cmdForceUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                
                loaddocForced();
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath(@"App_Data\br.xml"));
               

                Label2.Text = "Update klaar";
            }
            catch (Exception ex)
            {

                Label1.Text = ex.Message;
            }
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            try
            {
                
                XmlDocument doc = new XmlDocument();
                XmlNodeList list1 = null;
                doc.Load(Server.MapPath(@"App_Data\br.xml"));
                list1 = doc.GetElementsByTagName(weerstation());

            
                Label3.Text = "\r\nDatum        : " + list1[ListBox1.SelectedIndex].ChildNodes[4].InnerText;
                Label4.Text = "\r\nTemperatuur  : " + list1[ListBox1.SelectedIndex].ChildNodes[6].InnerText + " graden";
                Label5.Text = "\r\nNeerslag     : " + list1[ListBox1.SelectedIndex].ChildNodes[14].InnerText + " MM";
                Label6.Text = "\r\nWindrichting : " + list1[ListBox1.SelectedIndex].ChildNodes[10].InnerText;
                Label1.Text = list1[ListBox1.SelectedIndex].ChildNodes[1].InnerText;
                string lat = list1[ListBox1.SelectedIndex].ChildNodes[2].InnerText;
                string lon = list1[ListBox1.SelectedIndex].ChildNodes[3].InnerText;

                string Imageloc = list1[ListBox1.SelectedIndex].ChildNodes[15].InnerText;

                
                string imagelocMap = "http://maps.google.com/staticmap?&markers=" + lat + "," + lon + "," + Marker() + "&zoom=" + Zoom() + "&size=240x240&key=" + GoogleApi(); 
                    
              
                string fileName =  Label1.Text.Replace(" ","_") + ".jpg";
                string localImageUrl1 = Server.MapPath(@"Images\" + fileName);
                string localImageUrl2 = Server.MapPath(fileName);

               // System.Diagnostics.Debugger.Break();
               
               
                System.Net.WebClient client = new System.Net.WebClient();
                if (!File.Exists(localImageUrl1))
                {
                client.DownloadFile(imagelocMap, localImageUrl1);
                }
                   Label2.Text = Imageloc + "\r\n " + localImageUrl1 + " " +  fileName + "  " + File.Exists(localImageUrl1);
                
                Image1.ImageUrl = Imageloc;
                Image2.ImageUrl = localImageUrl1;
            }
            catch (Exception ex)
            {

                Label1.Text = ex.Message;
            }
            
            
                ListBox1.Focus();
              
           
            
        }
        private void createlistb(XmlNodeList list1)
        {
            
          
            ListBox1.Items.Clear();

            foreach (XmlNode node in list1)
            {
                ListBox1.Items.Add(node.ChildNodes[1].InnerText);
            }
               
        }

        private string GoogleApi()
        {

            string GoogleMapKey = ConfigurationManager.AppSettings["GoogleMapKey"];
            
            return GoogleMapKey;
        }
        private string Marker()
        {
            string marker = ConfigurationManager.AppSettings["markercolor"];

            return marker;
        }

        private string Zoom()
        {
            string zoom = ConfigurationManager.AppSettings["zoom"];

            return zoom;
        }
    }
}
0

According to your code:

string Imageloc = list1[ListBox1.SelectedIndex].ChildNodes[15].InnerText;
string localImageUrl1 = Server.MapPath(@"Images\" + fileName);
string localImageUrl2 = Server.MapPath(fileName);
Image1.ImageUrl = Imageloc;
Image2.ImageUrl = localImageUrl1;

You're comparing the working image to the listBox selected item and localImageUrl2. You described comparing localImageUrl1/localImageUrl2. What does "Imageloc" evaluate to in the designer?

It looks like you may have your wires crossed here.

[edit]unless those are intended to be two separate images?[/edit]

0

unless those are intended to be two separate images?

Yes, these are two seperate images.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.