Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
//dlg.FileName = "Document";
//dlg.DefaultExt = ".txt"; 
dlg.Filter = "jpg files (*.jpg)|*.jpg|gif files (*.gif)|*.gif|jpeg files (*.jpeg)|*.jpeg";
if(dlg.ShowDialog()==true)
{
  var fileName = System.IO.Path.GetFileName(dlg.FileName);
  textBox4.Text=fileName.ToString();
  //var path = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("/Images/"), fileName);
  BitmapImage src = new BitmapImage();
  src.BeginInit();
  src.UriSource = new Uri(textBox4.Text.Trim(), UriKind.Relative);
  src.CacheOption = BitmapCacheOption.OnLoad;
  src.EndInit();
  image1.Source = src;

the error shows
Could not find file 'c:\users\chinmaya\documents\visual studio 2010\Projects\WpfApplication5\WpfApplication5\bin\Debug\Chrysanthemum.jpg'.
Posted
Updated 29-Sep-12 1:18am
v3

You have defined:
C#
src.UriSource = new Uri(textBox4.Text.Trim(), UriKind.Relative);

Based on that, the filepath formed is pointing to your Bin/Debug folder.

Based on the error, it looks like the image is not placed in Bin/Debug/ folder. You need to make sure that the path is correct.

Have a defined image folder where you keep the images. If inserted, place the image there. At the time of picking it up for showing/editing, use the folder path (fixed and known by you) along with the filename to display.
 
Share this answer
 
Comments
chinmaya parija1 1-Oct-12 0:59am    
how to insert image in the folder. please help me.
var path = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("/Images/"), fileName);
it is not working.
first a folder name(image) in your project inside Bin->Debug folder
C#
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.Filter = "jpg files (*.jpg)|*.jpg|gif files (*.gif)|*.gif|jpeg files (*.jpeg)|*.jpeg";
            if (dlg.ShowDialog() == true)
            {
                
                if (!File.Exists(dlg.FileName))
                {
                    var fileName = System.IO.Path.GetFileName(dlg.FileName);
                    textBox4.Text = fileName.ToString();
                    image1.Source = new BitmapImage(new Uri(dlg.FileName, UriKind.Absolute));
                    File.Copy(dlg.FileName, System.IO.Path.Combine(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "image"), System.IO.Path.GetFileName(dlg.FileName)));
                }
                else
                    MessageBox.Show("This image already exists"+"\n"+"Please upload a new image.");               
            }
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900