Results 1 to 6 of 6

Thread: [RESOLVED] Creating a function to place images on any control

  1. #1

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Resolved [RESOLVED] Creating a function to place images on any control

    Code:
        Private Sub PlaceImage(ByVal image As String, ByVal layout As String, ByVal control As String)
            Dim MyWebClient As New System.Net.WebClient
            Dim ImageInBytes() As Byte = MyWebClient.DownloadData(image)
            Dim ImageStream As New IO.MemoryStream(ImageInBytes)
            Dim picture As New PictureBox
            picture = PictureBox1
    
            PictureBox1.Image = New System.Drawing.Bitmap(ImageStream)
    
            If layout = "sm" Or "small" Then
                PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
            End If
        End Sub
        Private Sub frmLauncher_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PlaceImage("http://admin.ecolosseum.net/Content/images/login-symbol.jpg", "sm", "PictureBox1")

    I would like to make a function to place images wherever i want easy. I'm having difficulty telling it what control to use. I have unfinished code above, any help?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Creating a function to place images on any control

    You can't use any control because only the PictureBox class has that Image property. Just declare that third parameter as type PictureBox and then pass in the control you want to display the Image

  3. #3
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Creating a function to place images on any control

    Another method to handle this would be this pass an image, graphics object and bounding rectangle to a method then manually draw the image with GDI. Doing that would allow you to draw the image on any control from the control's paint event handler.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  4. #4

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: Creating a function to place images on any control

    Quote Originally Posted by jmcilhinney View Post
    You can't use any control because only the PictureBox class has that Image property. Just declare that third parameter as type PictureBox and then pass in the control you want to display the Image
    Can you give an example?
    Code:
     Private Sub PlaceImage(ByVal image As String, ByVal layout As String, ByVal destination As PictureBox)
            Dim MyWebClient As New System.Net.WebClient
            Dim ImageInBytes() As Byte = MyWebClient.DownloadData(image)
            Dim ImageStream As New IO.MemoryStream(ImageInBytes)
            Dim picture As New PictureBox
    
            picUsername.Image = New System.Drawing.Bitmap(ImageStream)
    
            If layout = "sm" Then
                picUsername.BackgroundImageLayout = ImageLayout.Stretch
            End If
    Where would I make it return to the specific control

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Creating a function to place images on any control

    Quote Originally Posted by Reapism View Post
    Can you give an example?
    Code:
     Private Sub PlaceImage(ByVal image As String, ByVal layout As String, ByVal destination As PictureBox)
            Dim MyWebClient As New System.Net.WebClient
            Dim ImageInBytes() As Byte = MyWebClient.DownloadData(image)
            Dim ImageStream As New IO.MemoryStream(ImageInBytes)
            Dim picture As New PictureBox
    
            picUsername.Image = New System.Drawing.Bitmap(ImageStream)
    
            If layout = "sm" Then
                picUsername.BackgroundImageLayout = ImageLayout.Stretch
            End If
    Where would I make it return to the specific control
    The first thing I would do is make the download part its own function, then after that I think you would need to check the type of control that you passed to the sub, maybe something like?..
    Code:
    Public Class Form1
    
        Private Sub PlaceImage(ByVal url As String, ByVal layout As ImageLayout, ByVal sender As Control)
            Dim newImage = DownloadImage(url)
            If newImage IsNot Nothing Then
                If TypeOf sender Is PictureBox Then
                    Dim pb = DirectCast(sender, PictureBox)
                    pb.BackgroundImage = newImage
                    pb.BackgroundImageLayout = layout
                ElseIf TypeOf sender Is Label Then
                   ' labels don't have a back/ImageLayout, so just set the Image property.
                    DirectCast(sender, Label).Image = newImage
                ElseIf TypeOf sender Is Panel Then
                    Dim pnl = DirectCast(sender, Panel)
                    pnl.BackgroundImage = newImage
                    pnl.BackgroundImageLayout = layout
                    '
                    ' ElseIf TypeOf sender Is ??? Then bla bla bla...
                End If
            Else
                MessageBox.Show("Download failed!")
            End If
        End Sub
    
        Private Function DownloadImage(imageUrl As String) As Image
            Dim img As Image = Nothing
            Try
                Using w As New Net.WebClient
                    Using s As New IO.MemoryStream(w.DownloadData(imageUrl))
                        img = CType(Image.FromStream(s).Clone, Image)
                    End Using
                End Using
            Catch ex As Exception
            End Try
            Return img
        End Function
    
    End Class
    Last edited by Edgemeal; Jan 27th, 2016 at 04:05 PM. Reason: remove button code

  6. #6

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: [RESOLVED] Creating a function to place images on any control

    This absolutely worked. Well made. Much appreciated!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width