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.
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)
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 ); }
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
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.
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
Aug 14, 2011 09:02 AM|nathanael.jones@gmail.com|LINK
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.
None
0 Points
12 Posts
can I load image from URL
Mar 15, 2004 01:51 PM|hardeepsingh|LINK
None
0 Points
7 Posts
Re: can I load image from URL
Mar 15, 2004 04:08 PM|Carsinigin|LINK
System.Net.WebClient objWebClient = new System.Net.WebClient objWebClient.DownloadFile(URL, FILENAME) System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(FILENAME)
None
0 Points
12 Posts
Re: can I load image from URL
Mar 16, 2004 08:23 AM|hardeepsingh|LINK
None
0 Points
37 Posts
Re: can I load image from URL
Mar 16, 2004 10:45 AM|sac87126|LINK
None
0 Points
32 Posts
Re: can I load image from URL
Mar 17, 2004 09:18 AM|rafv|LINK
None
0 Points
1 Post
Re: can I load image from URL
Jun 09, 2009 09:06 PM|Javaman59|LINK
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.
Member
3 Points
22 Posts
Re: can I load image from URL
Aug 12, 2011 01:48 PM|afetchko|LINK
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
Member
70 Points
34 Posts
Re: can I load image from URL
Aug 14, 2011 09:02 AM|nathanael.jones@gmail.com|LINK
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.