Wednesday, 30 October 2013

How to Close Fancybox and Redirect to another page in asp.net and C#.

Just need to use the code.

 ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "add", "window.top.location.href ='" +RedirectUrl+ "'; parent.jQuery.fancybox.close();", true);

Sunday, 6 October 2013

how to trim text box value in javascript in .Net

    // implementing a trim function for strings in javascript
            <script language="JavaScript" type="text/javascript">
            <!--
            String.prototype.trim = function () {
            return this.replace(/^\s*/, "").replace(/\s*$/, "");
           }
           </script>

  <asp:TextBox runat="server" ID="abdc" onkeyup ="OnChange(this)" onblur="ShowAvailability()" onchange="this.value=this.value.trim()"></asp:TextBox>

Friday, 4 October 2013

Date and Time formats in asp.net using C#,VB.Net | Convert date time formats

Date and Time formats in asp.net using C#,VB.Net | Convert date time formats

Introduction: In this article i am going to explain with example list of different date and time formats/types available in asp.net using both C# and VB.Net language.

Description: In previous related articles i explained How to Get age in years,months,days,hours and seconds from DOB in asp.net C#,Vb.Net and Formatting Date in GridView and Get current date in dd/mm/yyyy format and Date format in sql server and  How to Convert Rupees,Currency or Numbers to Words.
It is very common in asp.net application or any other application to show date and time in various formats as per application requirement. There are so many different datetime formats/type available that can be used as per application requirement.Some of them are custom date and time formats. So this example will be helpful for those who are searching for datetime formatting and converting date and time format to one format to another e.g. dd/MM/yyyy to MM/dd/yyyy and in any other.

Implementation: Let's see the different formats with example.I have listed many date time formats that will be useful for developers.

C#.Net Code to show/display list of different Date and Time formats

protected void btnDateTime_Click(object sender, EventArgs e)
    {
        Response.Write("<BR>" + DateTime.Today.ToString("dd-MM-yyyy"));  // Output : 14-08-2013
        Response.Write("<BR>" + DateTime.Today.ToString("dd/MM/yyyy"));  // Output : 14/08/2013
        Response.Write("<BR>" + DateTime.Today.ToString("dd MM yyyy"));  // Output : 14 08 2013
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("dd-MM-yy"));  // Output : 14-08-13
        Response.Write("<BR>" + DateTime.Today.ToString("dd/MM/yy"));  // Output : 14/08/13
        Response.Write("<BR>" + DateTime.Today.ToString("dd MM yy"));  // Output : 14 08 13
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("ddd-dd-MM-yy"));  // Output : Wed-14-08-13
        Response.Write("<BR>" + DateTime.Today.ToString("ddd/dd/MM/yy"));  // Output : Wed/14/08/13
        Response.Write("<BR>" + DateTime.Today.ToString("ddd dd MM yy"));  // Output : Wed 14 08 13
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("dddd-dd-MM-yy"));  // Output : Wednesday-14-08-13
        Response.Write("<BR>" + DateTime.Today.ToString("dddd/dd/MM/yy"));  // Output : Wednesday/14/08/13
        Response.Write("<BR>" + DateTime.Today.ToString("dddd dd MM yy"));  // Output : Wednesday 14 08 13
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("dd-MMM-yyyy"));  // Output : 14-Aug-2013
        Response.Write("<BR>" + DateTime.Today.ToString("dd/MMM/yyyy"));  // Output : 14/Aug/2013
        Response.Write("<BR>" + DateTime.Today.ToString("dd MMM yyyy"));  // Output : 14 Aug 2013
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("dd-MMMM-yyyy"));  // Output : 14-August-2013
        Response.Write("<BR>" + DateTime.Today.ToString("dd/MMMM/yyyy"));  // Output : 14/August/2013
        Response.Write("<BR>" + DateTime.Today.ToString("dd MMMM yyyy"));  // Output : 14 August 2013
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("MM-dd-yyyy"));  // Output : 08-14-2013
        Response.Write("<BR>" + DateTime.Today.ToString("MM/dd/yyyy"));  // Output : 08/14/2013
        Response.Write("<BR>" + DateTime.Today.ToString("MM dd yyyy"));  // Output : 08 14 2013
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("MMM-dd-yyyy"));  // Output : Aug-14-2013
        Response.Write("<BR>" + DateTime.Today.ToString("MMM/dd/yyyy"));  // Output : Aug/14/2013
        Response.Write("<BR>" + DateTime.Today.ToString("MMM dd yyyy"));  // Output : Aug 14 2013
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("yyyy-MM-dd"));  // Output : 2013-08-14
        Response.Write("<BR>" + DateTime.Today.ToString("yyyy/MM/dd"));  // Output : 2013/08/14
        Response.Write("<BR>" + DateTime.Today.ToString("yyyy MM dd"));  // Output : 2013 08 14
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy-MM-dd h:m:s"));  // Output : 2013-08-14 5:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy/MM/dd h:m:s"));  // Output : 2013/08/14 5:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy MM dd h:m:s"));  // Output : 2013 08 14 5:12:30
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));  // Output : 2013-08-14 05:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"));  // Output : 2013/08/14 05:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy MM dd hh:mm:ss"));  // Output : 2013 08 14 05:12:30
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy-MM-dd H:m:s"));  // Output : 2013-08-14 17:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy/MM/dd H:m:s"));  // Output : 2013/08/14 17:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy MM dd H:m:s"));  // Output : 2013 08 14 17:12:30
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));  // Output : 2013-08-14 17:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));  // Output : 2013/08/14 17:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy MM dd HH:mm:ss"));  // Output : 2013 08 14 17:12:30
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss tt"));  // Output : 2013-08-14 17:12:30 PM
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss tt"));  // Output : 2013/08/14 17:12:30 PM
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy MM dd HH:mm:ss tt"));  // Output : 2013 08 14 17:12:30 PM

        Response.Write("<BR/>" + "------------------------");

        DateTime now = DateTime.Now;
        Response.Write("<BR/>" + now.ToString("d"));  // Output : 14-Aug-13
        Response.Write("<BR/>" + now.ToString("D"));  // Output : Wednesday, August 14, 2013
        Response.Write("<BR/>" + now.ToString("f"));  // Output : Wednesday, August 14, 2013 5:12 PM
        Response.Write("<BR/>" + now.ToString("F"));  // Output : Wednesday, August 14, 2013 5:12:30 PM
        Response.Write("<BR/>" + now.ToString("g"));  // Output : 14-Aug-13 5:12 PM
        Response.Write("<BR/>" + now.ToString("G"));  // Output : 14-Aug-13 5:12:30 PM
        Response.Write("<BR/>" + now.ToString("m"));  // Output : August 14
        Response.Write("<BR/>" + now.ToString("M"));  // Output : August 14
        Response.Write("<BR/>" + now.ToString("o"));  // Output : 2013-08-14T17:12:30.1324811+05:30
        Response.Write("<BR/>" + now.ToString("O"));  // Output : 2013-08-14T17:12:30.1324811+05:30
        Response.Write("<BR/>" + now.ToString("s"));  // Output : 2013-08-14T17:12:30
        Response.Write("<BR/>" + now.ToString("t"));  // Output : 5:12 PM
        Response.Write("<BR/>" + now.ToString("T"));  // Output : 5:12:30 PM
        Response.Write("<BR/>" + now.ToString("u"));  // Output : 2013-08-14 17:12:30Z
        Response.Write("<BR/>" + now.ToString("U"));  // Output : Wednesday, August 14, 2013 11:42:30 AM
        Response.Write("<BR/>" + now.ToString("y"));  // Output : August, 2013
        Response.Write("<BR/>" + now.ToString("Y"));  // Output : August, 2013

        Response.Write("<BR/>" + "------------------------");

        Response.Write("<BR/>" + now.ToLongDateString());  // Output : Wednesday, August 14, 2013
        Response.Write("<BR/>" + now.ToLongTimeString());  // Output : 5:12:30 PM
        Response.Write("<BR/>" + now.ToShortDateString());  // Output : 14-Aug-13
        Response.Write("<BR/>" + now.ToShortTimeString());  // Output : 5:12 PM
        Response.Write("<BR/>" + now.ToString());  // Output : 14-Aug-13 5:12:30 PM
    }

VB.Net Code to show/display list of different Date and Time formats

Protected Sub btnDateTime_Click(sender As Object, e As System.EventArgsHandlesbtnDateTime.Click
        Response.Write("<BR>" & DateTime.Today.ToString("dd-MM-yyyy")) ' Output : 14-08-2013
        Response.Write("<BR>" & DateTime.Today.ToString("dd/MM/yyyy")) ' Output : 14/08/2013
        Response.Write("<BR>" & DateTime.Today.ToString("dd MM yyyy")) ' Output : 14 08 2013
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("dd-MM-yy")) ' Output : 14-08-13
        Response.Write("<BR>" & DateTime.Today.ToString("dd/MM/yy")) ' Output : 14/08/13
        Response.Write("<BR>" & DateTime.Today.ToString("dd MM yy")) ' Output : 14 08 13
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("ddd-dd-MM-yy")) ' Output : Wed-14-08-13
        Response.Write("<BR>" & DateTime.Today.ToString("ddd/dd/MM/yy")) ' Output : Wed/14/08/13
        Response.Write("<BR>" & DateTime.Today.ToString("ddd dd MM yy")) ' Output : Wed 14 08 13
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("dddd-dd-MM-yy")) ' Output : Wednesday-14-08-13
        Response.Write("<BR>" & DateTime.Today.ToString("dddd/dd/MM/yy")) ' Output : Wednesday/14/08/13
        Response.Write("<BR>" & DateTime.Today.ToString("dddd dd MM yy")) ' Output : Wednesday 14 08 13
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("dd-MMM-yyyy")) ' Output : 14-Aug-2013
        Response.Write("<BR>" & DateTime.Today.ToString("dd/MMM/yyyy")) ' Output : 14/Aug/2013
        Response.Write("<BR>" & DateTime.Today.ToString("dd MMM yyyy")) ' Output : 14 Aug 2013
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("dd-MMMM-yyyy")) ' Output : 14-August-2013
        Response.Write("<BR>" & DateTime.Today.ToString("dd/MMMM/yyyy")) ' Output : 14/August/2013
        Response.Write("<BR>" & DateTime.Today.ToString("dd MMMM yyyy")) ' Output : 14 August 2013
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("MM-dd-yyyy")) ' Output : 08-14-2013
        Response.Write("<BR>" & DateTime.Today.ToString("MM/dd/yyyy")) ' Output : 08/14/2013
        Response.Write("<BR>" & DateTime.Today.ToString("MM dd yyyy")) ' Output : 08 14 2013
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("MMM-dd-yyyy")) ' Output : Aug-14-2013
        Response.Write("<BR>" & DateTime.Today.ToString("MMM/dd/yyyy")) ' Output : Aug/14/2013
        Response.Write("<BR>" & DateTime.Today.ToString("MMM dd yyyy")) ' Output : Aug 14 2013
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("yyyy-MM-dd")) ' Output : 2013-08-14
        Response.Write("<BR>" & DateTime.Today.ToString("yyyy/MM/dd")) ' Output : 2013/08/14
        Response.Write("<BR>" & DateTime.Today.ToString("yyyy MM dd")) ' Output : 2013 08 14
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy-MM-dd h:m:s")) ' Output : 2013-08-14 5:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy/MM/dd h:m:s")) ' Output : 2013/08/14 5:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy MM dd h:m:s")) ' Output : 2013 08 14 5:12:30
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")) ' Output : 2013-08-14 05:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")) ' Output : 2013/08/14 05:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy MM dd hh:mm:ss")) ' Output : 2013 08 14 05:12:30
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy-MM-dd H:m:s")) ' Output : 2013-08-14 17:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy/MM/dd H:m:s")) ' Output : 2013/08/14 17:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy MM dd H:m:s")) ' Output : 2013 08 14 17:12:30
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) ' Output : 2013-08-14 17:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")) ' Output : 2013/08/14 17:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy MM dd HH:mm:ss")) ' Output : 2013 08 14 17:12:30
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss tt")) ' Output : 2013-08-14 17:12:30 PM
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss tt")) ' Output : 2013/08/14 17:12:30 PM
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy MM dd HH:mm:ss tt")) ' Output : 2013 08 14 17:12:30 PM
        Response.Write("<BR/>" & "------------------------")

        Dim now As DateTime = DateTime.Now
        Response.Write("<BR/>" & now.ToString("d")) ' Output : 14-Aug-13
        Response.Write("<BR/>" & now.ToString("D")) ' Output : Wednesday, August 14, 2013
        Response.Write("<BR/>" & now.ToString("f")) ' Output : Wednesday, August 14, 2013 5:12 PM
        Response.Write("<BR/>" & now.ToString("F")) ' Output : Wednesday, August 14, 2013 5:12:30 PM
        Response.Write("<BR/>" & now.ToString("g")) ' Output : 14-Aug-13 5:12 PM
        Response.Write("<BR/>" & now.ToString("G")) ' Output : 14-Aug-13 5:12:30 PM
        Response.Write("<BR/>" & now.ToString("m")) ' Output : August 14
        Response.Write("<BR/>" & now.ToString("M")) ' Output : August 14
        Response.Write("<BR/>" & now.ToString("o")) ' Output : 2013-08-14T17:12:30.1324811+05:30
        Response.Write("<BR/>" & now.ToString("O")) ' Output : 2013-08-14T17:12:30.1324811+05:30
        Response.Write("<BR/>" & now.ToString("s")) ' Output : 2013-08-14T17:12:30
        Response.Write("<BR/>" & now.ToString("t")) ' Output : 5:12 PM
        Response.Write("<BR/>" & now.ToString("T")) ' Output : 5:12:30 PM
        Response.Write("<BR/>" & now.ToString("u")) ' Output : 2013-08-14 17:12:30Z
        Response.Write("<BR/>" & now.ToString("U")) ' Output : Wednesday, August 14, 2013 11:42:30 AM
        Response.Write("<BR/>" & now.ToString("y")) ' Output : August, 2013
        Response.Write("<BR/>" & now.ToString("Y")) ' Output : August, 2013
        Response.Write("<BR/>" & "------------------------")

        Response.Write("<BR/>" & now.ToLongDateString()) ' Output : Wednesday, August 14, 2013
        Response.Write("<BR/>" & now.ToLongTimeString()) ' Output : 5:12:30 PM
        Response.Write("<BR/>" & now.ToShortDateString()) ' Output : 14-Aug-13
        Response.Write("<BR/>" & now.ToShortTimeString()) ' Output : 5:12 PM
        Response.Write("<BR/>" & now.ToString()) ' Output : 14-Aug-13 5:12:30 PM
    End Sub



How to get IP address in Sql server.


SELECT SERVERPROPERTY('ComputerNamePhysicalNetBIOS') [Machine Name]
   ,SERVERPROPERTY('InstanceName') AS [Instance Name]
   ,LOCAL_NET_ADDRESS AS [IP Address Of SQL Server]
   ,CLIENT_NET_ADDRESS AS [IP Address Of Client]
 FROM SYS.DM_EXEC_CONNECTIONS
 WHERE SESSION_ID = @@SPID

Friday, 21 June 2013

How to add the Progress bar when u are Uplading a Video in asp.net

Programatically create buttons in code behind

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MultipleDynamicButtons.aspx.cs" Inherits="WebUI.Forums.MultipleDynamicButtons" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder runat="server" ID="place" /><br />
        <asp:Literal runat="server" ID="lit" />
    </div>
    </form>
</body>
</html>
 
 
 
.CS Page
 
 
using System;
using System.Web.UI.WebControls;

namespace WebUI.Forums
{
    public partial class MultipleDynamicButtons : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 3; i++)
   {
                Button btn = new Button();
                btn.ID = "Button" + i.ToString();
                btn.Text = "Test button" + i.ToString();
                btn.Click += new EventHandler(btn_Click);
                place.Controls.Add(btn);
   }
        }

        void btn_Click(object sender, EventArgs e)
        {
            lit.Text = string.Format("Button {0} was pressed.", ((Button)sender).ID);
        }
    }
} 

Friday, 14 June 2013

How to Bind the Gridview Vertically...

Hi All ,
I am mentioning the Code to bind the GrdiView in vartically...

<%@ Page Title="" Language="C#" MasterPageFile="~/master.master" AutoEventWireup="true" CodeFile="Testpage.aspx.cs" Inherits="Testpage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  
               
    <asp:GridView ID="GridView1" runat="server" ShowHeader="false" RowStyle-Wrap="true" Width="300px" GridLines="Both">

    </asp:GridView>
    
</asp:Content>


.CS page Code..
---------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class Testpage : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
        if (!Page.IsPostBack)
        {
            BindGridView();
        }
 }
    private DataTable PivotTable(DataTable origTable)
    {

        DataTable newTable = new DataTable();
        DataRow dr = null;
        //Add Columns to new Table
        for (int i = 0; i <= origTable.Rows.Count; i++)
        {
            newTable.Columns.Add(new DataColumn(origTable.Columns[i].ColumnName, typeof(String)));
        }

        //Execute the Pivot Method
        for (int cols = 0; cols < origTable.Columns.Count; cols++)
        {
            dr = newTable.NewRow();
            for (int rows = 0; rows < origTable.Rows.Count; rows++)
            {
                if (rows < origTable.Columns.Count)
                {
                    dr[0] = origTable.Columns[cols].ColumnName; // Add the Column Name in the first Column
                    dr[rows + 1] = origTable.Rows[rows][cols];
                }
            }
            newTable.Rows.Add(dr); //add the DataRow to the new Table rows collection
        }
        return newTable;
    }

private void BindGridView()
{
        try
        {
            DataTable dt = Library.ExecuteQuery("select * from VipUsersInfo where VID=2");
            if (dt.Rows.Count > 0)
            {
                //Bind the First GridView with the original data from the DataTable
                //GridView1.DataSource = dt;
                //GridView1.DataBind();

                //Pivot the Original data from the DataTable by calling the
                //method PivotTable and pass the dt as the parameter

                DataTable pivotedTable = PivotTable(dt);
                GridView1.DataSource = pivotedTable;
                GridView1.DataBind();
            }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Fetch Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
       
}


}


Thursday, 13 June 2013

How to get the Column list in sql srever

Hi ,


Try this ,You will get the Solution...........

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TableName' AND COLUMN_NAME not in('ColumnName1','ColumnName2')

Tuesday, 11 June 2013

How to Add strings of the Array in Single String


 string[] strarr = { opt1,opt2,opt3,opt4,opt5};
 for (int i = 0; i <= strarr.Length - 1; i++)
        {
            if (strarr[i] != "")
            {
                if (options == "")
                {
                    options = strarr[i];
                }
                else
                {
                    options += "," + strarr[i];
                }
            }
        }

Wednesday, 5 June 2013

Crop Image And Add water mark as a Image to image and Saved in Database and Filder also


Hello All,
I am mentioning here  the code to add watermark to the image Programmatically......You can use it.

  protected void btnupload_Click(object sender, EventArgs e)
    {
        String csName = "ButtonClickScript";
        Type csType = this.GetType();
        if (FileUpload1.HasFile)
        {
            try
            {
              
                string imageUrl = "~/UserImage/UserUploadImage/" + FileUpload1.PostedFile.FileName;
                //Fetching the image path of the Image that will be bind as Matermark
                string serverPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);
                string imageIconUrl = serverPath + "\\img\\watermarkimage.png";
  
                    //Get the location of the image
                    Image imagePhoto = Image.FromStream(FileUpload1.PostedFile.InputStream);

                    // Get dimensions of image
                    int imageHeight = imagePhoto.Height;
                    int imageWidth = imagePhoto.Width;

                    //Create a new Bitmap
                    Bitmap oBitmap = new Bitmap(imageWidth, imageHeight, PixelFormat.Format24bppRgb);

                    //Load Background Graphic from Image
                    Graphics oGraphics = Graphics.FromImage(oBitmap);
                    oGraphics.SmoothingMode = SmoothingMode.HighQuality;
                    oGraphics.DrawImage(imagePhoto, new Rectangle(0, 0, imageWidth, imageHeight), 0, 0, imageWidth, imageHeight, GraphicsUnit.Pixel);

                    //Layer 1: Add an Image Logo to the top left
                    if (!String.IsNullOrEmpty(imageIconUrl))
                    {
                        Image imageIcon = Image.FromFile(imageIconUrl);
                        oGraphics.DrawImage(imageIcon, new Rectangle(imageWidth-50, imageHeight-50, 35, 35), 0, 0, imageIcon.Width, imageIcon.Height, GraphicsUnit.Pixel);

                        imageIcon.Dispose();
                    }


   #region TO ADD THE TEXT AS WATER MARK
                    ////Layer 2: Add Comment
                    //if (!String.IsNullOrEmpty(imageComment))
                    //{
                    //    Font commentFont = new Font("Arial", 14, FontStyle.Regular); //Font Style
                    //    StringFormat commentFormat = new StringFormat();
                    //    commentFormat.Alignment = StringAlignment.Near; //Align text in left of layer

                    //    SolidBrush commentBrush = new SolidBrush(Color.Black); //Font Colour

                    //    oGraphics.FillRectangle(Brushes.Beige, 5, imageHeight - 55, imageWidth - 15, 50); //Create a rectangle with white background
                    //    oGraphics.DrawString(imageComment, commentFont, commentBrush, new Rectangle(5, imageHeight - 55, imageWidth - 15, 50), commentFormat); //Add comment text inside rectangle
                    //}

                    //Layer 3: Add Copyright watermark
                    //Font watermarkFont = new Font("Arial", 40, FontStyle.Bold); //Font Style
                    //SolidBrush semiTransBrush = new SolidBrush(Color.LightGray); //Font Colour
                    //StringFormat watermarkFormat = new StringFormat();
                    //watermarkFormat.Alignment = StringAlignment.Center; //Align text in center of image

                    //oGraphics.DrawString("Copyright",
                    //    watermarkFont,
                    //    semiTransBrush,
                    //    new PointF(imageWidth / 2, imageHeight / 2), watermarkFormat);
                    #endregion

                    //Dispose of graphic objects
                    imagePhoto.Dispose();
                    oGraphics.Dispose();

                    //Output image
                    string pageimageFolder = Server.MapPath(imageUrl);
                    if (File.Exists(pageimageFolder))
                    {
                        imageUrl = "";
                        imageUrl = "~/UserImage/" + DateTime.Now.Year + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + FileUpload1.FileName.ToString();
                        ////pageimageFolder = Server.MapPath(path);
                        oBitmap.Save(Server.MapPath(imageUrl));
                    }
                    oBitmap.Save(Server.MapPath(imageUrl));

                ClientScript.RegisterClientScriptBlock(csType, csName, "<script>alert('Image added successfully in the database and the folder!!!')</script>");
               
            }
            catch (Exception ex)
            {
                ClientScript.RegisterClientScriptBlock(csType, csName, "<script>alert('Some problem occured while adding the image!!! Please try again!!!')</script>");
                Response.Write(ex.Message);
            }
        }
    }