473,626 Members | 3,237 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is is possible to programmaticaly save a graphic forma given URL?

Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
want to save that graphic to a local file. The approach below gets the
response, but I can't quite figure out how to save it to a file. In this
instance, a file gets created but it only contains the text
"System.Io.Stre amReader" I have also tried writer.write(re ader.ReadToEnd) ,
but the file still displays nothing.
Dim URL As String = GraphicURL

Dim request As WebRequest = WebRequest.Crea te(URL)

Dim response As WebResponse = request.GetResp onse()

Dim reader As StreamReader = New

StreamReader(re sponse.GetRespo nseStream())

Dim writer As StreamWriter = New StreamWriter("C :\temp\" & GraphicKey &
".jpg")

writer.Write(re ader)

writer.Close()

reader.Close()

response.Close( )
Stacey Levine
Nov 21 '05 #1
13 1530
Yes, you are writing the reader, rather than writing what the reader is
reading :) Try writing reader.readtoen d (or something).
"Stacey Levine" <st****@newsgro up.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
want to save that graphic to a local file. The approach below gets the
response, but I can't quite figure out how to save it to a file. In this
instance, a file gets created but it only contains the text
"System.Io.Stre amReader" I have also tried writer.write(re ader.ReadToEnd) ,
but the file still displays nothing.
Dim URL As String = GraphicURL

Dim request As WebRequest = WebRequest.Crea te(URL)

Dim response As WebResponse = request.GetResp onse()

Dim reader As StreamReader = New

StreamReader(re sponse.GetRespo nseStream())

Dim writer As StreamWriter = New StreamWriter("C :\temp\" & GraphicKey &
".jpg")

writer.Write(re ader)

writer.Close()

reader.Close()

response.Close( )
Stacey Levine

Nov 21 '05 #2
I did try writer.write(re ader.ReadToEnd) , and the file has more data.. it
is all hex.. but it still won't open as a picture. I am not sure if I missed
writing something.
"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:da******** ***********@new s.demon.co.uk.. .
Yes, you are writing the reader, rather than writing what the reader is
reading :) Try writing reader.readtoen d (or something).
"Stacey Levine" <st****@newsgro up.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
want to save that graphic to a local file. The approach below gets the
response, but I can't quite figure out how to save it to a file. In this
instance, a file gets created but it only contains the text
"System.Io.Stre amReader" I have also tried
writer.write(re ader.ReadToEnd) , but the file still displays nothing.
Dim URL As String = GraphicURL

Dim request As WebRequest = WebRequest.Crea te(URL)

Dim response As WebResponse = request.GetResp onse()

Dim reader As StreamReader = New

StreamReader(re sponse.GetRespo nseStream())

Dim writer As StreamWriter = New StreamWriter("C :\temp\" & GraphicKey &
".jpg")

writer.Write(re ader)

writer.Close()

reader.Close()

response.Close( )
Stacey Levine


Nov 21 '05 #3
One other wierd bit.. when I save the file using the reader.ReadToEn d ..
the file has 1302 bytes.
If I right click the image and save it, it has 2257 bytes.

very very wierd

"Stacey Levine" <st****@newsgro up.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
want to save that graphic to a local file. The approach below gets the
response, but I can't quite figure out how to save it to a file. In this
instance, a file gets created but it only contains the text
"System.Io.Stre amReader" I have also tried writer.write(re ader.ReadToEnd) ,
but the file still displays nothing.
Dim URL As String = GraphicURL

Dim request As WebRequest = WebRequest.Crea te(URL)

Dim response As WebResponse = request.GetResp onse()

Dim reader As StreamReader = New

StreamReader(re sponse.GetRespo nseStream())

Dim writer As StreamWriter = New StreamWriter("C :\temp\" & GraphicKey &
".jpg")

writer.Write(re ader)

writer.Close()

reader.Close()

response.Close( )
Stacey Levine

Nov 21 '05 #4
Try BinaryWriter/BinaryReader:

Try
Dim Request As HttpWebRequest =
HttpWebRequest. Create(http://i3.microsoft.com/h/en-us/i/SpeedPC_2_10.jpg)
Request.Timeout = 5000
Dim Response As HttpWebResponse = Request.GetResp onse()
Dim myReader As BinaryReader
myReader = New BinaryReader(Re sponse.GetRespo nseStream())
Dim Result() As Byte = myReader.ReadBy tes(Response.Co ntentLength)
Dim myFileStream As New FileStream("c:\ SpeedPC_2_10.jp g",
FileMode.Create New)
Dim myWriter As New BinaryWriter(my FileStream)
myWriter.Write( Result)
myWriter.Close( )
Catch e As Exception
MessageBox.Show (e.Message, "Error Occurred")
End Try
"Stacey Levine" <st****@newsgro up.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
want to save that graphic to a local file. The approach below gets the
response, but I can't quite figure out how to save it to a file. In this
instance, a file gets created but it only contains the text
"System.Io.Stre amReader" I have also tried writer.write(re ader.ReadToEnd) ,
but the file still displays nothing.
Dim URL As String = GraphicURL

Dim request As WebRequest = WebRequest.Crea te(URL)

Dim response As WebResponse = request.GetResp onse()

Dim reader As StreamReader = New

StreamReader(re sponse.GetRespo nseStream())

Dim writer As StreamWriter = New StreamWriter("C :\temp\" & GraphicKey &
".jpg")

writer.Write(re ader)

writer.Close()

reader.Close()

response.Close( )
Stacey Levine

Nov 21 '05 #5
You're using a regular StreamReader and StreamWriter, so it's using Default
Text encodings. You need to use BinaryReader/BinaryWriter for Binary
graphic data. See other post.

"Stacey Levine" <st****@newsgro up.nospam> wrote in message
news:ev******** ******@TK2MSFTN GP09.phx.gbl...
One other wierd bit.. when I save the file using the reader.ReadToEn d ..
the file has 1302 bytes.
If I right click the image and save it, it has 2257 bytes.

very very wierd

"Stacey Levine" <st****@newsgro up.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
want to save that graphic to a local file. The approach below gets the
response, but I can't quite figure out how to save it to a file. In this
instance, a file gets created but it only contains the text
"System.Io.Stre amReader" I have also tried
writer.write(re ader.ReadToEnd) , but the file still displays nothing.
Dim URL As String = GraphicURL

Dim request As WebRequest = WebRequest.Crea te(URL)

Dim response As WebResponse = request.GetResp onse()

Dim reader As StreamReader = New

StreamReader(re sponse.GetRespo nseStream())

Dim writer As StreamWriter = New StreamWriter("C :\temp\" & GraphicKey &
".jpg")

writer.Write(re ader)

writer.Close()

reader.Close()

response.Close( )
Stacey Levine


Nov 21 '05 #6
That was it. Thanks. I was banging my head against the wall.

Stacey
"Michael C#" <ho***@boutdat. com> wrote in message
news:%2******** *******@TK2MSFT NGP15.phx.gbl.. .
Try BinaryWriter/BinaryReader:

Try
Dim Request As HttpWebRequest =
HttpWebRequest. Create(http://i3.microsoft.com/h/en-us/i/SpeedPC_2_10.jpg)
Request.Timeout = 5000
Dim Response As HttpWebResponse = Request.GetResp onse()
Dim myReader As BinaryReader
myReader = New BinaryReader(Re sponse.GetRespo nseStream())
Dim Result() As Byte = myReader.ReadBy tes(Response.Co ntentLength)
Dim myFileStream As New FileStream("c:\ SpeedPC_2_10.jp g",
FileMode.Create New)
Dim myWriter As New BinaryWriter(my FileStream)
myWriter.Write( Result)
myWriter.Close( )
Catch e As Exception
MessageBox.Show (e.Message, "Error Occurred")
End Try
"Stacey Levine" <st****@newsgro up.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
want to save that graphic to a local file. The approach below gets the
response, but I can't quite figure out how to save it to a file. In this
instance, a file gets created but it only contains the text
"System.Io.Stre amReader" I have also tried
writer.write(re ader.ReadToEnd) , but the file still displays nothing.
Dim URL As String = GraphicURL

Dim request As WebRequest = WebRequest.Crea te(URL)

Dim response As WebResponse = request.GetResp onse()

Dim reader As StreamReader = New

StreamReader(re sponse.GetRespo nseStream())

Dim writer As StreamWriter = New StreamWriter("C :\temp\" & GraphicKey &
".jpg")

writer.Write(re ader)

writer.Close()

reader.Close()

response.Close( )
Stacey Levine


Nov 21 '05 #7
"Stacey Levine" <st****@newsgro up.nospam> schrieb:
Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
want to save that graphic to a local file. The approach below gets the
response, but I can't quite figure out how to save it to a file.


Check out 'WebClient.Down loadFile'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8
HTH

"Stacey Levine" <st****@newsgro up.nospam> wrote in message
news:Of******** *****@TK2MSFTNG P15.phx.gbl...
That was it. Thanks. I was banging my head against the wall.

Stacey
"Michael C#" <ho***@boutdat. com> wrote in message
news:%2******** *******@TK2MSFT NGP15.phx.gbl.. .
Try BinaryWriter/BinaryReader:

Try
Dim Request As HttpWebRequest =
HttpWebRequest. Create(http://i3.microsoft.com/h/en-us/i/SpeedPC_2_10.jpg)
Request.Timeout = 5000
Dim Response As HttpWebResponse = Request.GetResp onse()
Dim myReader As BinaryReader
myReader = New BinaryReader(Re sponse.GetRespo nseStream())
Dim Result() As Byte = myReader.ReadBy tes(Response.Co ntentLength)
Dim myFileStream As New FileStream("c:\ SpeedPC_2_10.jp g",
FileMode.Create New)
Dim myWriter As New BinaryWriter(my FileStream)
myWriter.Write( Result)
myWriter.Close( )
Catch e As Exception
MessageBox.Show (e.Message, "Error Occurred")
End Try
"Stacey Levine" <st****@newsgro up.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic,
I want to save that graphic to a local file. The approach below gets the
response, but I can't quite figure out how to save it to a file. In this
instance, a file gets created but it only contains the text
"System.Io.Stre amReader" I have also tried
writer.write(re ader.ReadToEnd) , but the file still displays nothing.
Dim URL As String = GraphicURL

Dim request As WebRequest = WebRequest.Crea te(URL)

Dim response As WebResponse = request.GetResp onse()

Dim reader As StreamReader = New

StreamReader(re sponse.GetRespo nseStream())

Dim writer As StreamWriter = New StreamWriter("C :\temp\" & GraphicKey &
".jpg")

writer.Write(re ader)

writer.Close()

reader.Close()

response.Close( )
Stacey Levine



Nov 21 '05 #9
Stacey,
In addition to the other comments:

Don't use StreamReader & StreamWriter on binary files (such as graphic
files), as StreamReader & StreamWriter are used with Text Files.

If you simply want to copy the file, I would not even bother with
BinaryReader & BinaryWriter, as StreamReader & StreamWriter and BinaryReader
& BinaryWriter are used to parse "formatted" files, you simply want to copy
the bytes.

The "easiest" way is to use WebClient.Downl oadFile as Herfried shows.
Alternatively you can use a loop, something like:

Dim URL As String = GraphicURL
Dim request As WebRequest = WebRequest.Crea te(URL)
Dim response As WebResponse = request.GetResp onse()

Dim input As Stream = response.GetRes ponseStream()
Dim output As New FileStream("C:\ temp\" & graphicKey & ".jpg",
FileMode.Create )

Dim count As Integer = 32 * 1024
Dim buffer(count - 1) As Byte

Do
count = input.Read(buff er, 0, count)
If count = 0 Then Exit Do
output.Write(bu ffer, 0, count)
Loop

input.Close()
output.Close()
response.Close( )

Hope this helps
Jay

"Stacey Levine" <st****@newsgro up.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
| Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
| want to save that graphic to a local file. The approach below gets the
| response, but I can't quite figure out how to save it to a file. In this
| instance, a file gets created but it only contains the text
| "System.Io.Stre amReader" I have also tried writer.write(re ader.ReadToEnd) ,
| but the file still displays nothing.
|
|
| Dim URL As String = GraphicURL
|
| Dim request As WebRequest = WebRequest.Crea te(URL)
|
| Dim response As WebResponse = request.GetResp onse()
|
| Dim reader As StreamReader = New
|
| StreamReader(re sponse.GetRespo nseStream())
|
| Dim writer As StreamWriter = New StreamWriter("C :\temp\" & GraphicKey &
| ".jpg")
|
| writer.Write(re ader)
|
| writer.Close()
|
| reader.Close()
|
| response.Close( )
|
|
| Stacey Levine
|
|
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2102
by: Maggie | last post by:
Hi, I am in a process of creating a text version of my website and was wondering if it was possible to accomplish this without actually making the same page twice using CSS. I have a stylesheet for printing only, which has no graphics. Would it be possible to use the same page to display when the users clicks on the text version link?
20
2684
by: Joe exCSSive | last post by:
Hi, folks: I'm back...and now I'm really stuck !! I have a graphic ( http://www.sundialontario.com/images/makeitatable.jpg ) that I would like to convert to CSS and I'm not sure how to do this.
3
2607
by: glevik | last post by:
Hello, Anyone that can think of a way to programmaticaly determine the word on an HTML page that the user clicked on will be my hero for life. Leo
2
3878
by: Dave Bootsma | last post by:
Is it possible to programatically save a certain image from a certain web page? I want to automatically get a specific graphic from a specific web page programatically so I can automate the task. I know I can NAVIGATE to the page with the MS web browser control, but at that point I am stumped as to wether I can even save the page... TIA
8
1328
by: Vincent Finn | last post by:
I know the answer to the question above is yes but my reason for asking is that it doesn't seem to work properly I have a manged C++ WinForms project In it I have a form A, I want to derive a form B from it Firstly the menu that the MSDN says should be available from the 'Add' menu to 'Add an Inherited Form' isn't there This seems to only be available for C# and VB.Net
2
1929
by: Erik Juhlin | last post by:
I want to edit a web.confgi file Programmaticaly. I can´t use the System.Configuration.ConfigurationSettings.AppSettings.Add becuase the web.config file i want to edit does belongs to another web site. I have no problems open the web.config-file as stream or XMLDocument.Load, but as sone as I try to save web.config it says "cannot save.. because the file is used by another process" or something like that. I guess that the IIS is...
4
1141
by: Marc | last post by:
Hi, In order to save user preferences I need to grab the name and position of all buttons on a form, which i will then save to a text file. Anyone know how I can easily get this information? Thanks Marc
1
4584
by: Sylvie | last post by:
I am Developing a web application and reading connection strings from Web.Config file, How can I write appSettings keys programmaticaly via a web page ? Thanks <?xml version="1.0"?> <configuration>
3
4660
by: dbuchanan | last post by:
Here is part of the web.config - it is given to me like this. ======================= <configuration> <appSettings/> <connectionStrings> <add name="MyConnectionName" connectionString="Data Source=MyServerName;Initial Catalog=MyDatabaseName;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> <system.web>
0
8262
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8701
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6122
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.