Tuesday 12 April 2016

Generate Bitmap containing Centered Text

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication14
{
    class Program
    {
        static void Main(string[] args)
        {
            string txt = "HELLO";
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(131, 50);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
            g.Clear(System.Drawing.Color.DarkGreen);
            System.Drawing.Font f = new System.Drawing.Font("Courier", 25, System.Drawing.FontStyle.Bold);
            System.Drawing.SizeF szf = new System.Drawing.SizeF();
            szf = g.MeasureString(txt, f);
            int xoff = (int)((bmp.Width - szf.Width) / 2);
            g.DrawString(txt, f, System.Drawing.Brushes.Red, new System.Drawing.Rectangle(/*x*/xoff, /*y*/0, bmp.Width, bmp.Height));
            bmp.Save("c:/temp/xxx.bmp");
        }
    }

}

C# Formax XML

     // Load the XmlDocument with the XML.
     document.LoadXml(fp.FetchXml);

     writer.Formatting = System.Xml.Formatting.Indented;

     // Write the XML into a formatting XmlTextWriter
     document.WriteContentTo(writer);
     writer.Flush();
     mStream.Flush();

     // Have to rewind the MemoryStream in order to read
     // its contents.
     mStream.Position = 0;

     // Read MemoryStream contents into a StreamReader.
     System.IO.StreamReader sReader = new System.IO.StreamReader(mStream);

     // Extract the text from the StreamReader.
     String FormattedXML = sReader.ReadToEnd();

     string[] Result = FormattedXML.Replace("\r", "").Split(new char[] { '\n'});
     foreach (string s in Result)
        Consolw.WriteLine("{0}", s);

Serialize CRM Object

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Xrm.Sdk.Entity E = new Microsoft.Xrm.Sdk.Entity("entity");
            System.Runtime.Serialization.DataContractSerializer Sz =
                new System.Runtime.Serialization.DataContractSerializer(typeof(Microsoft.Xrm.Sdk.Entity));
            StringBuilder sb = new StringBuilder();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            Sz.WriteObject(ms, E);
            string str = Encoding.ASCII.GetString(ms.ToArray());
        }
    }
}