Monday, December 8, 2014

DeepCopy of objects

public static T DeepCopy<T>(T other)
{
    using (MemoryStream ms = new MemoryStream())
    {
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(ms, other);
        ms.Position = 0;
        return (T)formatter.Deserialize(ms);
    }
}

Monday, January 20, 2014

Thursday, December 5, 2013

Read SVG Data into C# , Dotnet Graphics

Below is an easy way to convert an SVG path tag into a C# System.Drawing.Drawing2D.GraphicsPath.


This SVG project provides a solution in the following way:
var pathData = ...;

var graphicsPath = new GraphicsPath();

foreach (var segment in SvgPathBuilder.Parse(pathData))
    segment.AddToPath(graphicsPath);

graphics.DrawPath(Pens.Black, graphicsPath);
It's available as a NuGet package via:
PM> Install-Package Svg

http://stackoverflow.com/questions/5711503/converting-svg-path-data-into-gdi-graphicspath-data


Tuesday, August 14, 2012

Web.Config Transformation

Good article about Web config Transformations

This will allow you to have separate config files for both debug and release and have different setting in each.

http://blogs.msdn.com/b/webdev/archive/2009/05/04/web-deployment-web-config-transformation.aspx

Wednesday, January 25, 2012

Manual GZipStream Compression / Decompression

 public MemoryStream GZipCompress(MemoryStream mStreamOrg)
         {
             try
             {

                 byte[] buffer = mStreamOrg.ToArray();

                 MemoryStream ms = new MemoryStream();
                 // Use the newly created memory stream for the compressed data.
                 GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true);
                 Console.WriteLine("Compression");
                 compressedzipStream.Write(buffer, 0, buffer.Length);
                 // Close the stream.
                 compressedzipStream.Close();
                 Console.WriteLine("Original size: {0}, Compressed size: {1}", buffer.Length, ms.Length);

                 // Reset the memory stream position to begin decompression.
                 ms.Position = 0;
                 return ms;

             } // end try
             catch
             {
                 return null;
             }
            
         }

         private  void GZipDeCompress(MemoryStream compressedMemoryStream, long actualLength)
         {
            
             GZipStream zipStream = new GZipStream(compressedMemoryStream, CompressionMode.Decompress);
             Console.WriteLine("Decompression");
             byte[] decompressedBuffer = new byte[actualLength];
             // Use the ReadAllBytesFromStream to read the stream.
             int totalCount = ReadAllBytesFromStream(zipStream, decompressedBuffer);
             Console.WriteLine("Decompressed {0} bytes", totalCount);

            string result = Encoding.UTF8.GetString(decompressedBuffer);
             MemoryStream msTemp = new MemoryStream(Encoding.UTF8.GetBytes(result));

             var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(EntityObj));


             EntityObj deserializedObj =
                 (EntityObj)serializer.ReadObject(msTemp);



             zipStream.Close();
         }

 public static int ReadAllBytesFromStream(Stream stream, byte[] buffer)
         {
             // Use this method is used to read all bytes from a stream.
             int offset = 0;
             int totalCount = 0;
             //while (true)
             //{
             //    int bytesRead = stream.Read(buffer, offset, 100);
             //    if (bytesRead == 0)
             //    {
             //        break;
             //    }
             //    offset += bytesRead;
             //    totalCount += bytesRead;
             //}
             totalCount = stream.Read(buffer, offset, buffer.Length);
             return totalCount;
         }

Tuesday, January 24, 2012

Manual Serializaion / Deserialization of Datacontract classes

In the example given below, we will first serialize an object in memory and then deserializeit.

MemoryStream mStream = new MemoryStream();


-- Seriailize an Object

public  void WriteObject(ClassTest objTest)
         {
           
             DataContractSerializer ser =
                 new DataContractSerializer(typeof(ClassTest ));
             ser.WriteObject(mStream, objTest);
            
         }

-- Deserialize the object in memory

 public void ReadObject(ClassTest objTest)
         {
            
             DataContractSerializer ser = new DataContractSerializer(typeof(ClassTest ));

            string result = Encoding.UTF8.GetString(mStream.GetBuffer(), 0, (int)mStream.Position);
             MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(result));

             EntityObj deserializedObj =
                 (EntityObj)ser.ReadObject(ms);
             mStream.Close();
            
         }

Monday, January 23, 2012

DataContractSerializer Error in Deserialization : There was an error deserializing the object of type The maximum nametable character count quota (16384) has been exceeded while reading XML data.

Below is an example of Datacontract deserialization of an object

 public static void ReadObject(ClassTest tstObj)
         {
             FileStream fs = new FileStream(@"D:\" + tstObj.Name + ".xml", FileMode.Open);

             XmlDictionaryReader reader =
                 XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
            
             DataContractSerializer ser = new DataContractSerializer(typeof(ClassTest ));

             // Deserialize the data and read it from the instance.
             ClassTest deserializedTest =
                 (ClassTest )ser.ReadObject(reader, true);
             reader.Close();
             fs.Close();
            
         }

To overcome the error just change the highlighted line to below:

XmlDictionaryReaderQuotas dictQuota = new XmlDictionaryReaderQuotas();
             dictQuota.MaxNameTableCharCount = int.MaxValue;
             dictQuota.MaxArrayLength = int.MaxValue;
             dictQuota.MaxStringContentLength = int.MaxValue;
             XmlDictionaryReader reader =
                 XmlDictionaryReader.CreateTextReader(writer, dictQuota);