Using WebRequest to Save URL as Thumbnail Image in VB.NET

I am trying to write a simple procedure where I pass in a url and it displays the content of a webresponse message as jpg. I found a solution, some kind in C #, and ported it to vb.net, however, when I run it, it triggers an Invalid Parameter when I try to instantiate an image. Can anyone take a look at the following code and let me know if I'm on the right track?

Sub SaveUrl(ByVal aUrl As String)
    Dim response As WebResponse
    Dim remoteStream As Stream
    Dim readStream As StreamReader
    Dim request As WebRequest = WebRequest.Create(aUrl)
    response = request.GetResponse
    remoteStream = response.GetResponseStream
    readStream = New StreamReader(remoteStream)
    Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(remoteStream)
    img.Save(aUrl & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
    response.Close()
    remoteStream.Close()
    readStream.Close()
End Sub

      

To clarify: Yes, I know I need BIG code to accomplish what I want to do to do / capture the screen capture of the URL (html, images, all markup, everything) and save it as a jpg thumbnail.

If you've used Google Chrome, you've seen the start page with thumbnails of all the sites you use frequently. Something like that.

Update: Good. I found commercial paid products to accomplish this, such as http://www.websitesscreenshot.com/Index.html , but not open source.

+1


source to share


7 replies


Just because the Image class has a method .FromStream()

doesn't mean that any stream is a valid image, and the html response from the web server will of course not be a valid image. Rendering a web page is not a "simple procedure". There's a lot going on there.



You can try using the WebBrowser control as it can do most of the work for you.

+3


source


Well, either:

My.Computer.Network.DownloadFile("http://example.com/file.jpeg", "local.jpeg")

      

Or, to create a thumbnail:



Using wc As New Net.WebClient()
    Dim img = Image.FromStream(wc.OpenRead("http://example.com/file"))
    img.GetThumbnailImage(32, 32, Function() False, Nothing).Save("c:\local.jpg")
End Using

      

... maybe add some error handling .; -)

+3


source


I just found a German snippet of code in ActiveVB that takes a screenshot from a website. Probably you can fix it? Unfortunately, the code is not only explained in German but also in VB6. However, the logic is essentially the same and shouldn't be difficult to port to .NET. Hope it helps.

+3


source


What are you trying to do?

Are you trying to convert your webpage to JPEG? It takes a little more code to do this, as your code is trying to do this to load an already existing image (like a gif, png or even another jpeg) and convert it to jpeg. You will need to render something to the HTML document, then you will need to capture the render image and then save it as a JPEG.

+2


source


It might be easier to use System.Net.WebClient

, I think you can remove everything but 2 or 3 lines of code. those.:WebClient.DownloadFile()

MSDN page

+1


source


I know you are asking VB, but there is C # code here that I am using in my project to grab local thumbnails from image urls (asynchronously). I've tried to highlight all of the project-specific stuff, so this makes sense as a separate example.

var wc = new WebClient();
wc.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
wc.DownloadDataCompleted += wc_DownloadDataCompleted;

wc.DownloadDataAsync(imageProvider.Image, "yourFilenameHere");

      

And here's the wc_DownloadDataCompleted event handler:

private void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
    if (e.Error == null && !e.Cancelled)
    {
        var filename = e.UserState.ToString();

        using (var ms = new MemoryStream(e.Result))
        {
            var bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = ms;
            bi.DecodePixelWidth = 80; // _maxThumbnailWidth;
            bi.EndInit();

            var encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bi));
            var filename = Path.Combine(_thumbFolder, filename);
            try
            {
                using (var fs = new FileStream(filename, FileMode.Create))
                {
                    encoder.Save(fs);
                }
            }
            catch { }
        }
    }
}

      

Edit

I think I should add that this was in a WPF application - hence the use of "BitmapImage" and "BitmapFrame" to decode the stream. So this may not help you. I'll stay here anyway.

0


source


Or you could just do this:

    Dim webclient As New Net.WebClient

    Dim FileName As String = "C:\Share\local.Gif"
    webclient.DownloadFile(URI, FileName)
    PictureBox1.Image = Image.FromFile(FileName)

      

0


source







All Articles