locked
Stream a remote image RRS feed

  • Question

  • User1510859543 posted

    We are trying the code below to stream an image on our company public website on another remote server to our intranet site.  Below is the image element and the relevant code in page-behind.  Apparently we cannot use a URL as a file, even though the remote image shows up fine in the image control.  What can we use to stream this remote file as we get an error about "URI formats are not supported" on the OpenRead property.

            <p>
                <asp:Image ID="Image1" runat="server" Width="300" />
            </p>
    
    
    
            Dim strSource As String = "http://myweb.com/uploads/22018111908_4.jpg"
            Dim strDest As String = Server.MapPath("~/images/")
            strDest &= "9084833_4.jpg"
    
            Image1.ImageUrl = "http://myweb.com/uploads/2201811_3.jpg"
            Dim filepath As String = strDest
            Dim bmpImg As Bitmap = Nothing
            Using fileStream As FileStream = File.OpenRead("http://myweb.com/uploads/2201811_3.jpg")
                Dim memStream As New MemoryStream()
                memStream.SetLength(fileStream.Length)
                fileStream.Read(memStream.GetBuffer(), 0, CInt(fileStream.Length))
                bmpImg = FilesClass.Resize_Image(fileStream, 2533, 1900)
                bmpImg.Save(filePath, ImageFormat.Jpeg)
                fileStream.Close()
                fileStream.Dispose()
            End Using
    

    Wednesday, November 21, 2018 8:50 PM

Answers

All replies

  • User475983607 posted

    We are trying the code below to stream an image on our company public website on another remote server to our intranet site.  Below is the image element and the relevant code in page-behind.  Apparently we cannot use a URL as a file, even though the remote image shows up fine in the image control. 

    It sounds like all you have to do is use the full URL as the image source.  If you do not know the URL, then right click the image and select "Copy link Address" or view the HTML source with dev tools.

    Wednesday, November 21, 2018 9:15 PM
  • User1510859543 posted

    I thought I was using the URL as file source when setting the filestream.  What am I doing wrong?

    Wednesday, November 21, 2018 10:07 PM
  • User475983607 posted

    I thought I was using the URL as file source when setting the filestream.  What am I doing wrong?

    Yeah, you're pretty far off...  The file path is needed not the URL.

    Place the URL in your browser's address bar and press enter.  You should see the image in the browser. 

    If you do not see the image then the URL is probably incorrect.  Find the image URL by going a page that shows the image, right click the image, and select "Copy link Address".  Paste the URL in your browser and press enter.  Now you should see the image.

    Finally, set the ImageUrl to the URL in the browser's address bar.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.image.imageurl?view=netframework-4.7.2

    Wednesday, November 21, 2018 11:11 PM
  • User1510859543 posted

    Yes, I see the image when putting the URL in address bar.  I am not interested in the image control but rather streaming that remote image to a file.  I must be missing something. Please turn your attention to the streaming of that url.

    Thursday, November 22, 2018 12:11 AM
  • User475983607 posted
    You keep using “streaming” which is not clear. Where do you want the image? In the browser? Copied to another server? What does streaming mean to you?
    Thursday, November 22, 2018 1:31 AM
  • User-893317190 posted

    Hi dlchase,

    It seems you want to get image from your remote website and deal with it in your local machine with c#.

    If so, you should pay attention,File.OpenRead could only open file in local machine.

    Since the image is in another machine, you could use File,OpenRead.

    I suggest you could use HttpClient to get the image as byte[], HttpClient could send http request to a remote website and you could get the image through the request.

    Below is my test code. You could import HttpClient  and Task through ctrl + alt + f10

      Dim client As HttpClient = New HttpClient()
    
            Dim bys As Task(Of Byte()) = client.GetByteArrayAsync("http://localhost:57510/images/WingtipToys/boatbig.png") 
            Using stream As MemoryStream = New MemoryStream(bys.Result) 'bys.Result   get the Byte[] of image from the result
                Using bmpImg As Bitmap = New Bitmap(stream)
                    bmpImg.Save(Server.MapPath("/images/test.png"))
                End Using
    
            End Using

    The result.

    Best regards,

    Ackerly Xu

    Thursday, November 22, 2018 2:10 AM
  • User1510859543 posted

    Yes, to a file. I am going with Ackerly Xu solution and see if that works.

    Thursday, November 22, 2018 2:16 PM
  • User1510859543 posted

    Using your code example, I get an error on the first line where you set HttpClient variable. The error states "Type 'HttpClient' is not defined."

    Thursday, November 22, 2018 2:28 PM
  • User-893317190 posted

    Hi dlchase,

    Please add reference to System.Net.Http

    You could also use WebRequest to make a  http request,please refer to https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-request-data-using-the-webrequest-class

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 23, 2018 1:40 AM
  • User1510859543 posted

    When I search assemblies I only get System.Net returned and no System.Net.Http so I would like to try a webrequest.  I found the code below in C# but I want to be able to save the request stream as a bitmap jpg file.  What do I need to add/replace below to make that happen?

            public static void Main()  
            {  
                // Create a request for the URL.   
                WebRequest request = WebRequest.Create(  
                  "http://www.contoso.com/default.html");  
                // If required by the server, set the credentials.  
                request.Credentials = CredentialCache.DefaultCredentials;  
                // Get the response.  
                WebResponse response = request.GetResponse();  
                // Display the status.  
                Console.WriteLine (((HttpWebResponse)response).StatusDescription);  
                // Get the stream containing content returned by the server.  
                Stream dataStream = response.GetResponseStream();  
                // Open the stream using a StreamReader for easy access.  
                StreamReader reader = new StreamReader(dataStream);  
                // Read the content.  
                string responseFromServer = reader.ReadToEnd();  
                // Display the content.  
                Console.WriteLine(responseFromServer);  
                // Clean up the streams and the response.  
                reader.Close();  
                response.Close();  
            }  
    

    Monday, November 26, 2018 2:35 PM
  • User1510859543 posted

    I figured it out and am able to save the request image.

    Monday, November 26, 2018 6:44 PM