将PictureBox内容发送到MsPaint

时间:2013-04-17 23:48:06

标签: vb.net arguments picturebox

如何发送要在油漆中编辑的图片框的内容? 我想过快速保存它然后发送临时地址加载,但我认为这会导致一些小的保存问题。

2 个答案:

答案 0 :(得分:3)

不幸的是,我现在正在提供C#的答案。幸运的是,只需要改变语法而不是内容。

假设这是您的图片框控件,请将内容(作为位图)放在剪贴板上。现在你可以将它粘贴到MSPaint中,但如果你把它作为前景,你可以使用SendMessage或SendKeys等。

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

一个糟糕的例子,可选择打开mspaint并等待它出现,使用SendKeys粘贴。

    [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");
    }

答案 1 :(得分:0)

回答我自己只是为其他可能需要一个简单例子的人展示我的工作代码。

所以用img_picture作为我的图片框

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")

如果没有线程暂停,您将收到AppActivate声明没有退出此类进程的错误。

感谢Bland的帮助。