This is a migrated thread and some comments may be shown as answers.

Add pictures to PictureBox dynamically

2 Answers 476 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 20 Oct 2016, 06:40 AM

Hi

I'm new to Teleric and am struggling to dynamically load a picture into a PictureBox. No matter what I try I get "Could not find part of the path". I found this function in an article on User Functions but it doesn't work: I get the same error message and "Invalid URL: the url is empty":

--------------------------------------------------------------------------------------------------------------------------------

    Private Sub PictureBoxRecipePicture_ItemDataBinding(sender As Object, e As EventArgs) Handles PictureBox1.ItemDataBinding
        Try
            Me.PictureBox1.Value = ResolveUrl("C:\MyPictures\MyPicture.jpg")
        Catch ex As Exception
        Finally
        End Try
    End Sub

    Public Shared Function ResolveUrl(relativeUrl As String) As System.Drawing.Image
        Dim path As String = System.Web.HttpContext.Current.Server.MapPath(relativeUrl)
        Return System.Drawing.Image.FromFile(path)
    End Function

--------------------------------------------------------------------------------------------------------------------------------

Can anyone point me in the right direction for how to do this? I realise there may be a much better way of dynamically assigning pictures but simply can't find a method that works.

Thanks for reading
Richard

2 Answers, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 1
answered on 20 Oct 2016, 08:29 AM
Incidentally, I also tried this from the PictureBox example and it didn't work either. I think the original example i posted expects a relative path but surely this should work?:

        Dim image1 As Image = Image.FromFile("C:\MyPictures\MyPicture.jpg")
        Me.PictureBox1.Value = image1
0
Stef
Telerik team
answered on 24 Oct 2016, 11:37 AM
Hello Richard,

If the image will be changed dynamically, our recommendation is to use a user function that returns an Image object, path to an image file on the server or the image bytes - PictureBox: Data Binding.

If there is an error on preview, it is related to the attempt to get the image. If it is about the path to the image, please check if the image is on the server machine at the specified address.


In order to provide you more accurate suggestions, please post the error message and the PictureBox settings (code snippet), including details in which preview you get the error (design-time or in a viewer).

Regards,
Stef
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Richard
Top achievements
Rank 1
Answers by
Richard
Top achievements
Rank 1
Stef
Telerik team
Share this question
or