VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Page 1 of 1
  • You cannot start a new topic
  • Reply Reply

Convert Image at URL to binary format Rate Topic: -----

#1 Deeko  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 29-November 05


Dream Kudos: 0

Share |

Convert Image at URL to binary format

Post icon  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?
Was This Post Helpful? 0
  • +
  • -


#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • Icon

Reputation: 137
  • View blog
  • Posts: 4,622
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

Re: Convert Image at URL to binary format

Posted 08 June 2008 - 07:32 AM

You will have to download the file at the URL (called URI now) and then use the System.Text.Encoding.ASCII.GetBytes() function.
Was This Post Helpful? 1
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • iHater.Init(this);
  • Icon

Reputation: 1364
  • View blog
  • Posts: 19,696
  • Joined: 26-July 07


Dream Kudos: 12925

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

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


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 :)
Was This Post Helpful? 1

#4 Deeko  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 29-November 05


Dream Kudos: 0

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.
Was This Post Helpful? 0
  • +
  • -

#5 Deeko  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 29-November 05


Dream Kudos: 0

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.
Was This Post Helpful? 0
  • +
  • -

#6 Deeko  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 29-November 05


Dream Kudos: 0

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.

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");

Was This Post Helpful? 1

#7 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • Icon

Reputation: 137
  • View blog
  • Posts: 4,622
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

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
  • 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.
:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1
  • You cannot start a new topic
  • Reply Reply


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users