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.
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
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.
hardeepsingh
Member
80 Points
16 Posts
can I load image from URL
Mar 15, 2004 05:51 PM|LINK
Carsinigin
Member
40 Points
8 Posts
Re: can I load image from URL
Mar 15, 2004 08:08 PM|LINK
hardeepsingh
Member
80 Points
16 Posts
Re: can I load image from URL
Mar 16, 2004 12:23 PM|LINK
sac87126
Member
190 Points
41 Posts
Re: can I load image from URL
Mar 16, 2004 02:45 PM|LINK
rafv
Member
190 Points
38 Posts
Re: can I load image from URL
Mar 17, 2004 01:18 PM|LINK
Javaman59
Member
2 Points
1 Post
Re: can I load image from URL
Jun 10, 2009 01:06 AM|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.
afetchko
Member
13 Points
13 Posts
Re: can I load image from URL
Aug 12, 2011 05:48 PM|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
nathanael.jo...
Member
137 Points
37 Posts
Re: can I load image from URL
Aug 14, 2011 01:02 PM|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.