Create Image from relative path

D

David W. Simmonds

I know that I can create an Image object by using a path like
C:\MyPictures\pic.jpg, but how can I create an Image object by using a path
like /MyPictures/pic.jpg where the path is relative to the site. For
instance, if the site is http://www.foobar.com, then /MyPictures/pic.jpg
would be fully qualified by http://www.foobar.com/MyPictures/pic.jpg.
Presumably there is a way to get the fully qualified path by giving the
relative path.
 
D

David W. Simmonds

Ok, great.

Now, to complicate the issue. What if the file exists on a completely
different web server than the asp.net code is executing on. The path to the
file would be the fully qualified path. How can an Image object be created
this way?
 
C

Chad McCune

Combine the System.Drawing.Image.FromStream() method with the
System.Net.WebClient class

Chad Mccune, MCSE, MCDBA
 
S

Steven Cheng[MSFT]

Hi David,


Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
assisting you on this issue.
From your description, your current question is how to create an Image from
a remote site via the Url address such as
"http://www.foobar.com/MyPictures/pic.jpg"
If there is anything I misunderstood, please feel free to let me know.

First , I'd like to confirm something on this issue:
Does the "create" you mentioned in your questoin means to create a Image
object(System.Drawing.Image) instance which contains the image stream of
the remote picture or just want to show a picture on a web page via its url
address?

If you just want to how a picture on a page via its Url address, then you
can use the "System.Web.UI.WebControls.Image" control or even just use the
"<img ..>" html element. As for the Image control, you can use its
"ImageUrl" property to specify a certain picture's Url address. Such as:
imgMS.ImageUrl =
"http://support.microsoft.com/library/images/support/bnr_microsoft.gif";

If you use the "<img >" html element, you can specify a picture like below:
<img
src="http://support.microsoft.com/library/images/support/bnr_microsoft.gif"
/>

Else case, if you want to retrieve a image stream from a remote site and
store into a "System.Drawing.Image"
class object. I think you need to consider Chad McCune's suggestion:
Using the WebClient class to get the remote file stream and store into a
Image object(System.Drawing.Image).
For example:
System.Net.WebClient wc = new System.Net.WebClient();
Stream sm =
wc.OpenRead("http://support.microsoft.com/library/images/support/bnr_microso
ft.gif");
System.Drawing.Image img = System.Drawing.Image.FromStream(sm);
img.Save(Server.MapPath("MS.gif"));


Please check out the preceding items to see whether they are helpful. If
you feel my suggestions not quite suitable for your situation, please feel
free to let me know more about your requirement so as for me to search for
further resource to assist you.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top