Tag Archive for 'Code Snippet'

AODL – The .NET Interface for ODF files

In my spare free time im working on a simple movie database to have a sortable collection list of my movies. Hitherto i’ve do that with the Spreadsheet of OpenOffice.
After some time of development i’ve finished a first betaversion of my tool and starts to work on a importer.
The best way to import ODF files is to extract the content files fromt the zip-encoded files and read the xml. This steps were already done by the OpenOffice team and somebody has implement this steps in a .NET interface library. I’ve found this project (AODL, Sourceforge) and starts to working with it. Bug it has a bad bug.
I have a big table with many cells in many rows, but some of this cell – also severals side by side – are empty. And OpenOffice creates on this empty cells a XML attribute called “table:number-columns-repeated” which descripes how many copies of this cells came after this one.
The AODL assembly do’snt has this attribute calculation implemented so that if’ve sit down and do that. So, here’s the bugfix and the assembly.

Link: Download

flattr this!

Code Snippet – C#: Prüfen ob ein String eine gültige Zahl ist / ein gültiger Alpha-numerischer String

public bool IsAlphaNumericString(string strAlphanum) {
    System.Text.RegularExpressions.Regex pattern = new System.Text.RegularExpressions.Regex(@”^[A-Za-z0-9]+$”);
    return pattern.IsMatch(strAlphanum.Trim());
}

public bool IsNumeric(string strAlphanum) {
    System.Text.RegularExpressions.Regex pattern = new System.Text.RegularExpressions.Regex(@”^[0-9]+$”);
    return pattern.IsMatch(strAlphanum.Trim());
}

flattr this!

LIMIT x,y in T-SQL?

Wer kennt nicht das Problem, Daten Seitenweise aus der Datenbank zu laden. MySQL kann das auch sehr schön mit einem beherzten LIMIT x,y, aber wenn man nun an einem Microsoft SQL Server sitzt und nur T-SQL hat?
Dagegen hilft nur der folgender Code Schnitzel:

SELECT * FROM
    SELECT TOP x * FROM (
        SELECT TOP y fields
        FROM table
        WHERE conditions
        ORDER BY table.field  ASC) as foo
    ORDER by field DESC) as bar
ORDER by field ASC

flattr this!

Code Snippet – C#: Generate square thumbnails

To generate a square thumbnail in c#, use this code:

public Bitmap Generate75x75Pixel(Bitmap image) {
    if(image == null)
    throw new ArgumentNullException("image");
    Bitmap bmp = null;
    Bitmap crapped = null;
    int x = 0, y = 0;
    double prop = 0;
    if(image.Width > 75) {
        // compute proportation
        prop = (double)image.Width / (double)image.Height;
        if(image.Width > image.Height) {
            x = (int)Math.Round(75 * prop, 0);
            y = 75;
        }
        else {
            x = 75;
            y = (int)Math.Round(75 / prop, 0);
        }
        bmp = new Bitmap((Image)image, new Size(x, y));
        crapped = new Bitmap(75, 75);
        Graphics g = Graphics.FromImage(crapped);
        g.DrawImage(bmp,
            new Rectangle(0, 0, 75, 75),
            new Rectangle(0, 0, 75, 75),
            GraphicsUnit.Pixel
        );
        bmp = crapped;
    }
    else {
        crapped = image;
    }
    return bmp;
}

flattr this!