Monday, January 18, 2016

Generic in C# with example?



Generic can be defined as <T> sign after the class name, by using the generic there is no need of boxing and unboxing. Generic also can be implemented with interfaces, delegates and methods.

Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class Class1<T>
    {
        T i;
        public T prop1
        {
            get
            {
                return i;
            }
            set
            {
                i = value;
            }
        }
    }
}


Code on button click:
Class1<int> obj = new Class1<int>();
        private void button1_Click(object sender, EventArgs e)
        {

            obj.prop1 = 100;
            int x = obj.prop1;
            MessageBox.Show(x.ToString());
        }

Output : 100

Friday, January 1, 2016

Write Dynamically Text on Image in ASP.NET C#

In this article, I am going to explain how to write some text on a image in ASP. net c#.

Follow given below step to write text on an image in asp.net c#

Step 1

Go to Menu File->New-Project


Step 2

After clicking on project a new window will be open

select ASP.NET Web Application and write the name "DynamicTextOnImage" and click on "OK" button.


Code that write text on image

string name = "Dear Reader,";
 
                Bitmap bitMapImage = new System.Drawing.Bitmap(Server.MapPath(@"happynewyear.jpg"));
                Graphics graphicImage = Graphics.FromImage(bitMapImage);
 
                Color colorStringColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
                graphicImage.DrawString(name, new Font("Arial", 15, FontStyle.Bold), new SolidBrush(colorStringColor), new Point(100, 8));
 
                string stringOutPutFileName = Server.MapPath("Image\\happynewyear.jpg");
 
                using (MemoryStream memory = new MemoryStream())
                {
                    using (FileStream fs = new FileStream(stringOutPutFileName, FileMode.Create, FileAccess.ReadWrite))
                    {
                        bitMapImage.Save(memory, ImageFormat.Jpeg);
                        byte[] bytes = memory.ToArray();
                        fs.Write(bytes, 0, bytes.Length);
                    }
                }
                graphicImage.Dispose();
                bitMapImage.Dispose();
                bitMapImage.Dispose();

aspx.cs code

using System;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
 
namespace DynamicTextOnImage
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
            try
            {
                string name = "Dear Reader,";
 
                Bitmap bitMapImage = new System.Drawing.Bitmap(Server.MapPath(@"happynewyear.jpg"));
                Graphics graphicImage = Graphics.FromImage(bitMapImage);
 
                Color colorStringColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
                graphicImage.DrawString(name, new Font("Arial", 15, FontStyle.Bold), new SolidBrush(colorStringColor), new Point(100, 8));
 
                string stringOutPutFileName = Server.MapPath("Image\\happynewyear.jpg");
 
                using (MemoryStream memory = new MemoryStream())
                {
                    using (FileStream fs = new FileStream(stringOutPutFileName, FileMode.Create, FileAccess.ReadWrite))
                    {
                        bitMapImage.Save(memory, ImageFormat.Jpeg);
                        byte[] bytes = memory.ToArray();
                        fs.Write(bytes, 0, bytes.Length);
                    }
                }
                graphicImage.Dispose();
                bitMapImage.Dispose();
                bitMapImage.Dispose();
            }
            catch (Exception ex)
            {
 
            }
        }
    }
}
 
Image before writing text on image
 
image source
 


After writing text on image