Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a Stream from an absolute path?

I have this method:

public RasImage Load(Stream stream);

if I want to load a url like:

string _url = "http://localhost/Application1/Images/Icons/hand.jpg";

How can I make this url in to a stream and pass it into my load method?

like image 999
VoodooChild Avatar asked Sep 03 '10 20:09

VoodooChild


People also ask

How to get file absolute path java?

io. File. getAbsolutePath() is used to obtain the absolute path of a file in the form of a string. This method requires no parameters.

How do you find the absolute path?

To find the full absolute path of the current directory, use the pwd command. Once you've determined the path to the current directory, the absolute path to the file is the path plus the name of the file.

What is Java Inputstream file?

A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader .


Video Answer


1 Answers

Here's one way. I don't really know if it's the best way or not, but it works.

// requires System.Net namespace
WebRequest request = WebRequest.Create(_url);

using (var response = request.GetRespone())
using (var stream = response.GetResponseStream())
{
    RasImage image = Load(stream);
}

UPDATE: It looks like in Silverlight, the WebRequest class has no GetResponse method; you've no choice but to do this asynchronously.

Below is some sample code illustrating how you might go about this. (I warn you: I wrote this just now, without putting much thought into how sensible it is. How you choose to implement this functionality would likely be quite different. Anyway, this should at least give you a general idea of what you need to do.)

WebRequest request = WebRequest.Create(_url);

IAsyncResult getResponseResult = request.BeginGetResponse(
    result =>
    {
        using (var response = request.EndGetResponse(result))
        using (var stream = response.GetResponseStream())
        {
            RasImage image = Load(stream);
            // Do something with image.
        }
    },
    null
);

Console.WriteLine("Waiting for response from '{0}'...", _url);
getResponseResult.AsyncWaitHandle.WaitOne();

Console.WriteLine("The stream has been loaded. Press Enter to quit.");
Console.ReadLine();
like image 106
Dan Tao Avatar answered Sep 20 '22 21:09

Dan Tao