Link to home
Start Free TrialLog in
Avatar of printmedia
printmedia

asked on

Open Windows Photo Viewer within my vb.net windows form

Hi all.

I have the code below that opens the image (exactimagefile) in Windows Photo Viewer. We would like to have Windows Photo Viewer open inside a vb.net windows form let's say myForm1. How can we do this?

The reason we want to do this is because we will have multiple images open at the same time and would like to customize the form title. One would say: "Exact Image", another form will same "OEM Image" (and shows windows photo viewer inside the form) etc.

Unless there's a way to change the Windows Photo Viewer form title I think this is the only way to get it to work: Display windows photo viewer within a vb.net windows form with this windows form having a specific form title.

Thank you in advance!
Private Sub ExactImage_DoubleClick(sender As Object, e As EventArgs) Handles ExactImage.DoubleClick
        System.Diagnostics.Process.Start("\\myserver\Graphics\Cross Reference Images\" & exactimagefile)
        
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of printmedia
printmedia

ASKER

Thank you for your reply.
I was testing out your VB solution and I replaced your line:
proc = Process.Start(txtProcess.Text)

Open in new window

With (to open the image file)
proc = Process.Start("\\myServer\Graphics\Cross Reference Images\1000038.jpg")

Open in new window

And I get the following error: "An unhandled exception of type 'System.NullReferenceException' occurred in VBDemo.exe
Additional information: Object reference not set to an instance of an object."

And it highlights the following line of code:
        Do While (proc.MainWindowHandle = IntPtr.Zero OrElse Not IsWindowVisible(proc.MainWindowHandle))

Open in new window

Although it opens Windows Photo Viewer with the image, it opens it outside the vb.net windows form in your solution. Is it because I'm not calling Windows Photo Viewer?
you surely get the error because the path contains spaces. you have to include the path in double quotes
I changed the path to have no spaces and the path is already in double quotes and I still get the same error:

proc = Process.Start("\\myServer\Graphics\1000038.jpg")

Open in new window

try

proc = Process.Start("Microsoft.Photos.exe \\myServer\Graphics\1000038.jpg")

or

proc = Process.Start("Microsoft.Photos.exe ""\\myServer\Graphics\1000038.jpg""")

Thank you for your reply HainKurt. I tried both of your suggestions and it went to the Catch statement: "Something went wrong trying to start your process",
                            "App Hoster".

I think it's something with calling Windows Photo Viewer because I tested a text file: MyTextFile.txt with some random text in the file and it was successfully loaded.
this worked for me in powershell

Start-Process "ms-photos: C:\Users\HainKurt\Pictures\84303.jpg"
so, try

proc = Process.Start("ms-photos:  \\myServer\Graphics\1000038.jpg")

or, if the path may contain spaces

proc = Process.Start("ms-photos:  ""\\myServer\Graphics\1000038.jpg""")


Same error.
what is exact code you used and the message again?


Sure. The code was borrowed from Eric's solution he provided in his initial reply.

Public Class Form1

    Private Const SC_MAXIMIZE As Integer = 61488
    Private Const WM_CLOSE As Integer = 16
    Private Const WM_SYSCOMMAND As Integer = 274

    Declare Auto Function IsWindowVisible Lib "user32.dll" (
                                                           ByVal hWnd As IntPtr
                                                           ) As Boolean
    Declare Auto Function MoveWindow Lib "user32.dll" (
                                                      ByVal hWnd As IntPtr,
                                                      ByVal x As Integer,
                                                      ByVal y As Integer,
                                                      ByVal nWidth As Integer,
                                                      ByVal nHeight As Integer,
                                                      ByVal bRepaint As Boolean
                                                      ) As Boolean
    Declare Auto Function PostMessage Lib "user32.dll" (
                                                       ByVal hWnd As IntPtr,
                                                       ByVal msg As UInteger,
                                                       ByVal wParam As IntPtr,
                                                       ByVal lParam As IntPtr
                                                       ) As Boolean
    Declare Auto Function SendMessage Lib "user32.dll" (
                                                       ByVal hWnd As IntPtr,
                                                       ByVal msg As Integer,
                                                       ByVal wParam As Integer,
                                                       ByVal lParam As Integer
                                                       ) As Integer
    Declare Auto Function SetParent Lib "user32.dll" (
                                                     ByVal hWndChild As IntPtr,
                                                     ByVal hWndNewParent As IntPtr
                                                     ) As Integer

    Private _appHandle As IntPtr
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        Dim proc As Process
proc = Process.Start("ms-photos:  \\myServer\Graphics\1000038.jpg")

Catch ex As Exception
            MessageBox.Show("Something went wrong trying to start your process",
                            "App Hoster",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error)
            Return
        End Try
'host the started process in the panel 
        Threading.Thread.Sleep(500)
        Do While (proc.MainWindowHandle = IntPtr.Zero OrElse Not IsWindowVisible(proc.MainWindowHandle))
            Threading.Thread.Sleep(10)
            proc.Refresh()
        Loop

        proc.WaitForInputIdle()
        _appHandle = proc.MainWindowHandle

        SetParent(_appHandle, panel1.Handle)
        SendMessage(_appHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    End Sub
End Class

Open in new window










what is the exception details?
not custom message that you put...
I tried it with command line as well

start "My Cute Photos" ms-photos: \\myServer\Graphics\1000038.jpg

I can start the app, but cannot send any parameters...

so, the only option is associate jpg file with ms-photos and
use

proc = Process.Start("\\myServer\Graphics\1000038.jpg") 
I don't see the exception details just the custom error message. If I call the Process.Start("\\myServer\Graphics\1000038.jpg") line without wanting to have it run within the vb.net windows form then yes it opens in Windows Photo Viewer, but when I want it to open within the vb.net windows form then that's where I'm unable.
I was reading online and it seems there isn't an executable, as such, for the Photo Viewer - it's actually a .dll. The command to open an image file in the Windows Photo Viewer is:

%SystemRoot%\System32\rundll32.exe "%ProgramFiles%\Windows Photo Viewer\PhotoViewer.dll", ImageView_Fullscreen <full path to image file> 

But can't seem to get it to work in vb.net
I have an exe

User generated image
I don't see that. I don't have a Windows App folder. Here's what I have:

C:\Program Files\Windows Photo Viewer
And when I go in there there are just dll (PhotoViewer.dll) files no exe.
are you sure you are opening photo viewer instead of ms-photos?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sure. Here's a screenshot:
User generated image
ok, so you are using Photo viewer and it is dll
so, you should pass path to explorer
and explorer checks registry to find out what app can open it
then it finds Photo Viewer and opens it passing the path to it...

I guess you should check

Launching Applications (ShellExecute, ShellExecuteEx, SHELLEXECUTEINFO)

https://docs.microsoft.com/en-us/windows/win32/shell/launch

I was trying to run this from shell/cmd but could not
maybe you can do it from your app

give it a try

proc = Process.Start("C:\windows\system32\rundll32.exe ""C:\Program Files\Windows Photo Viewer\PhotoViewer.dll"", ImageView_Fullscreen ""\\myServer\Graphics\1000038.jpg""")
Hi HainKurt,

Yeah I tried that as well and no luck. I'm going to recommend to my team to use an actual photo program (i.e. Microsoft Photos). Thank you for all your help! To you both!