Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8306217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:17:09+00:00 2026-06-08T18:17:09+00:00

I have an application that holds an image in a picture box. When you

  • 0

I have an application that holds an image in a picture box. When you press ctrl+C it copies the image to clipboard. I use a thread to perform the actual clipboard operations.

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

Right now, if I repeatly send ctrl+C (eg: hold the keys down), a new thread would be spawned. How can I change the code so that I re-use the clipboard thread, and have it tell me whether it’s currently copying data or not so that I don’t try to execute another copy command while it’s still working.

UPDATE

Now it works

I’ve changed it to use a background worker

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

But now an exception occurs on

Clipboard.Clear();

saying

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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:17:10+00:00Added an answer on June 8, 2026 at 6:17 pm

    First off, you should be invoking when accessing UI controls from a background thread:

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

    Then invoke that from a BackgroundWorker. Lots of examples of that are available on the web.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that is pretty memory hungry. It holds a large amount
In my application I have a UILabel that holds an expanding number of entries
In my application I want to have one view that holds 2 partial views.
I have an MVVM Silverlight 4 application that holds a list of modules (a
I have ipad application which has 3 tableviews with holds data like label,image,property lists
I have a GUI Strings .resx file in my application that holds all the
I have application that makes different queries with different results so the caching in
I have application that is connecting to the DB and if I enter incorrect
I have application that is up more than 3 days. I can see in
I have application that brings response via Ajax and creates 5-20 new jQuery click

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.