Random Snippets

  • Shorthand WPF animation
(:source lang=C#:)
var da = new DoubleAnimation(0, new Duration(TimeSpan.Parse("0:0:1"));
this.rectangle.BeginAnimation(Rectangle.OpacityProperty, da);
(:source lang=C# :)
// 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
(:source lang=Python :)
f = StringIO.StringIO()
im.save(f, "JPEG")
data = f.getvalue()
(:source lang=DOS :)
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? ;)
(:source lang=Python :)
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.