Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grab an image from the internet and save it to the file system. 1

Status
Not open for further replies.
Jan 11, 2007
51
0
0
This is a pretty simple question I guess. I just want to save an image from a web url to the file system of our server.

The purpose is, we have a image processing tool that is a web URL that takes in a bunch of web paramaters and outputs a new JPG image and I want to take the outputed image and save it to a folder as a thumnail. Here is an example Image URL:

lily-allen-threatens-kick-stab-peaches.jpg


Thank you!!
 
use the Image object:
1. pass the url to the Image.
2. adjust the image for thumbanil.
3. save the adjusted image on your server.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you very much for your response.

Can you please elaborate on how I woudl save the image to the server (step 3). For the sake of clarity, say I want to directly save the URL reference above to a file on my server.

1) Do I need to create a new bitmap object or image object?

2) Is there some way to save the file without using an image object? Mabye using the webclient class or reading the bytes of the file?
 
I assume you could, but the Image object is very simple to use. there are many examples of this on the web.

why would you not want to use the image object?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Code:
public class RemoteImage
{
  public RemoteImage() { }

  public SaveRemoteImage(string remoteLocation)
  {
    Image remoteImage = new Image();
    remoteImage.FromFile(remoteLocation);

    //save full size image
    remoteImage.Save("path to save file");

    //save thumbnail
    remoteImage.GetThumbnailImage(100, 100, ThumbnailCallback, null).Save("path to save thumbnail");
  }

  priavte bool ThumbnailCallback()
  {
    return false;
  }
}
Note: this has not be tested. you will need to tweak the save locations.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 

Hey thanks for the code sample. I didn't realize it was so easy. OK so I see there are two methods for saving an image...

Code:
 // build the url for the image processor that creates the thumb nail image
                string thumbImgUrl = Core.Config.Root + "/shared/WiseImage.ashx?Image=" + fileName + "&width=75&cropHeight=30";

                System.Drawing.Image thumbImb = System.Drawing.Image.FromStream(thumbImgUrl);
                thumbImb.Save("th_"+fileName);

When i try to save I get this error:

Compiler Error Message: CS1502: The best overloaded method match for 'System.Drawing.Image.FromStream(System.IO.Stream)' has some invalid arguments


I think it needs a stream.... but I'm passing a URL? Do i need to convert the URL to a stream, then pass that into an image object?

Thanks.
 
By the way - I am confused about what a "Stream" is. When a function asks for a stream, does it want a byte array, or some other kind of data type? Does it want me to convert it to a stream type? Is a Stream a datatype, an object, or what?
 
stream is an abstract object. there are a variety of concrete objects that inherit from stream. StreamWriter, StreamReader, and few others I think. a string cannot be converted to a stream. you will need a streamreader, pass the url to the reader and then pass the stream to the image object.

I think your ashx is trying to do too many things at once.
You should have a routine to get a file from the remote location and save that file and thumbnail to the local box. Then you need another routine to get the file/thumbnail from the local machine.

if you want 1 ashx to do it all you need to pass the local path, the remote path, and the demensions of the thumbnail. then check if the local file exists, if not get the remote file, save to local location and then get the local file.


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
OK - well, ashx aside - how would I handle simply pull a web image from a URL into an image object?

Take this code sample for example:

Code:
string thumbImgUrl = "[URL unfurl="true"]http://eur.news1.yimg.com/eur.yimg.com/xp/wenn/20060928/12/3390327964-lily-allen-threatens-kick-stab-peaches.jpg";[/URL]

                System.Drawing.Image thumbImb = System.Drawing.Image.FromStream(thumbImgUrl);
                thumbImb.Save("th_"+fileName);

thumbImgUrl is just some image on the web. Obviously something needs to be done before it can be read into an image object. How do I read that JPG into either a BITMAP or IMAGE object?

Thanks again for your help!
 
Code:
using(SreamReader reader = new StreamReader(url))
{
   Image image = new Image();
   image.FromStream(reader.BaseStream);
   image.Save("full path for regular image");
   image.GetThumbnailImage(100, 100, [abort], null).Save("full thumbnail path");
}
the using statement will automatically close/dispose of the reader object.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I dont think that the streamreader object is what I need to use? I tried to impliment it to pull the image from a URL and it gives me the error that URI formats are not supported:



Code:
string thumbImgUrl = "[URL unfurl="true"]http://eur.news1.yimg.com/eur.yimg.com/xp/wenn/20060928/12/3390327964-lily-allen-threatens-kick-stab-peaches.jpg";[/URL]

                using(System.IO.StreamReader reader = new System.IO.StreamReader(thumbImgUrl))
                {
                   System.Drawing.Image image = System.Drawing.Image.FromStream(reader.BaseStream);
                   image.Save("th_"+fileName);
                }


Maybe I need to rethink how I'm doing this....
 
There's a much easier method you can use...
Code:
Dim s As New System.Net.WebClient
s.DownloadFile("[URL unfurl="true"]http://www.tek-tips.com/images/logo.gif",[/URL] "c:\mylogo.gif")



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Once Again! ca8msm saves the day. Thanks so much!!!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top