Error showing an image on image webcontrol

The image control is blank with a red X on the left top corner

Page 1 of 1

2 Replies - 5778 Views - Last Post: 04 November 2007 - 05:17 AM Rate Topic: -----

#1 rodniko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-November 07

Error showing an image on image webcontrol

Posted 04 November 2007 - 02:16 AM

i'm building a simple application about showing images that are placed inside a folder ("Images" folder under wwwroot)
with a linkbutton of "Previous picture" and "next Picture".
my problem is that the image control doesn't show the image although it gets the right path ( i think so )
i use VS2005. with explorer 6.0

i want to display an image that placed in the path C:\Inetpub\wwwroot\Images\bluehills.jpg through the code.

when i use the properies page to set the imageUrl to "C:\inetpub\wwwroot\Images\Bluehills.jpg" it works great.

but when i do it from the code by setting imageUrl to "http://localhost:1096/Images/Bluehills.jpg" it doesn't work - i cant see nothing on the image control.

if i write the address :
"http://localhost:1096/Default.aspx" in the browser myself i see the same problem - a blank image control

if i try to show the image on the browser, with this address :
"http://localhost:1096/Images/Bluehills.jpg"
i get this error:


------------------------------------------------------------------------------


Server Error in '/' Application.
--------------------------------------------------------------------------------

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Images/Bluehills.jpg


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832

-------------------------------------------------------------------------------

Does that has to do with the location of my project ? cause it is placed in

"C:\Documents and Settings\asaf\My Documents\Visual Studio 2005\Projects"

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.IO;

namespace Chapter3PicView
{
	public partial class _Default : System.Web.UI.Page
	{
		private String[] m_arrImageNames;
		private String m_StrImageDirectory = @"C:\inetpub\wwwroot\Images";
		private int imageNum = 0;

		protected void Page_Load(object sender, EventArgs e)
		{
			// if it is the first load ( not refresh )
			if (!IsPostBack)
			{
				// if there are images in the folder
				if (GetImages())
				{
					// Set the ImageURL of Image1 to the first image that was found
					Image1.ImageUrl = GetImageURL(m_arrImageNames[0]);
					
				}
			}
		}

		protected void LinkButton1_Click(object sender, EventArgs e)
		{
			ChangeImage(-1);
		}

		protected void LinkButton2_Click(object sender, EventArgs e)
		{
			ChangeImage(1);
		}

		private void ChangeImage(int intChange)
		{
			// Get the list of images
			if (GetImages())
			{
				// update image index
				imageNum += intChange;

				// show image
				Image1.ImageUrl = GetImageURL(m_arrImageNames[imageNum]);
			}
		}

		private bool GetImages()
		{
			// Get the image filenames
			m_arrImageNames = Directory.GetFiles(m_StrImageDirectory);

			// Make sure there are images to display
			if (m_arrImageNames.Length == 0)
				return false;

			return true;
		}

		private String GetImageURL(String StrImageFilePath)
		{
			String Str1 = Request.Url.GetLeftPart(UriPartial.Authority);
			//String Str2 = Request.ApplicationPath;
			String Str3 = @"/Images/";
			String Str4 = StrImageFilePath.Substring(m_StrImageDirectory.Length +1);
			return Str1 + Str3 + Str4;
		}

	}
}



The markup code is :

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Chapter3PicView._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
	<title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" Width="98px">Previous Image</asp:LinkButton> <asp:Image
			ID="Image1" runat="server" Height="363px" ImageUrl="C:\inetpub\wwwroot\Images\Bluehills.jpg"
			Width="336px" /> 
		<asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">Next Image</asp:LinkButton>
	
	</div>
	</form>
</body>
</html>



could it be i have two different problems? :
1. showing image on control
2. showing image on browser

how do i solve them?
thanks

*Always use code tags:) => :code:

This post has been edited by PsychoCoder: 04 November 2007 - 07:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Error showing an image on image webcontrol

#2 Footsie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 24
  • View blog
  • Posts: 370
  • Joined: 20-September 07

Re: Error showing an image on image webcontrol

Posted 04 November 2007 - 03:37 AM

This might be worth a try:
Try and rather put your images in their own folder within your solution. Right click your solution in solution explorer and add "New folder" and name it something like "Pictures". Put all the images your website will use in there and use that path in your url.

EDIT:
You can then use the path as follows
 Image1.ImageUrl = "~/Pictures/bluehills.jpg" 

The ~ operator specifies the root folder of the application then your "Picture" folder and then your"bluehills" image.

Hope this helps.

This post has been edited by Footsie: 04 November 2007 - 03:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 rodniko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-November 07

Re: Error showing an image on image webcontrol

Posted 04 November 2007 - 05:17 AM

View PostFootsie, on 4 Nov, 2007 - 03:37 AM, said:

This might be worth a try:
Try and rather put your images in their own folder within your solution. Right click your solution in solution explorer and add "New folder" and name it something like "Pictures". Put all the images your website will use in there and use that path in your url.

EDIT:
You can then use the path as follows
 Image1.ImageUrl = "~/Pictures/bluehills.jpg" 

The ~ operator specifies the root folder of the application then your "Picture" folder and then your"bluehills" image.

Hope this helps.


You were right about one thing , creating the "Images" folder under my project folder. i stayed with the same code and it suddently works.
i'm confused now....
i thought that "http://localhost:1096" represents the wwwroot folder , but it doesn't , it represents my project folder, since only when i created the images folder under my project everything works.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1