【问题标题】:Sending PictureBox Contents to MsPaint将 PictureBox 内容发送到 MsPaint
【发布时间】:2013-04-18 11:12:49
【问题描述】:

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

【问题讨论】:

    标签: vb.net arguments picturebox


    【解决方案1】:

    不幸的是,我现在用 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");
        }
    

    【讨论】:

    • 优秀。我花了一秒钟才弄清楚为什么从未找到应用程序 ID,但是一个小线程暂停修复了它。效果很好,谢谢。
    【解决方案2】:

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

    所以用 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 错误,声称没有此类进程退出。

    感谢布兰德的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 2022-01-23
      相关资源
      最近更新 更多