Page 1 of 1
Convert Image at URL to binary format
#1
Convert Image at URL to binary format
Posted 08 June 2008 - 07:12 AM
Hi there,
I am currently developing a small application briefly as follows : - I run an Http web server with a basic page that displays a jpg from my webcam that refreshes the image every 10 mins. I currently use the webcams displayed image URL for a facebook application I am developing that includes a submit button that submits various user selectable attributes for the image to an SQL database. Currently developing in ASP.NET with code behind in VB. I can submit the selected attributes for the image easily to the database through the submit button but I havent figured out a way yet to convert the image Url to a binary file (imgbyte ?) for submission to the database without using an upload file control. I do not want or need an upload control, just a basic conversion of the image at the URL to binary format for submission to the SQL database.
Does anyone have any ideas here how to convert the image at the url to a binary file or possibly even convert from a file with a fixed filename without using an ASP.NET upload control?
I am currently developing a small application briefly as follows : - I run an Http web server with a basic page that displays a jpg from my webcam that refreshes the image every 10 mins. I currently use the webcams displayed image URL for a facebook application I am developing that includes a submit button that submits various user selectable attributes for the image to an SQL database. Currently developing in ASP.NET with code behind in VB. I can submit the selected attributes for the image easily to the database through the submit button but I havent figured out a way yet to convert the image Url to a binary file (imgbyte ?) for submission to the database without using an upload file control. I do not want or need an upload control, just a basic conversion of the image at the URL to binary format for submission to the SQL database.
Does anyone have any ideas here how to convert the image at the url to a binary file or possibly even convert from a file with a fixed filename without using an ASP.NET upload control?
#3
Re: Convert Image at URL to binary format
Posted 08 June 2008 - 07:33 AM
Well, first off it's never a good idea to store images in a database, it is a complete waste of system resources. The images should be stored in a directory on your web server, and at most details about the image be inserted into the database.
If, however, you're insistant on storing the image in the database, you could check into using a MemoryStream for converting the image to a byte array, here's a basic example
Hope that helps
If, however, you're insistant on storing the image in the database, you could check into using a MemoryStream for converting the image to a byte array, here's a basic example
Public Sub ConvertImage(ByVal file As String)
Dim stream As New MemoryStream(file, FileMode.Open)
'Get the files information
Dim info As New FileInfo(file)
Dim temp As Long = info.Length
'Get the integer size of the image so we
'know how large of a Byte array we need
Dim length As Integer = Convert.ToInt32(temp)
'Create our byte array the size we ned
Dim pic As Byte() = New Byte(length - 1) {}
'Now read our image into the byte array
stream.Read(pic, 0, length)
End Function
Hope that helps
#4
Re: Convert Image at URL to binary format
Posted 08 June 2008 - 07:48 AM
Wow, fast response guys, much appreciated! With more thought on this I am beginning to question if I should store the images on the database as it is easy enough to store the image path and filename. Ill need to think more on this, the images themselves are fairly small, around 10k. My thinking was that the database might well be moved in the future and thought it might be smarter to simply use the database as storage for the images and simply back up and restore from an SQL .bak file. In the meantime I will look into the suggestions from above and post back with my results. Thank you.
#5
Re: Convert Image at URL to binary format
Posted 08 June 2008 - 12:30 PM
Thank you for the help guys, I now have this working through the file system. I am intrigued by this possible method 'System.Text.Encoding.ASCII.GetBytes()' as mentioned above as it reminds me of a possibly similar method in Java I have used before using URL objects and BufferedReader, InputSreamReader. Ill look into it more.
#6
Re: Convert Image at URL to binary format
Posted 09 June 2008 - 03:52 AM
Hello again guys,
As I have got the image conversion working fine from the file system I have now looked further into converting an image from a URL to binary. I have the following C# code working but would like some help in converting it over to VB.net as I dont work with C# 99% of the time. Many thanks.
As I have got the image conversion working fine from the file system I have now looked further into converting an image from a URL to binary. I have the following C# code working but would like some help in converting it over to VB.net as I dont work with C# 99% of the time. Many thanks.
HttpWebRequest request = WebRequest.Create("http://www.image.com/image.jpg") as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream stream = response.GetResponseStream();
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
BinaryReader br = new BinaryReader(stream);
bw.Write(br.ReadBytes((int)response.ContentLength));
ms.Position = 0;
Bitmap fred = Bitmap.FromStream(ms) as Bitmap;
fred.Save(@"C:\temp\wibble.jpg");
#7
Re: Convert Image at URL to binary format
Posted 10 June 2008 - 06:57 AM
Have you written this code yourself in C#?
I'm assuming that you haven't, and so I can't give you the solution directly.
Use these rules to convert your C# code into VB.NET
I think you've got yourself enough info to convert the code yourself.
Let me know if you're still having trouble with it.
I'm assuming that you haven't, and so I can't give you the solution directly.
Use these rules to convert your C# code into VB.NET
- MemoryStream ms = new MemoryStream(); becomes Dim ms as New MemoryStream()
- HttpWebResponse response = request.GetResponse() as HttpWebResponse; becomes Dim response As HttpWebResponse = request.GetResponse()
- Unlike C# statements, VB.NET statements do not end with a semicolon.
I think you've got yourself enough info to convert the code yourself.
Let me know if you're still having trouble with it.
Page 1 of 1

Reply




MultiQuote




|