Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF
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 29-Sep-12 2:15am
Edited 29-Sep-12 2:18am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have defined:
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.
  Permalink  
Comments
chinmaya parija1 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

first a folder name(image) in your project inside Bin->Debug folder
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.");               
            }
  Permalink  
v4

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

  Print Answers RSS
0 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100