How do I set it up so an application can use mspaint to modify an Image multiple times in a single run?

Robert Gustafson 606 Reputation points
2021-10-05T08:41:20.797+00:00

WHAT I HAVE:
Visual Basic 2019, WinForms, .NET Framework 4.6.1+

MY PROBLEM:

I've learned how to design an app so that it can use mspaint.exe to edit an Image for the app user, as follows:

ImageInstance.Save(FileName) 'FileName is a temporary file to be passed to mspaint
Process.Start("mspaint.exe", """" & FileName & """").WaitForExit()
ImageInstance.FromFile(FileName)

There are some problems with this approach, however. While it works like a charm the very 1st time the code is executed in a run, if the above code is executed again within a single program run--say, if the user wants to edit the image again--then I get an ExternalException on the Image.Save method the 2nd time around. (You can't save an image back to the same file it's created from! And if I try to delete FileName first, I get a "used by another process" exception.) Also, even if I get past that, the mspaint app itself won't let a user save changes upon being fired up the 2nd time around, citing "sharing issues"! (BTW, if I try to start mspaint using a Process instance, rather than the static method, then mspaint balks the very 1st time the user tries to save changes in it.)

I need a reliable way for a program to repeatedly pass an image to an external app such as mspaint, where the user can then use the app to make changes to the image, and then have the program retrieve the saved changes to the image--all during the same run of the "primary" program. Please give me an answer ASAP, in VB.NET, and in a general-purpose form that will work for working with a typical graphics-editor app (like mspaint)--or any other external app being run within my apps, for that matter. And keep it as simple as possible!

PS. If I can't solve this problem, then I could have serious difficulty being able to pass data back and forth to other apps for those apps to modify while my own apps are running and using them.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,870 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,655 questions
0 comments No comments
{count} votes

Accepted answer
  1. Robert Gustafson 606 Reputation points
    2021-10-05T18:51:17.55+00:00

    From RobertGustafson-1682 to @Viriol-1:

    The code below works, because Path.GetTempFileName() guarantees a different file name every time it's called. Note that I have to generate a Bitmap copy from the Image returned in the PictureFile by mspaint and destroy the original Image returned in order to be able Delete the PictureFile thereafter:

            Dim PictureFile As String = IO.Path.GetTempFileName()
            Try
                ImageInstance.Save(PictureFile, Drawing.Imaging.ImageFormat.Jpeg)
                Process.Start(FileName, """" & PictureFile & """").WaitForExit()
                Dim NewImage As Image = Bitmap.FromFile(PictureFile)
                ImageInstance = New Bitmap(NewImage)
                NewImage.Dispose() : NewImage = Nothing
             Finally
                IO.File.Delete(PictureFile)
            End Try
    
    0 comments No comments

0 additional answers

Sort by: Most helpful