Solved

How do I download images using WebClient?

Posted on 2008-10-15
1
454 Views
Last Modified: 2013-12-20
I used the following code to iterate an array of URI based image locations to download the image files to my local drive so I could use them in System.Net.Mail as embedded images. The files on my local drive aren't the image files, they're just small binary stubs. I cribbed this code from a bunch of sources without really understanding in detail what I'm doing. The IMGS array contains (0) the URI , (1) the generated CID for embedding and (3) the local file path/name. Any help is greatly appreciated:

            WebClient wc = new WebClient();
            for (int i=0; i<=ct; i++)
            {
                byte[] image = wc.DownloadData(imgs[i,0]);
                string LocalFile = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), imgs[i,1].ToString().Substring(4,imgs[i,1].ToString().Length-4));
                BinaryWriter wr = new BinaryWriter(File.Open(LocalFile, FileMode.Create));
                wr.Write(image.Length);
                wr.Close();
                imgs[i, 2] = LocalFile;
            }
0
Comment
Question by:rschaeferhig
1 Comment
 

Accepted Solution

by:
rschaeferhig earned 0 total points
ID: 22721858
Line
wr.Write(image.Length);
changed to
wr.Write(image, 0, image.Length)
Fixed the problem.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question