28

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();
spajce
  • 7,044
  • 5
  • 29
  • 44
Beliszz
  • 283
  • 1
  • 3
  • 4

5 Answers5

97

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.

iandotkelly
  • 9,024
  • 8
  • 48
  • 67
  • 4
    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. – Trent Feb 05 '13 at 01:22
  • 5
    Didn't work for me. `The specified executable is not a valid application for this OS platform` – Saeed Neamati Oct 07 '19 at 14:51
  • 1
    @SaeedNeamati ... without knowing much about your OS setup, I can't help specifically. You might find the following will help you look into this ... https://stackoverflow.com/questions/46808315/net-core-2-0-process-start-throws-the-specified-executable-is-not-a-valid-appl?rq=1 – iandotkelly Oct 07 '19 at 15:15
  • @SaeedNeamati: this used to work for me in the past, but indeed I get the same error as yours. Things must be different with .NET Core. What worked for me was following `ahmed.zubaca`'s solution. Just open explorer.exe instead, and pass the jpg as the 2nd parameter. – Veverke Aug 24 '23 at 13:35
20

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();
Jalal Said
  • 15,906
  • 7
  • 45
  • 68
6
public void ImageViewer(string path)
        {            
            Process.Start("explorer.exe",path);            
        }

Path is the file path of the image to be previewed.

ahmed.zubaca
  • 107
  • 2
  • 7
5

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...

1

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.