Hi,
My application is in VS2010,Silverlight and coded in C#.Net.
My requirement is i want to upload Image in my Imagebox control.As there is no PictureBox control in Silverlight im using ImageBox.
I have a Imagebox,below that a Browse button.
When the User clicks on Browse button,Dialogbox should open and when the USer selects a Image it should be loaded to Image box..
Example for Picturebox is available on the forum but i want it for Imagebox.
Below is my code.

OpenFileDialog imageDialog = new OpenFileDialog();

            //Filter default filetype to be allowed for selection 
            imageDialog.Filter = "Pictures (*.jpg)|*.jpg";
            imageDialog.FilterIndex = 1;

            //Allows multiple files to be selected 
            imageDialog.Multiselect = true;

            //Call the ShowDialog method to show the dialog box 
            bool? userClickedOK = imageDialog.ShowDialog();

            //Process input if the user clicked OK 

            if (userClickedOK == true)
            {
              image1.Source = 
            }

image1 is my image control
Please correct me in image1.Source=.....

Dani AI

Generated

You are on the right track: in Silverlight you load into an Image control (not a WinForms PictureBox), and you feed it a BitmapImage built from a stream. is correct about the control difference, and your later snippet is close. The part that trips people up is that browser‑hosted Silverlight cannot use a local file path URI; it must read the selected file as a stream and call BitmapImage.SetSource.

Here is a slightly safer pattern that keeps the file stream short‑lived, copies to memory, and wires up basic error handling. It also supports common formats via the filter.

private void Browse_Click(object sender, RoutedEventArgs e)
{
    var dlg = new OpenFileDialog
    {
        Filter = "Image files (*.jpg;*.jpeg;*.png;*.gif)|*.jpg;*.jpeg;*.png;*.gif",
        Multiselect = false
    };

    if (dlg.ShowDialog() == true && dlg.File != null)
    {
        // Copy file to memory so we can close the file immediately.
        using (var fileStream = dlg.File.OpenRead())
        {
            var ms = new MemoryStream();
            var buffer = new byte[4096];
            int read;
            while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                ms.Write(buffer, 0, read);

            ms.Position = 0;

            var bmp = new BitmapImage();
            bmp.ImageFailed += (s, args) => { /* show a friendly message */ };
            bmp.ImageOpened += (s, args) => { /* optional: update UI */ };
            bmp.SetSource(ms);

            image1.Source = bmp;   // image1 is your <Image/>
        }
    }
}

Notes:

  • ’s new BitmapImage(new Uri(filename)) works in WPF/WinForms, but not for local paths in sandboxed Silverlight. Use SetSource on the stream instead.
  • Call ShowDialog() from a user gesture (e.g., your Browse button click); Silverlight requires it.
  • If you only want one image, set Multiselect = false. If you later need multiple, iterate dlg.Files.
  • Consider image1.Stretch = Stretch.Uniform in XAML so large images scale nicely.

Recommended Answers

All 7 Replies

I don't know of any ImageBox in C#.
Only a PictureBox control with an Image property.
To load an image in a picturebox use something like:
pictureBox1.Image = MyJPEGimage;

im Using Silverlight.I have tried Picturebox in normal windows application.
But there is no Picturebox in silverlight application on Imagebox is there.

imageDialog is the object of OpenFileDialog and i was assigning this object to Image Source.

We will have to bind image to Image.Source. Below is the code snippet I tried and able to bind image.

OpenFileDialog objFileDialog = new OpenFileDialog();
            bool? IsSelected = objFileDialog.ShowDialog();
 
            if (IsSelected == true)
            {
                BitmapImage bitImage = new BitmapImage();
                bitImage.SetSource(objFileDialog.File.OpenRead());
 
                //Image1 is object of Image
                Image1.Source = bitImage;
            }

the following is some codes in loading images form files in C#.

    public static void LoadImageFromFileDemo()
    {
        string fileName = "c:/Sample.png";

        REImage reImage = REFile.OpenImageFile(fileName);

    } 

with proper C# sample codes, you can load image from url and load image from graphics and other routes. thanks for the above helping.

if(userClickedOK == true){

string filename = imageDialog.FileName;
image1.Source = new BitmapImage(new Uri(filename));

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.