Thursday, 6 February 2014

Write text on a image C#

 Write text on an image c# code:



 FileStream fs = new FileStream(@"d:\certificate.jpg", FileMode.Open, FileAccess.Read);
            System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
            fs.Close();

            Bitmap b = new Bitmap(image);
            Graphics graphics = Graphics.FromImage(b);


            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;

            SizeF s = graphics.MeasureString("[The Employee Name]", new Font("Thaoma", 70, FontStyle.Bold));
            RectangleF rectf = new RectangleF(950, 1450, 1200, 150);
            float fontScale = Math.Max(s.Width / rectf.Width, s.Height / rectf.Height);

            // graphics.DrawString("[The Employee Name]", new Font("Thaoma", 70, FontStyle.Bold), Brushes.Black, 1000, 1450, sf);

            using (Font font = new Font("Thaoma", 70 / fontScale, GraphicsUnit.Point))
            {
                //Employee Name
                graphics.DrawString("[The Employee Name]", new Font("Thaoma", 70, FontStyle.Bold), Brushes.Black, rectf, sf);

            }
            graphics.DrawString("01/01/2014", new Font("Thaoma", 45), Brushes.Black, 1200, 2100);//Date
            graphics.DrawString("My Office", new Font("Thaoma", 45), Brushes.Black, 1200, 2300);//Held at

            b.Save(@"d:\certificate1.jpg", image.RawFormat);

            image.Dispose();
            b.Dispose();
:) Enjoy