3
Answers

To save image in application

Photo of mahesh

mahesh

13y
1.9k
1
Dear All,
I am working on a silverlight project.In this I need to upload an image and save it in a folder in the server(i.e., in a folder in the application). I want this funtion to be done on button click.

Thanks in advance.

Answers (3)

0
Photo of Priya Linge
NA 5k 708.3k 13y

Attachment uploadimages.rar

Hi,

You can upload images as follow,

On Button_click.

  private void button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "JPEG files|*.jpg";
            //openFileDialog.Filter = "Images (*.jpg, *.png, *.bmp)|*.jpg;*.png;*.bmp";
            if (openFileDialog.ShowDialog() == true)
            {
                Stream stream = (Stream)openFileDialog.File.OpenRead();
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, (int)stream.Length);
                bi = new BitmapImage();
                bi.SetSource(stream);
                Myimage.Source = bi;
                string fileName = openFileDialog.File.Name;

                ServiceReference1.ImageFile imagefile = new ServiceReference1.ImageFile();
                imagefile.ImageName = fileName;
                imagefile.Imagestream = bytes;

//Serive call for saving your images in server folder.

                ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
                service.UploadCompleted += new EventHandler<ServiceReference1.UploadCompletedEventArgs>(service_UploadCompleted);
                service.UploadAsync(imagefile);

At service side we have following code to save images in server folder.


public bool Upload(ImageFile image)
        {
            FileStream fileStream = null;
            BinaryWriter writer = null;
            string filePath;
            try
            {
                filePath = HttpContext.Current.Server.MapPath(".") + ConfigurationManager.AppSettings["PictureUploadDirectory"] + image.ImageName;
                if (image.ImageName != string.Empty)
                {
                    fileStream = File.Open(filePath, FileMode.Create);
                    writer = new BinaryWriter(fileStream);
                    writer.Write(image.ImageName);
                }
                return true;
            }

         catch (Exception)
            {
                return false;
            }
            finally
            {
                if (fileStream != null)  
                    fileStream.Close();
                if (writer != null)    
                    writer.Close();
            }
        }
         

You can check images which keeps in pictures folder in server,

D:\\priya\\dotnet\\silverlight applications\\Uploadimages\\Uploadimages.Web/Pictures/pis.jpg.

Please see attached file .

Accepted
0
Photo of mahesh
NA 116 67.4k 13y
Thank you so much..
0
Photo of Jiteendra Sampathirao
NA 6.9k 1.5m 13y

Hi mahesh go through this thread

hope it will help you......