Sending PictureBox Contents to MsPaint

时光总嘲笑我的痴心妄想 提交于 2019-12-11 01:34:22

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/16072215/sending-picturebox-contents-to-mspaint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!