Archive for the ‘WinForms’ Category

Retrieve Image from Windows Clipboard via .Net C#

Monday, November 2nd, 2009

If you want to snag clipboard data from a different application into the context of your running .Net application, here’s how you can do it:

private static Image _clipBoardImage = null;

private static Image GetImageFromCopyPasteBuffer()
{
Thread t = new Thread(new ThreadStart(GetClipboardBitmap));
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
return _clipBoardImage;
}

private static void GetClipboardBitmap()
{
IDataObject data = Clipboard.GetDataObject();
if (data == null || !data.GetDataPresent(DataFormats.Bitmap, true))
throw new ApplicationException("No clipboard image data was present.");
_clipBoardImage = (Image)data.GetData(DataFormats.Bitmap);
}

Hope this helps!

ASP.Net WebServices: Enabling or Disabling POST, GET, and Documentation Requests

Wednesday, September 10th, 2008

It is relatively easy to control access to the various HTTP request types (protocols) for your ASP.Net WebService.

Add something like this to the system.web section of your web.config:

<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpGet"/> <!-- Because this web service is all GET methods remotely -->
<add name="HttpPostLocalhost"/> <!-- So you can test/POST locally -->
<remove name="Documentation"/>  <!-- So the outside world can't see your method documentation -->
<remove name="HttpPost"/> <!-- Remove general POSTs from the outside world -->
</protocols>
</webServices>

Of course you will want to tailor this to the needs of your own web service. :)



A Program Window Restores Down to Just the Title Bar

Monday, August 4th, 2008

Yeah, this really has nothing to do with programming, but wow, was this annoying.  Some how, Outlook shrunk itself down to just the title bar and I couldn’t find a way to restore it to a useful size (it was either just the title bar or fully maximized).

Lo and behold, there is a handy-dandy way to resize your windows.

First, Restore the window.  Next, right-click on the title bar and choose “Size”.  This allows you to customize (and fix whatever borked it) the window size.

Always check your context menu is the moral of the story I guess…

WinForm Exception Catching

Monday, July 28th, 2008

Here’s a general way to catch most outside exceptions in your WinForm code.  Add this to your program wrapper:

Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Of course, it is always better to handle specific cases with specific types of exceptions, but it is also nice to have a general handler for anything that escapes your exception handling.