Friday 12 September 2008

Scrapping data using C#

Found an interesting article about screen scrapping here. Didn't think it was that easy. Here is some C# code that gets google's homepage data.

WebClient webClient = new WebClient();
string url = "http://www.google.com";
byte[] dataArray = webClient.DownloadData(url);
UTF8Encoding encoding = new UTF8Encoding();
string data = encoding.GetString(dataArray);


Once you have the data string you can do your own parsing to use the data.

TFS 2005 to TFS 2008 upgrade

Ran into a bit of an issue today when upgrading to TFS 2008. What we did was create an identical copy of TFS 2005 server (the live machine) to a new cloned server.

I then started to follow the directions laid out by Mathias here (Very useful)

The problem is I began to uninstall on the "clone" server and all of a sudden our live TFS server started to report errors. I found a useful post here detailing the error and the reasons.

Basically what it boils down to is the TFS clone server has the original live server's name configured as the database tier. So as I uninstalled it on the clone server it was actually removing stored procedures on the live server. Not good!!!

We resolved this my reverting back to the previous nights backup.

So be aware of this when installing TFS 2008 on a cloned machine.

Thursday 11 September 2008

CS0433: The type {0} exists in both Temporary ASP.NET Files dlls

I ran into this bug today and found a really simple solution that worked for me. Hopefully it can help others.

Bug:
CS0433: The type 'ASCX' exists in both '..Temporary ASP.NET Files\{0}.dll' and '..Temporary ASP.NET Files\{1}.dll'

Solution:
In your Visual Studio solution. Clean the entire solution. Then rebuild the entire solution.

Wednesday 10 September 2008

Simple example how to resize images using C#

The other day I needed to resize images with a width greater than 500px to a new width of 250px, but also keeping the height in proportion to the width. Below is a snippet of code I used to achieve this.

using System.Drawing;
using System.Drawing.Drawing2D;

string targetPath = "C:\\TestImage.jpg";
string destinationPath = "C:\\TestImage_New.jpg";
int newWidth = 200;
Image image = Image.FromFile(targetPath );
if (image.Width > 500)
{
    float percent = (float)newWidth / (float)image.Width;
int newHeight = (int)(image.Height * percent);
Bitmap bitmap = new Bitmap(newWidth, newHeight);
Graphics graphics = Graphics.FromImage((Image)bitmap);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
graphics.Dispose();
image.Save(destinationPath);
}