Random Snippets
- Shorthand WPF animation
var da = new DoubleAnimation(0, new Duration(TimeSpan.Parse("0:0:1")); this.rectangle.BeginAnimation(Rectangle.OpacityProperty, da);
- ASync read in c# http://blogs.msdn.com/b/coding4fun/archive/2007/03/14/1879033.aspx
// sure, we could find this out the hard way using HID, but trust me, it's 22 private const int REPORT_LENGTH = 22; // report buffer private byte[] mBuff = new byte[REPORT_LENGTH]; private void BeginAsyncRead() { // if the stream is valid and ready if(mStream.CanRead) { // create a read buffer of the report size byte[] buff = new byte[REPORT_LENGTH]; // setup the read and the callback mStream.BeginRead(buff, 0, REPORT_LENGTH, new AsyncCallback(OnReadData), buff); } } private void OnReadData(IAsyncResult ar) { // grab the byte buffer byte[] buff = (byte[])ar.AsyncState; // end the current read mStream.EndRead(ar); // start reading again BeginAsyncRead(); // handle data.... }
- PIL Image to string
f = StringIO.StringIO() im.save(f, "JPEG") data = f.getvalue()
- Add passwords to a pmwiki group — edit the main group page, and append to the URL: "&action=attr" … e.g.
"http://wiki/pmwiki.php?n=NewGroup.MainPage" --> "http://wiki/pmwiki.php?n=NewGroup.MainPage&action=attr"
- ffmpeg command to crop 720x480 video to 640x480 video, bitrate at 2.8M
ffmpeg.exe -i "input.avi" -vcodec msmpeg4v2 -cropleft 40 -cropright 40 -s 640x480 -aspect 4:3 -b 2.8M -f avi output.avi
- PythonMultiFileScript - ugly, but gets the job done, right? ;)
import os from os import * files = filter (lambda f: f.find("wmv") > -1, os.listdir(os.getcwd())) for file in files: print "Doing " + file L = ['ffmpeg.exe', '-i', file, '-b', '4M', '-vcodec', 'msmpeg4v2', '-acodec', 'copy', '-f', 'avi', file + '.avi'] os.spawnv(os.P_WAIT, 'ffmpeg.exe', L)
C# Recipes and Samples
A collection of (mostly) C# recipes for doing stuff in the .NET Framework.
- Multi-Threaded Events and Event Handler - Appropriate when trying to build a multi-threaded application where one of those threads fires events.
- Configuration File Reader - Allows an app to take in configuration parameters without the use of command line arguments.
- Change Windows Wallpaper - Appropriate for pranks and such.
- Download a URL - Grabs something off the internet.
- Read a File
- Taking a Screenshot?
- Get a Temporary Filename
- GDI+ Color and HatchStyle chart
- Serialize an Object
- Binding to Methods with ObjectDataProvider
- Drop-In Reflection-based ToString method
- File name from Path
- Scratch
- FFMPEG wrapper for c# that writes flv files