none
webpage to image RRS feed

  • Question

  • I want to convert a full web page by typing the link to a picture and saving it to the picturebox  without WebBrowser 

    Friday, November 10, 2017 12:39 AM

Answers

  •  

    If you want to test is a string is null or is all white spaces then first test it for null.  If it is not null then trim the white space and test again. If the length is now greater than 0 then it is not null and has something in addition to the white space. There are many other ways to do the same thing.   There's no point in trying to do it all in a single If statement.

    .IsNullOrWhiteSpace takes care of it but I suspect he's not using it correctly.

    The reason that I use it is so that:

    "        "

    ...will be considered null even thought it's actually not.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by ahmeddc Friday, November 10, 2017 2:20 AM
    Friday, November 10, 2017 1:46 AM
  •  Perhaps the external library (link below) will be of some help.  It is free but,  it is also from 2006 so,  it may work for some pages but,  may not work for others.  I have not tested it myself.  It is in C# but,  you should be able to add a reference to the dll library in your vb.net application.  You could also use a C# to VB.Net code converter such as the free online Telerik Code Converter to try converting it to VB.Net if you want.

    Print HTML in C# with or without the web browser control and the print dialog

     I am not sure if it will supply a direct Bitmap to assign to the PictureBox but,  if not,  maybe you can print it to a file and then load it into the PictureBox.

     Other than that,  all i could suggest is using a WebBrowser which is either not visible or not added to the form's Control collection so the user does not see it.


    If you say it can`t be done then i`ll try it

    • Marked as answer by ahmeddc Friday, November 10, 2017 2:20 AM
    Friday, November 10, 2017 1:57 AM

All replies

  • I want to convert a full web page by typing the link to a picture and saving it to the picturebox  without WebBrowser 

    This is an excerpt but hopefully you'll see how you can use it:

                    Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(sb.ToString)
    
                    Using request As System.Net.WebResponse = req.GetResponse
                        Using stream As System.IO.Stream = request.GetResponseStream
                            _image = New Bitmap(System.Drawing.Image.FromStream(stream))
                        End Using
                    End Using

    The "sb.ToString" is the URL of the image online. In what I was using there, "sb" was a StringBuilder. The essense is this: It "reads" the stream from online and with that, uses Image.FromStream.

    Give it a try. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 10, 2017 12:44 AM
  • That might be confusing -- this one is a little more obvious:

        Private Function GetImageFromURL(ByVal url As String) As Bitmap
    
            Dim retVal As Bitmap = Nothing
    
            Try
                If Not String.IsNullOrWhiteSpace(url) Then
                    Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(url)
    
                    Using request As System.Net.WebResponse = req.GetResponse
                        Using stream As System.IO.Stream = request.GetResponseStream
                            retVal = New Bitmap(System.Drawing.Image.FromStream(stream))
                        End Using
                    End Using
                End If
    
            Catch ex As Exception
                ' Nothing to do -- the return
                ' value defaults to null. Be
                ' sure to test that it's valid
                ' before using it!
            End Try
    
            Return retVal
    
        End Function

    When you use that function, be sure to test that the return value IsNot Nothing.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 10, 2017 12:52 AM
  •  If it is actually a link to an existing Image on the internet that you want to put in a PictureBox,  then just set the PictureBox.ImageLocation Property to the url of the image.

     This is the code i used for the example in the animated image below...

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            PictureBox1.ImageLocation = TextBox1.Text
        End Sub
    End Class
    
     

     


    If you say it can`t be done then i`ll try it

    Friday, November 10, 2017 1:00 AM
  • error

    Not String.IsNullOrWhiteSpace(url)
    Error 1 'IsNullOrWhiteSpace' is not a member of 'String'.
    Friday, November 10, 2017 1:04 AM
  • I want to convert a full web page by typing the link to a picture and saving it to the picturebox  without WebBrowser 

    This is an excerpt but hopefully you'll see how you can use it:

                    Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(sb.ToString)
    
                    Using request As System.Net.WebResponse = req.GetResponse
                        Using stream As System.IO.Stream = request.GetResponseStream
                            _image = New Bitmap(System.Drawing.Image.FromStream(stream))
                        End Using
                    End Using

    The "sb.ToString" is the URL of the image online. In what I was using there, "sb" was a StringBuilder. The essense is this: It "reads" the stream from online and with that, uses Image.FromStream.

    Give it a try. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Error 1 '_image' is not declared. It may be inaccessible due to its protection level.
    Friday, November 10, 2017 1:07 AM
  • error

    Not String.IsNullOrWhiteSpace(url)
    Error 1 'IsNullOrWhiteSpace' is not a member of 'String'.

    You must be using an older version framework.

    Change that to .IsNullOrEmpty and that will work. That's old though.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 10, 2017 1:08 AM
  • Error 1 'IsNullOrWhiteSpace' is not a member of 'String'.

    According to the documentation it is a member.  Either it is misspelled in the code or you are using an obsolete version of the framework.
    https://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace(v=vs.100).aspx

    You can replace it with specific testing for exactly the content you require, eg trimmed length <>0.

    Friday, November 10, 2017 1:10 AM
  • Error 1 '_image' is not declared. It may be inaccessible due to its protection level.

    It's not declared.

    Ignore that post and look at the other one.

    Please don't use the code if you don't understand it though. IR posted a valid reply but if you want to have access to the image itself, that's what I posted. The return value will be a valid bitmap.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 10, 2017 1:13 AM
  • I want to convert a full web page by typing the link to a picture and saving it to the picturebox  without WebBrowser 

    There's another way that you can use and you might want to if the connection is slow or if the file is large:

    PictureBox.LoadAsync

    That will load it asynchronously and you can use two events to know what the progress is and when it's completed.

    Be sure to read that though - you'll need to set it up to do that but it works well once you do.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 10, 2017 1:19 AM
  • That might be confusing -- this one is a little more obvious:

        Private Function GetImageFromURL(ByVal url As String) As Bitmap
    
            Dim retVal As Bitmap = Nothing
    
            Try
                If Not String.IsNullOrWhiteSpace(url) Then
                    Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(url)
    
                    Using request As System.Net.WebResponse = req.GetResponse
                        Using stream As System.IO.Stream = request.GetResponseStream
                            retVal = New Bitmap(System.Drawing.Image.FromStream(stream))
                        End Using
                    End Using
                End If
    
            Catch ex As Exception
                ' Nothing to do -- the return
                ' value defaults to null. Be
                ' sure to test that it's valid
                ' before using it!
            End Try
    
            Return retVal
    
        End Function

    When you use that function, be sure to test that the return value IsNot Nothing.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    not work 

    full code

        Private Function GetImageFromURL(ByVal url As String) As Bitmap
    
            Dim retVal As Bitmap = Nothing
    
            Try
                If Not String.IsNullOrEmpty(url) Then
                    Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(url)
    
                    Using request As System.Net.WebResponse = req.GetResponse
                        Using stream As System.IO.Stream = request.GetResponseStream
                            retVal = New Bitmap(System.Drawing.Image.FromStream(stream))
                        End Using
                    End Using
                End If
    
            Catch ex As Exception
                ' Nothing to do -- the return
                ' value defaults to null. Be
                ' sure to test that it's valid
                ' before using it!
            End Try
    
            Return retVal
    
        End Function
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            PictureBox1.BackgroundImage = GetImageFromURL("https://social.msdn.microsoft.com/Forums/vstudio/en-US/b8240681-fdb5-4572-bf2c-2c8787e6383d/webpage-to-image?forum=vbgeneral")
        End Sub

    Friday, November 10, 2017 1:28 AM
  •  
     Public Shared Function IsNullOrWhiteSpace(
     ByVal value As String
    ) As Boolean
            Return String.IsNullOrEmpty(value) OrElse value.Trim().Length = 0
        End Function

    It does not work either
    Friday, November 10, 2017 1:33 AM
  • ...not work

    I don't think you understand: That's not the URL to an image.

    Try it with this as the URL:

    https://www.kingsford.com/wp-content/uploads/2014/11/kfd-hoetosteak-Steak_4_0323-1024x621.jpg

    I haven't tried it but one should work. That's how the PictureBox methods will work also; it assumes that there's an image at the other end. ;-)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 10, 2017 1:33 AM
  •  
     Public Shared Function IsNullOrWhiteSpace(
     ByVal value As String
    ) As Boolean
            Return String.IsNullOrEmpty(value) OrElse value.Trim().Length = 0
        End Function

    It does not work either

    You're confused.

    Try the code from IR or use .LoadAsync ... make sure that the URL is the URL of an image.

    It's really not hard at all, but until you understand it, it probably seems to be.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 10, 2017 1:37 AM
  • ...not work

    I don't think you understand: That's not the URL to an image.

    Try it with this as the URL:

    https://www.kingsford.com/wp-content/uploads/2014/11/kfd-hoetosteak-Steak_4_0323-1024x621.jpg

    I haven't tried it but one should work. That's how the PictureBox methods will work also; it assumes that there's an image at the other end. ;-)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    I want to convert a full web page content by typing the link to a picture and saving it to the picturebox  without WebBrowser 

       full web page

     to image


    • Edited by ahmeddc Friday, November 10, 2017 1:39 AM
    Friday, November 10, 2017 1:39 AM

  • I want to convert a full web page content by typing the link to a picture and saving it to the picturebox  without WebBrowser 

       full web page

     to image


    That's web-scraping which is frowned upon here (and may violate the terms of the website which in turn violates the terms of this forum).

    That aside though, what if there are no images? What if there are many images?

    Reconsider what you're attempting..


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 10, 2017 1:41 AM
  •  
     Public Shared Function IsNullOrWhiteSpace(
     ByVal value As String
    ) As Boolean
            Return String.IsNullOrEmpty(value) OrElse value.Trim().Length = 0
        End Function

    If you want to test is a string is null or is all white spaces then first test it for null.  If it is not null then trim the white space and test again. If the length is now greater than 0 then it is not null and has something in addition to the white space. There are many other ways to do the same thing.   There's no point in trying to do it all in a single If statement.

    Friday, November 10, 2017 1:43 AM
  •  

    If you want to test is a string is null or is all white spaces then first test it for null.  If it is not null then trim the white space and test again. If the length is now greater than 0 then it is not null and has something in addition to the white space. There are many other ways to do the same thing.   There's no point in trying to do it all in a single If statement.

    .IsNullOrWhiteSpace takes care of it but I suspect he's not using it correctly.

    The reason that I use it is so that:

    "        "

    ...will be considered null even thought it's actually not.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by ahmeddc Friday, November 10, 2017 2:20 AM
    Friday, November 10, 2017 1:46 AM

  • I want to convert a full web page content by typing the link to a picture and saving it to the picturebox  without WebBrowser 

       full web page

     to image


    That's web-scraping which is frowned upon here (and may violate the terms of the website which in turn violates the terms of this forum).

    That aside though, what if there are no images? What if there are many images?

    Reconsider what you're attempting..


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    I do not infringe on the rights of any site
    I want to like the capture method of the screen of the disc top and convert it to a picture

    But instead capture the screen of the disc capture screen

    Also like moving the page to print save as a picture

    Friday, November 10, 2017 1:51 AM

  • I do not infringe on the rights of any site
    I want to like the capture method of the screen of the disc top and convert it to a picture

    But instead capture the screen of the disc capture screen

    Also like moving the page to print save as a picture

    I wasn't accusing you of anything; don't take it that way.

    I don't understand what you want but hopefully others here can help you sort through it.

    I'm taking off for the night. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 10, 2017 1:55 AM
  •  Perhaps the external library (link below) will be of some help.  It is free but,  it is also from 2006 so,  it may work for some pages but,  may not work for others.  I have not tested it myself.  It is in C# but,  you should be able to add a reference to the dll library in your vb.net application.  You could also use a C# to VB.Net code converter such as the free online Telerik Code Converter to try converting it to VB.Net if you want.

    Print HTML in C# with or without the web browser control and the print dialog

     I am not sure if it will supply a direct Bitmap to assign to the PictureBox but,  if not,  maybe you can print it to a file and then load it into the PictureBox.

     Other than that,  all i could suggest is using a WebBrowser which is either not visible or not added to the form's Control collection so the user does not see it.


    If you say it can`t be done then i`ll try it

    • Marked as answer by ahmeddc Friday, November 10, 2017 2:20 AM
    Friday, November 10, 2017 1:57 AM