Accessing image from other server

4.1k views Asked by At

I have image file placed on the one server and application on other server.

I want to access that image, below code I have written:

On default.aspx, I have

 <asp:Image ID="Image1" runat="server"  ImageUrl= "GetImage.aspx?imgName=MyImage.jpg" />

and on GetImage.aspx, I have written the below code on page_load

 protected void Page_Load(object sender, EventArgs e)
    {
        // Changing the page's content type to indicate the page is returning an image
        Response.ContentType = "image/jpg";
        var imageName = Request.QueryString["imgName"];
        var path = "//SERVER/FOLDER/" + imageName;


        if ((string.IsNullOrEmpty(imageName) == false))
        {
            // Retrieving the image
            System.Drawing.Image fullSizeImg;
            fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path));
            // Writing the image directly to the output stream
            fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg);
            // Cleaning up the image
            fullSizeImg.Dispose();
        }
    }

But I am getting error at

fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path));

Please let me know where I am incorrect. Do I need to anything else other than Server.MapPath ? because my image is on other server.

EDIT

  • I have image folder in my computer
  • I have created a web app in other computer [same network], deployed in IIS, image is displayed correctly. With path like http://10.67.XX.XX/websiteName/Default.aspx
  • but when I am trying to access the same from my comupter or any other computer, I am not able to see the image.
2

There are 2 answers

10
Wiktor Zychla On BEST ANSWER

You shouldn't use Server.MapPath. This is used to map virtual paths under your site to physical paths under file system. If the file exists on another server, just access it by name directly, without Server.MapPath.

0
Geoff On

The answer above is incorrect because the images reside on a separate server.

So System.Images will not no where the image is.

Secondly you have to use server.Mappath for system images, it requires a windows path i.e. c:\blah\blah\whatever.jpg. you will need to use \\server\C$\folder\image.jpg this works well with system.image.FromFile

Also saving you can also utilize this.

Cheers