Tuesday, September 23, 2014

Photo Viewer using C# WPF

//Image Viewer
//create a folder named "images" under your application folder.
//then add an image viewer on your application and named it "Image_Viewer"
// next, add the code below under your browse image button or under MouseDown event of your Image_Viewer
//under Properties choose Common,Stretch=Uniform, StretchDirection=Both
//also add the following directives
//using System.IO;
//using Microsoft.Win32;

String AppPath = System.AppDomain.CurrentDomain.BaseDirectory;
OpenFileDialog dlg = new OpenFileDialog();


dlg.Filter = "(.jpg)|*.jpg";
dlg.DefaultExt = ".jpg";
dlg.ShowDialog();

try
    {
            if (System.IO.Directory.Exists(AppPath + "images") == false)
                {
                    System.IO.Directory.CreateDirectory(AppPath + "images");

                }

                //MessageBox.Show("@" + AppPath + "images\\" + System.IO.Path.GetFileName(dlg.FileName));
                File.Copy(dlg.FileName, AppPath + "images\\" + System.IO.Path.GetFileName(dlg.FileName), true);


                BitmapImage b = new BitmapImage();
                b.BeginInit();
                b.UriSource = new Uri(AppPath + "images\\" + System.IO.Path.GetFileName(dlg.FileName), UriKind.Absolute);
                b.CacheOption = BitmapCacheOption.OnLoad;
                b.EndInit();
               
                Image_Viewer.Source = b;

                //throw new Exception("");
        }
catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        }

0 comments: