can I load image from URL RRS feed

  • Question

  • User-11182147 posted
    when I load an external image like this: string srcPhoto = Request.QueryString["img"]; System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(srcPhoto); it gives error: Exception Details: System.ArgumentException: URI formats are not supported. Source Error: Line 28: //create a image object containing the photograph to watermark Line 29: string srcPhoto = Request.QueryString["img"]; Line 30: System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(srcPhoto); Line 31: int phWidth = imgPhoto.Width; Line 32: int phHeight = imgPhoto.Height; is there any other way to load image from url to System.Drawing.Image imgPhoto Thanks for your help.
    Monday, March 15, 2004 1:51 PM

All replies

  • User1335990741 posted
    What I would do is use the System.Net.WebClient object to download the file to the localdrive and then open it. System.Net.WebClient objWebClient = new System.Net.WebClient objWebClient.DownloadFile(URL, FILENAME) System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(FILENAME)
    Monday, March 15, 2004 4:08 PM
  • User-11182147 posted
    Thanks for the reply. That's a good idea and I'll try that.
    Tuesday, March 16, 2004 8:23 AM
  • User330683833 posted
    If you want to avoid saving the image to the local drive you cal load it into memory directly. I use a utility function to do it... private void LoadImageFromURL( string URL, ref Bitmap Img ) { const int BYTESTOREAD = 10000; WebRequest myRequest = WebRequest.Create( URL ); WebResponse myResponse = myRequest.GetResponse(); Stream ReceiveStream = myResponse.GetResponseStream(); BinaryReader br = new BinaryReader( ReceiveStream ); MemoryStream memstream = new MemoryStream(); byte[] bytebuffer = new byte[ BYTESTOREAD ]; int BytesRead = br.Read( bytebuffer, 0, BYTESTOREAD ); while ( BytesRead > 0 ) { memstream.Write( bytebuffer, 0, BytesRead ); BytesRead = br.Read( bytebuffer, 0, BYTESTOREAD ); } Img = new Bitmap( memstream ); }
    Tuesday, March 16, 2004 10:45 AM
  • User-303179724 posted
    I think this sample could help you! http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/winforms/Samples/GDIPlus/Images/Images.src&file=CS\GDIPImages.cs&font=3
    Wednesday, March 17, 2004 9:18 AM
  • User-1100543698 posted

     Update - June 2009

     I had to solve the problem of uploading an image from a URI to memory, and, after considerable searching, I found this solution (LoadImageFromUrl, in this thread, by sac87126). It surprising difficult, but it does work. So thankyou, sac, from 2009!


    Probably WCF offers a better way to do it.  http://stackoverflow.com/questions/104797/wcf-service-for-receiving-image.



    Tuesday, June 9, 2009 9:06 PM
  • User2020432662 posted

    This worked for me (after a little formatting)...

    VB version of code from sac87126

        Private Sub LoadImageFromURL(URL As String, ByRef Img As Drawing.Bitmap)
            Const BYTESTOREAD As Integer = 10000
            Dim myRequest As WebRequest = WebRequest.Create(URL)
            Dim myResponse As WebResponse = myRequest.GetResponse()
            Dim ReceiveStream As Stream = myResponse.GetResponseStream()
            Dim br As New BinaryReader(ReceiveStream)
            Dim memstream As New MemoryStream()
            Dim bytebuffer As Byte() = New Byte(BYTESTOREAD - 1) {}
            Dim BytesRead As Integer = br.Read(bytebuffer, 0, BYTESTOREAD)
            While BytesRead > 0
                memstream.Write(bytebuffer, 0, BytesRead)
                BytesRead = br.Read(bytebuffer, 0, BYTESTOREAD)
            End While
            Img = New Drawing.Bitmap(memstream)
        End Sub

    Friday, August 12, 2011 1:48 PM
  • User1792244448 posted

    Is this for a publicly accessible website? Allowing arbitrary URLs in the querystring is a vunerability unless you have some way of limiting them to 'safe zones' or you sign the request url with HMAC256. 

    I recently implemented this feature in the ImageResizing.net library, as the RemoteReader plugin.

    The code is open-source, so feel free to download it and take a look at the souce code to see how to implement the request signing and security.

    Also, I'd suggest using url-safe base64 encoding for the remote URL to prevent issues with IIS's request parsing.

    Sunday, August 14, 2011 9:02 AM