473,320 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Image1.ImageUrl not rendering image when retrieving from different Directory

Hi,

I am working on a job portal. I have an Organization picture that I have to Save & Retrieve . My image saving code is working perfectly. But I am dumping the images in my D drive. Now when I use
Expand|Select|Wrap|Line Numbers
  1. Image1.ImageUrl = @"D:\DirecotyPath\Image.jpg";
  2.  
I does not render an image on the front end...
What can be done in this situation? And are there any alternatives to that?


Thanks,

Regards,

Syed Ahmed Hussain
Mar 30 '10 #1

✓ answered by Frinavale

The reason this isn't working is because the image is in a directory that the web server doesn't have access to. So if the image control tries to retrieve the image from the server it wont be able to.

There's a couple of solutions to this problem.
The first is to move the image into a temporary images folder that is on the web server (say in the website) so that the browser can download it.

The second is to implement an ASPX page that retrieves the image (as a stream) and writes it to the Response.OutputStream.

This ASPX page will not return HTML which is what is returned by default..and is expected by the web browser. You need to change the Response.ContentType to "image/jpg" to indicate to the browser that you are sending an image instead.

There is no asp code in this page...it's just pure server-side .NET code that retrieves the image (probably based on a parameter that is provided when the page is called) and writes that image to the stream.

For example, all that would be in your ASPX page would be the following to return the full size image (VB.NET code):
Expand|Select|Wrap|Line Numbers
  1.  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.  
  3.    'Changing the page's content type to indicate the page is returning an image
  4.    Response.ContentType = "image/jpg"
  5.  
  6.    'Retrieving the name of the image 
  7.    Dim imageName = Request.QueryString("imgName")
  8.    Dim path = ""D:\DirectoryPath\"+imgName
  9.  
  10.    If String.IsNullOrEmpty(imageName) = False Then
  11.      'Retrieving the image
  12.      Dim fullSizeImg As System.Drawing.Image
  13.      fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path))
  14.  
  15.      'Writing the image directly to the output stream
  16.      fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg)
  17.  
  18.     'Cleaning up the image
  19.      fullSizeImg.Dispose()
  20.    End If
  21. End Sub
This code would be in a page called, say, GetImage.aspx ....

Now, when you specify the image source for the ImageControl you use "GetImage.aspx?imgName=Gorilla.jpg"

For example, in your page you would have:
Expand|Select|Wrap|Line Numbers
  1. <asp:Image ID="Image1" ImageUrl = "GetImage.aspx?imgName=Gorilla.jpg" />

-Frinny

10 15890
tlhintoq
3,525 Expert 2GB
Have you tried fixing the spelling of 'DirectoryPath'? Maybe you are just going to a folder that doen't exist?

Do you really have a folder named "DirectoryPath"? This sounds like a variable that should have been inserted into a string to make the full path.

Is it reasonable to assume that the viewer of the site doesn't have a D: drive with your photo?
Mar 30 '10 #2
Have you tried fixing the spelling of 'DirectoryPath'? Maybe you are just going to a folder that doen't exist?
What I use here the word " Directory Path" is just a dummy name I am using here.

Do you really have a folder named "DirectoryPath"? This sounds like a variable that should have been inserted into a string to make the full path.

Is it reasonable to assume that the viewer of the site doesn't have a D: drive with your photo?
No it was just an example (Dummy) name to show that any kind of directory that I wish to open It opens easily and all the paths are stored properly in my database. I use the paths from my DB to retrieve the jpgs from that directory of the given drive ..

My aim is actually to save the jpgs anywhere on the disk (for right now, later there will be some changes according to the needs) and it simply will store the path in the Database and when you want to retrieve an image, it simply takes the path from the database and then use as the following code line and there are no issue's in the path I have debugged my program several times.

Expand|Select|Wrap|Line Numbers
  1. Image1.ImageUrl = imagepath;
  2.  
and in image path for example there is a path stored
@"D:\DirectoryPath\Gorilla.jpg"

Now at this point when it render the control, it will show the image control empty.


Regards,

Syed Ahmed Hussain
Mar 30 '10 #3
Frinavale
9,735 Expert Mod 8TB
The reason this isn't working is because the image is in a directory that the web server doesn't have access to. So if the image control tries to retrieve the image from the server it wont be able to.

There's a couple of solutions to this problem.
The first is to move the image into a temporary images folder that is on the web server (say in the website) so that the browser can download it.

The second is to implement an ASPX page that retrieves the image (as a stream) and writes it to the Response.OutputStream.

This ASPX page will not return HTML which is what is returned by default..and is expected by the web browser. You need to change the Response.ContentType to "image/jpg" to indicate to the browser that you are sending an image instead.

There is no asp code in this page...it's just pure server-side .NET code that retrieves the image (probably based on a parameter that is provided when the page is called) and writes that image to the stream.

For example, all that would be in your ASPX page would be the following to return the full size image (VB.NET code):
Expand|Select|Wrap|Line Numbers
  1.  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.  
  3.    'Changing the page's content type to indicate the page is returning an image
  4.    Response.ContentType = "image/jpg"
  5.  
  6.    'Retrieving the name of the image 
  7.    Dim imageName = Request.QueryString("imgName")
  8.    Dim path = ""D:\DirectoryPath\"+imgName
  9.  
  10.    If String.IsNullOrEmpty(imageName) = False Then
  11.      'Retrieving the image
  12.      Dim fullSizeImg As System.Drawing.Image
  13.      fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path))
  14.  
  15.      'Writing the image directly to the output stream
  16.      fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg)
  17.  
  18.     'Cleaning up the image
  19.      fullSizeImg.Dispose()
  20.    End If
  21. End Sub
This code would be in a page called, say, GetImage.aspx ....

Now, when you specify the image source for the ImageControl you use "GetImage.aspx?imgName=Gorilla.jpg"

For example, in your page you would have:
Expand|Select|Wrap|Line Numbers
  1. <asp:Image ID="Image1" ImageUrl = "GetImage.aspx?imgName=Gorilla.jpg" />

-Frinny
Mar 30 '10 #4
tlhintoq
3,525 Expert 2GB
And I repeat
Is it reasonable to assume that the viewer of the site doesn't have a D: drive with your photo?
Meaning that me, sitting at my desk at my house looking at your site... I don't have 'your' D: drive with your D:\\DirectoryPath\Image.jpg on it - now do I?

Frinny you really do have much more patience than I do. It was terribly sweet of you to provide all the code to the OP
Mar 30 '10 #5
Frinavale
9,735 Expert Mod 8TB
No guarantees that it's going to work as is ;)

(but then again it looks right to me)
Mar 30 '10 #6
@tlhintoq :

You needed patience for what ??? :D

After all you both are the moderators, someone gotta be more patient then I thought one should be :) ...

No you dont have D Drive, but again, I am saving it on my Server, right here on my laptop!!!!

I am not deploying my site right now....Moreover wont it be saving the images on the server???
Mar 31 '10 #7
Server, means Client-Server Architecture???? I think you remember that???
Mar 31 '10 #8
Frinavale
9,735 Expert Mod 8TB
When I was referring to the web server I was referring to IIS....
You may consider your laptop as the server computer; but IIS doesn't have access to everything on the laptop.

It all depends on how you set up your website in IIS....but essentially when the browser requests some resource (a web page, an image, a JavaScript file...) the server has to return it. The only way that a browser can do this is using a URI. If the browser makes a request for "D:\somePath\something.xyz" then the server won't know how to translate it to retrieve the resource that you are looking for....never mind that you aren't even going to be able to make a request to the server using "D:\somePath\something.xyz"...the browser's just going to bock at this.

So you either need to copy/move/put the resource somewhere in your website so that you can create a URI/URL to the resource that the browser needs, or you need to create something (an ASPX page for example) that is accessible by the browser that will return the resource requested.

If the resource is on a different drive...you can't really specify a URL to that.

Therefore, I recommended that you create an ASPX page that retrieves the resources and writes it to the browser. This ASPX page is accessible by URL so that the browser can request the resource that is not "on the server" or "in the website".

-Frinny
Mar 31 '10 #9
Frinny :

Thanks, I am learning so much from you guys :)

Regards,

Syed Ahmed Hussain
Mar 31 '10 #10
I found a minor error in line 8:
Expand|Select|Wrap|Line Numbers
  1. Dim path = ""D:\DirectoryPath\"+imgName
where aspx image control property imgName should be the code behind variable imageName or some string literal.
Corrected:
Expand|Select|Wrap|Line Numbers
  1. Dim path = ""D:\DirectoryPath\"+imageName
Jul 26 '16 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application...
0
by: Panic Student | last post by:
hi all, i'm having trouble with changing image when mouseover on a particular label. Below is what i attempted to but it didn't seem to work at all. I guess my concept is wrong ): Please...
3
by: Jakob Petersen | last post by:
Hi, I need to increase the speed when retrieving data from a hosted SQL Server into VBA. I'm using simple SELECT statements. How important is the speed of my Internet connection? (I have...
2
by: blisspikle | last post by:
Is there any way to use the "imports" keyword for your current solution to use code (classes) that were used in a different solution in a different directory without compiling them to a dll? I...
3
by: singhPrabhat | last post by:
I want to compile source files which is in different directory than Makefile. How can I do this? GCC=gcc DEFs=-DCUSTOMERCENTRAL_V11 -DUNIX -DLINUX -DOPENSSL -D_REENTRANT CCWebConfiguration.o:...
9
by: ghjk | last post by:
change the background image when Button Click ====================================== I' developing site with php and postgres. It has menus list in the left side. and all are images(jpg) EX:Add...
1
by: harry4you | last post by:
When retrieving the data from DB2 through Excel do we need to catalog the databases. I am currently using ODBC DSN less connection to connect.
3
by: tess1243 | last post by:
I want the image to be changed when it is hovered, and display the changed image on the hovered image. I want the changed image exactly to display on the hovered image. Here is the code: ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.