Thursday, May 27, 2010

Generic GDI+ error when trying to save a bitmap

I was trying to read an image from a URL using webclient, but it kept erroring out with a GDI+ error when I was trying to save it. The image in question is Tiff, but the same code worked when it was JPG - strange!!

But anyway I was able to get around this issue after stumbling across the following MSDN article: http://support.microsoft.com/?id=814675

using (Stream ms = new MemoryStream(client.DownloadData("http://image.com")))
{
  Image img = Image.FromStream(ms);
  Bitmap bmp = img as Bitmap;
  Graphics g = Graphics.FromImage(bmp);
  Bitmap bmpNew = new Bitmap(bmp);
  g.DrawImage(bmpNew, new Point(0, 0));
  g.Dispose();
  bmp.Dispose();
  img.Dispose();

  bmpNew.Save("C:\\image.tiff");
}

Tuesday, May 25, 2010

Deleting an XML Node in C#

XML File



Code to remove an Order node
XmlDocument doc = new XmlDocument();
doc.Load(Properties.Settings.Default.OrderXMLFileLocation);
XmlNode node = doc.SelectSingleNode(string.Format("/CurrentOrders[Order='{0}']", orderNumber));
node.ParentNode.RemoveChild(node); 

Friday, May 21, 2010

Using XSD to generate classes

Its really nice to work with classes rather than parsing the XML structure using Xpath or the old fashioned XML reader. You can use XSD.exe to generate a c# class based on the XML document or XML schema and then use it to output an XML file.
1. Open VS command prompt.
2. CD to the location where your XSD file is.
3. Type in XSD -c -l:c# -n:[your namespace] [XSD filename]
This will generate a c# class. You can use that class along with the following code to output an XML file

XmlSerializer xmlSerializer = new XmlSerializer(typeof([Your Class]));
using (XmlWriter writer = XmlWriter.Create("[Path]"))
{
 xmlSerializer.Serialize(writer, [instance of your class]);
writer.Close(); }

Monday, May 17, 2010

Tortoise SVN Global Ignore Pattern for Visual Studio

I spend about 10 minutes everytime I install Tortoise SVN. So as a reminder to myself and anyone else who is interested - the global ignore pattersn for Tortoise:
*bin *obj RECYCLER Bin *.user *.suo *.dcu __history ModelSupport_* *.rsm thumbs.db