PC Review


Reply
Thread Tools Rate Thread

C# Clipboard problem

 
 
=?Utf-8?B?Sm9zZQ==?=
Guest
Posts: n/a
 
      25th Jul 2005
Hello,
I'm having a strange clipboard problem. In my code, I send Alt+Print Screen
to the current window and save the image to disk. The code is pasted below:

public string CaptureAndSave()
{
SendKeys.Send("%{PRTSC}");
//Clipboard.SetText("0");
//Clipboard.Clear();
//MessageBox.Show("here");
if (Clipboard.ContainsImage())
{
//get image
Image image = (Bitmap)Clipboard.GetImage();

//prep file name
string s = Path.GetFileName(Path.GetTempFileName());
string fileName =
Path.Combine(Directory.GetCurrentDirectory(),
Path.ChangeExtension(s, ".emf"));

//save image
image.Save(fileName, System.Drawing.Imaging.ImageFormat.Emf);
}
else
MessageBox.Show("no image!");

The behavior of the program, unexpectedly, depends on the current state of
the clipboard. If the clipboard is empty, or if it contains text,
"Clipboard.ContainsImage()" will return false. If the clipboard happens to
contain an image, it will save that one instead. This is strange because if I
open mspaint and click Edit->Paste, it will paste the correct image. I
thought, perharps, the SendKeys function needed more time than I was giving
it but the behavior is the same even if I use Thread.Sleep or FOR loops. The
only configuration that worked (as expected) was when I displayed a message
box after sending the keys. Does anyone have any idea why the program behaves
this way? I'm using VS 2005 beta2 with .NET Framework v2.0. I do not have a
release version of VS.NET installed to test whether this is a problem related
to my beta 2 version of VS.

Thanks in advance,
Jose
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9zZQ==?=
Guest
Posts: n/a
 
      26th Jul 2005
It appears that using SendKeys.SendWait, instead of SendKeys.Send, fixes the
problem. Still, many examples from programming forums use SendKeys.Send. Why
does it work for them?

"Jose" wrote:

> Hello,
> I'm having a strange clipboard problem. In my code, I send Alt+Print Screen
> to the current window and save the image to disk. The code is pasted below:
>
> public string CaptureAndSave()
> {
> SendKeys.Send("%{PRTSC}");
> //Clipboard.SetText("0");
> //Clipboard.Clear();
> //MessageBox.Show("here");
> if (Clipboard.ContainsImage())
> {
> //get image
> Image image = (Bitmap)Clipboard.GetImage();
>
> //prep file name
> string s = Path.GetFileName(Path.GetTempFileName());
> string fileName =
> Path.Combine(Directory.GetCurrentDirectory(),
> Path.ChangeExtension(s, ".emf"));
>
> //save image
> image.Save(fileName, System.Drawing.Imaging.ImageFormat.Emf);
> }
> else
> MessageBox.Show("no image!");
>
> The behavior of the program, unexpectedly, depends on the current state of
> the clipboard. If the clipboard is empty, or if it contains text,
> "Clipboard.ContainsImage()" will return false. If the clipboard happens to
> contain an image, it will save that one instead. This is strange because if I
> open mspaint and click Edit->Paste, it will paste the correct image. I
> thought, perharps, the SendKeys function needed more time than I was giving
> it but the behavior is the same even if I use Thread.Sleep or FOR loops. The
> only configuration that worked (as expected) was when I displayed a message
> box after sending the keys. Does anyone have any idea why the program behaves
> this way? I'm using VS 2005 beta2 with .NET Framework v2.0. I do not have a
> release version of VS.NET installed to test whether this is a problem related
> to my beta 2 version of VS.
>
> Thanks in advance,
> Jose

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Transfer clipboard from Task pane clipboard(office?) content to Excel (windows?) clipboard? tskogstrom Microsoft Excel Programming 2 6th Mar 2007 12:50 PM
Clipboard gets empty by itself, cleared clipboard, copy paste doesn't work, outlook clears clipboard, problems with clipboard - possible solution Jens Hoerburger Microsoft Outlook 0 24th Aug 2006 02:44 PM
Increase clipboard entries; find clipboard that holds multiples Chad Harris Windows XP Customization 3 28th Oct 2005 02:13 AM
How do I disable Office Clipboard so Copy Paste uses traditional Windows Clipboard dwilliamson@ivsi.com Microsoft Excel Discussion 10 24th Feb 2005 02:36 PM
Problems with clipboard : system.runtime.interopservices : The requested clipboard operation failed Dennis Microsoft C# .NET 0 5th Apr 2004 06:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:27 AM.