Open image in Windows Photo Viewer

48,486

Solution 1

Start it in a new Process

Process photoViewer = new Process();
photoViewer.StartInfo.FileName = @"The photo viewer file path";
photoViewer.StartInfo.Arguments = @"Your image file path";
photoViewer.Start();

Solution 2

I think you can just use:

Process.Start(@"C:\MyPicture.jpg");

And this will use the standard file viewer associated with .jpg files - by default the windows picture viewer.

Solution 3

The code fetch photo from ftp and shows the photo in Windows Photo Viewer. I hope it will usefully for you.

  public void ShowPhoto(String uri, String username, String password)
        {
            WebClient ftpClient = new WebClient();
            ftpClient.Credentials = new NetworkCredential(username,password);
            byte[] imageByte = ftpClient.DownloadData(uri);


            var tempFileName = Path.GetTempFileName();
            System.IO.File.WriteAllBytes(tempFileName, imageByte);

            string path = Environment.GetFolderPath(
                Environment.SpecialFolder.ProgramFiles);

            // create our startup process and argument
            var psi = new ProcessStartInfo(
                "rundll32.exe",
                String.Format(
                    "\"{0}{1}\", ImageView_Fullscreen {2}",
                    Environment.Is64BitOperatingSystem ?
                        path.Replace(" (x86)", "") :
                        path
                        ,
                    @"\Windows Photo Viewer\PhotoViewer.dll",
                    tempFileName)
                );

            psi.UseShellExecute = false;

            var viewer = Process.Start(psi);
            // cleanup when done...
            viewer.EnableRaisingEvents = true;
            viewer.Exited += (o, args) =>
            {
                File.Delete(tempFileName);
            };


        }

Best Regards...

Solution 4

I am trying the other answers but they all return the same error about how the location isn't an OS App, so I'm not sure where the issue lies. I did however discover another method to open the file.

string Location_ToOpen = @"The full path to the file including the file name";
if (!File.Exists(Location_ToOpen))
{
   return;
}

string argument = "/open, \"" + Location_ToOpen + "\"";

System.Diagnostics.Process.Start("explorer.exe", argument);

It starts out by testing if the file exists or not. If it doesn't exist it would have caused an error.

After that it simulates a "open" request on a file explorer without opening a file explorer, then the system opens the file with the default app.

I am currently using this method in my project so I hope it works for you too.

Share:
48,486

Related videos on Youtube

Beliszz
Author by

Beliszz

Updated on January 23, 2022

Comments

  • Beliszz
    Beliszz over 2 years

    How to open .jpg image in Windows Photo Viewer from C# app?

    Not inside app like this code,

    FileStream stream = new FileStream("test.png", FileMode.Open, FileAccess.Read);
    pictureBox1.Image = Image.FromStream(stream);
    stream.Close();
    
  • Trent
    Trent over 11 years
    The best part about using Process.Start() is that it doesn't care what sort of file you give it, it will just use the default viewer. i.e. a PDF will automagically open in Adobe Viewer.
  • Hakan Fıstık
    Hakan Fıstık almost 7 years
    Every one know that, but what is the name of the default Image viewer in Windows?
  • Saeed Neamati
    Saeed Neamati over 4 years
    Didn't work for me. The specified executable is not a valid application for this OS platform
  • iandotkelly
    iandotkelly over 4 years
    @SaeedNeamati ... without knowing much about your OS setup, I can't help specifically. You might find the following will help you look into this ... stackoverflow.com/questions/46808315/…