Welcome to Dream.In.Code
Become an Expert!

Join 150,239 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,345 people online right now. Registration is fast and FREE... Join Now!




Error showing an image on image webcontrol

 
Reply to this topicStart new topic

Error showing an image on image webcontrol, The image control is blank with a red X on the left top corner

rodniko
4 Nov, 2007 - 01:16 AM
Post #1

New D.I.C Head
*

Joined: 4 Nov, 2007
Posts: 2


My Contributions
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"

CODE

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 :

CODE

<%@ 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.gif

This post has been edited by PsychoCoder: 4 Nov, 2007 - 06:04 AM
User is offlineProfile CardPM
+Quote Post

Footsie
RE: Error Showing An Image On Image Webcontrol
4 Nov, 2007 - 02:37 AM
Post #2

D.I.C Regular
Group Icon

Joined: 20 Sep, 2007
Posts: 308



Thanked: 4 times
Dream Kudos: 50
My Contributions
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
CODE
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: 4 Nov, 2007 - 02:53 AM
User is offlineProfile CardPM
+Quote Post

rodniko
RE: Error Showing An Image On Image Webcontrol
4 Nov, 2007 - 04:17 AM
Post #3

New D.I.C Head
*

Joined: 4 Nov, 2007
Posts: 2


My Contributions
QUOTE(Footsie @ 4 Nov, 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
CODE
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.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 06:19AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month