Michael Parr
Michael Parr

Reputation: 134

Sending PictureBox Contents to MsPaint

How do i go about sending the contents of a picturebox to be edited in paint? I've thought of quickly saving it temporarily then sending the temp address to be loaded, but i'd think that would cause some minor saving issues.

Upvotes: 2

Views: 1794

Answers (2)

Michael Parr
Michael Parr

Reputation: 134

Answering myself only to show my working code for anyone else who may want a nice simple example.

So with img_picture as my picturebox

Dim sendimage As Bitmap = CType(img_picture.Image, Bitmap)
Clipboard.SetDataObject(sendimage)
Dim programid As Integer = Shell("mspaint", AppWinStyle.MaximizedFocus)
System.Threading.Thread.Sleep(100)
AppActivate(programid)
SendKeys.Send("^v")

Without the thread pause you'll get an error with AppActivate claiming no such process exits.

Thanks to Bland for helping.

Upvotes: 0

bland
bland

Reputation: 2004

Unfortunately I'm providing the answer in C# at this time. Luckily, just syntax and not content will have to change.

Assuming this is your picturebox control, take the contents (as a bitmap) and put it on the clipboard. Now you can paste it into MSPaint however you'd like with SendMessage or SendKeys if you make it foreground, etc.

Bitmap bmp = new Bitmap(pictureBox1.Image);
Clipboard.SetData(System.Windows.Forms.DataFormats.Bitmap, bmp);

A poor example, with the optional opening of the mspaint and waiting for it to appear, using SendKeys to paste.

    [DllImport("User32.dll", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);


    private static void TestSendPictureToMSPaint()
    {
        Bitmap bmp = new Bitmap(pictureBox1.Image);
        Clipboard.SetData(System.Windows.Forms.DataFormats.Bitmap, bmp);

        //optional#1 - open MSPaint yourself
        //var proc = Process.Start("mspaint");

        IntPtr msPaint = IntPtr.Zero;
        //while (msPaint == IntPtr.Zero) //optional#1 - if opening MSPaint yourself, wait for it to appear
        msPaint = FindWindowEx(IntPtr.Zero, new IntPtr(0), "MSPaintApp", null);

        SetForegroundWindow(msPaint); //optional#2 - if not opening MSPaint yourself

        IntPtr currForeground = IntPtr.Zero;
        while (currForeground != msPaint)
        {
            Thread.Sleep(250); //sleep before get to exit loop and send immediately
            currForeground = GetForegroundWindow();
        }
        SendKeys.SendWait("^v");
    }

Upvotes: 3

Related Questions