Bee Eee Blog

Archive for February, 2008

c# Measuring a String.

by brian on Feb.21, 2008, under .NET, GUI, c#, c# coding GUI

Often times it is desirable to draw a string with a System.Drawing.Graphics. My problem came when drawing text over an image. The image itself is dark but had light spots in it. This can make seeing the text very difficult. To fix the problem I fill a box behind the text. To do that I need to know how tall and wide the text will be.

The magic function is Graphics.MeasureString. This functions returns the calculated size of the string that is being drawn.

// measure the string
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Near;
SizeF boundry = g.MeasureString(ToString(), font);

// draw the string
Rectangle textRect = new Rectangle(10, 10, (int)(boundry.Width + 2), (int)(boundry.Height + 2));
g.FillRectangle(Brushes.Black, textRect);
g.DrawRectangle(Pens.White, textRect);
g.DrawString(ToString(), font, Brushes.Yellow, textRect.X, textRect.Y, sf);
Leave a Comment : more...

C# Determining if a column exists in an SQL database using ADO.Net

by brian on Feb.15, 2008, under c#, coding

This information was very difficult to come by. There isn’t any useful documentation or tutorials that I found. I have to admit that what follows may only be valid for the System.Data.SQLite.SQLiteConnection sql connection. Just as a warning it may or may not work with a different database connection.


The idea is that you’ve changed your database layout and you want test the opened database to make sure the changes are valid. If they aren’t then you want to alter the table. Anyway here is the code

DataTable columns = connection.GetSchema("columns");
System.Data.DataRow[] selColumns = columns.Select("COLUMN_NAME='PREVIEW' AND TABLE_NAME='IMAGES'");
if (selColumns.Length == 0)
{
        ExecuteNonQuerySql("ALTER TABLE IMAGES ADD COLUMN PREVIEW BLOB");
}

The following line gets the schema table that holds all of the columns from every table.

DataTable columns = connection.GetSchema("columns");

The following line filters through all of the columns and get’s just the column with name ‘PREVIEW’ and belongs to the table ‘IMAGES’.

System.Data.DataRow[] selColumns = columns.Select("COLUMN_NAME='PREVIEW' AND TABLE_NAME='IMAGES'");

If there are any columns that fit the filter than the length of selColumns will be greater than zero. If it doesn’t exist than selColumns.Length will be 0 and you know that the column doesn’t exist.

Leave a Comment : more...

c# Copying a 2 Dimensional Array

by brian on Feb.12, 2008, under Uncategorized

Here’s a snippet to copy a 2D array.

double[,] a = new double[3,3]{{1,2,3},{2,3,4},{3,4,5}};
double[,] b = new double[3,3];
Array.Copy(a,b,a.Length);

Pretty simple isn’t it!

Leave a Comment more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...