-
Jan 13th, 2016, 06:59 AM
#1
Thread Starter
Addicted Member
[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?
-
Jan 13th, 2016, 07:13 AM
#2
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
-
Jan 13th, 2016, 07:30 AM
#3
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
-
Jan 13th, 2016, 08:34 AM
#4
Thread Starter
Addicted Member
Re: Creating a function to place images on any control
Originally Posted by jmcilhinney
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
-
Jan 13th, 2016, 09:48 AM
#5
Re: Creating a function to place images on any control
Originally Posted by Reapism
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
-
Jan 13th, 2016, 05:29 PM
#6
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|