0

我有一个将图像保存在图片框中的应用程序。当您按 ctrl+C 时,它会将图像复制到剪贴板。我使用一个线程来执行实际的剪贴板操作。

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Control | Keys.C))
    {
        clipboardThread = new Thread(copy_to_clipboard);
        clipboardThread.SetApartmentState(ApartmentState.STA);
        clipboardThread.Start();
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

private void copy_to_clipboard()
{
    if (pic_display.Image != null)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            clipboardStatus.Text = "Copying image to clipboard...";
            pic_display.Image.Save(stream, ImageFormat.Png);
            var data = new DataObject("PNG", stream);
            Clipboard.Clear();
            Clipboard.SetDataObject(data, true);
            clipboardStatus.Text = "Copied successfully!";
        }
    }
}

现在,如果我重复发送 ctrl+C(例如:按住键),就会产生一个新线程。如何更改代码以便重新使用剪贴板线程,并让它告诉我它当前是否正在复制数据,这样我就不会在它仍在工作时尝试执行另一个复制命令。

更新

现在它可以工作了

我已将其更改为使用后台工作人员

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Control | Keys.C))
    {
        if (!backgroundWorker1.IsBusy)
            backgroundWorker1.RunWorkerAsync();
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

private void copy_to_clipboard()
{
    using (var stream = new MemoryStream())
    {
        clipboardStatus.Text = "Copying image to clipboard...";
        pic_display.Invoke((Action)(() => {
            if (pic_display.Image != null)
              pic_display.Image.Save(stream, ImageFormat.Png); 
        }));
        if (stream.Position == 0) return; // No image was saved
        var data = new DataObject("PNG", stream);
        BeginInvoke ( (Action) ( ()=> {
            Clipboard.Clear();
            Clipboard.SetDataObject(data, true);
        }
        clipboardStatus.Text = "Copied successfully!";
    }
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    copy_to_clipboard();
}

但是现在发生了异常

Clipboard.Clear();

Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.
4

4 回答 4

1

首先,您应该在从后台线程访问 UI 控件时调用:

private void copy_to_clipboard()
{
    using (var stream = new MemoryStream())
    {
        clipboardStatus.Text = "Copying image to clipboard...";
        pic_display.Invoke((Action)()=> { 
            if (pic_display.Image != null)
              pic_display.Image.Save(stream, ImageFormat.Png); 
        });
        if (stream.Position == 0) return; // No image was saved
        var data = new DataObject("PNG", stream);
        Clipboard.Clear();
        Clipboard.SetDataObject(data, true);
        clipboardStatus.Text = "Copied successfully!";
    }
}

然后从 BackgroundWorker 调用它。网上有很多这样的例子。

于 2012-07-31T19:44:47.393 回答
1

我建议在单击期间执行保存——无论如何,您都应该在 UI 线程上执行此操作。您可以使用 InvokeRequired/Invoke;但是您无法控制 Save 何时接管 UI 线程。在点击之后可能需要很长一段时间,这可能会让用户感到不安。或者,可能是用户更改图像之后。离开 UI 线程需要任何时间,这很糟糕。但从可用性的角度来看,越接近点击越好。在这种情况下,可能是这样的:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Control | Keys.C))
    {
        if (pic_display.Image != null)
        {
            MemoryStream stream = new MemoryStream())
            clipboardStatus.Text = "Copying image to clipboard...";
            pic_display.Image.Save(stream, ImageFormat.Png);

            clipboardThread = new Thread(copy_to_clipboard);
            clipboardThread.SetApartmentState(ApartmentState.STA);
            clipboardThread.Start(stream);
        }
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

private void copy_to_clipboard(object state)
{
    var stream = (Stream) state;
    try
    {
        var data = new DataObject("PNG", stream);
        Clipboard.Clear();
        Clipboard.SetDataObject(data, true);
        BeginInvoke((MethodInvoker) (() => clipboardStatus.Text = "Copied successfully!"));
    }
    finally
    {
        stream.Dispose();
    }
}
于 2012-07-31T20:11:20.833 回答
0

尝试这样的事情:

object lockObj = new object();

        private void copy_to_clipboard()
        {
            lock (lockObj)
            {
                if (pictureBox1.Image != null)
                    {
                        using (MemoryStream stream = new MemoryStream())
                        {
                            clipboardStatus.Text = "Copying image to clipboard...";                            
                            pictureBox1.Image.Save(stream, ImageFormat.Png);
                            var data = new DataObject("PNG", stream);
                            Clipboard.Clear();
                            Clipboard.SetDataObject(data, true);                            
                            clipboardStatus.Text = "Copied successfully!";
                        }
                    } 
            }
        }
于 2012-07-31T19:34:38.097 回答
0

您不能将线程存储为成员吗?除非我误解了这个问题,否则 Thread.IsAlive 会做你想做的事。但是,如果您正在修改 UI 控件,就像上面所说的那样,您应该真正使用 Invoke 来确保这发生在 UI 线程上。

protected Thread clipboardThread;

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{

if (keyData == (Keys.Control | Keys.C))
{
    if (clipBoardThread == null)
    {
       clipboardThread = new Thread(copy_to_clipboard);
       clipboardThread.SetApartmentState(ApartmentState.STA);
       clipboardThread.IsBackGround = false;
    }
    if (!clipboardThread.IsAlive)
    {
       clipboardThread.Start();
    }
    return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

private void copy_to_clipboard()
{
if (pic_display.Image != null)
{
    using (MemoryStream stream = new MemoryStream())
    {
        clipboardStatus.Text = "Copying image to clipboard...";
        pic_display.Image.Save(stream, ImageFormat.Png);
        var data = new DataObject("PNG", stream);
        Clipboard.Clear();
        Clipboard.SetDataObject(data, true);
        clipboardStatus.Text = "Copied successfully!";
    }
}
}

于 2012-07-31T19:51:59.620 回答