Click here to Skip to main content
15,886,258 members
Articles / Web Development / ASP.NET

Save image with printed text on it

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
25 Sep 2012CPOL2 min read 11.8K   121   2  
Create an ASP.NET page where the user can edit/modify text for an image and then save that image with printed text on it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadGrid();
        }
    }

    private void LoadGrid()
    {
        ImageCls cls = new ImageCls();
        DataTable dt = cls.GetImage();
        if (dt.Rows.Count > 0)
        {
            gvImage.DataSource = dt; gvImage.DataBind();
        }
        else
        {
            gvImage.DataSource = null;
            gvImage.DataBind();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        #region
        if (!(txt.Text.Equals("")))
        {
            lblImage.Text = txt.Text;
        }
        if (!(txtHeight.Text.Equals("")))
        {
            lblImage.Height = Convert.ToInt32(txtHeight.Text);
        }
        if (!(txtWidth.Text.Equals("")))
        {
            lblImage.Width = Convert.ToInt32(txtWidth.Text);
        }

        #endregion
        ImageCls cls = new ImageCls();
        Dictionary<string, string> ctrls = new Dictionary<string, string>();
        if (hdn.Value.Equals(""))
        {
            ctrls.Add("ID", "0");
            Guid ImageGuid = System.Guid.NewGuid();
            if (fuImage.HasFile)
            {
                #region
                string TargetFolder = Server.MapPath("images/");
                string ImageTempName = "Images." + Convert.ToString(ImageGuid) + "." + fuImage.FileName;
                fuImage.SaveAs(Server.MapPath("images/") + ImageTempName);
                tdImage.Attributes.Add("style", "background:url(images/" + ImageTempName + ")"); 
                ctrls.Add("ImageName", fuImage.FileName);
                ctrls.Add("ImageTempName", ImageTempName);
                #endregion
            }
            else
            {
                ctrls.Add("ImageName", "");
                ctrls.Add("ImageTempName", "");
            }
            #region
            ctrls.Add("ImageGUID", Convert.ToString(ImageGuid));
            ctrls.Add("ImageText", txt.Text);
            ctrls.Add("IsDeleted", "False");
            cls.SaveImage(ctrls);
            LoadGrid();
            #endregion
        }
        else if (!(hdn.Value.Equals("")))
        {
            ctrls.Add("ID", hdn.Value);
            Guid ImageGuid = System.Guid.NewGuid();
            if (fuImage.HasFile)
            {
                #region
                string TargetFolder = Server.MapPath("images/");
                string ImageTempName = "Images." + Convert.ToString(ImageGuid) + "." + fuImage.FileName;
                fuImage.SaveAs(Server.MapPath("images/") + ImageTempName);
                tdImage.Attributes.Add("style", "background:url(images/" + ImageTempName + ")"); 
                ctrls.Add("ImageName", fuImage.FileName);
                ctrls.Add("ImageTempName", ImageTempName);
                #endregion
            }
            else
            {
                ctrls.Add("ImageName", lblImageName.Text);
                ctrls.Add("ImageTempName", lblTempImageName.Text);
            }
            #region
            ctrls.Add("ImageGUID", Convert.ToString(ImageGuid));
            ctrls.Add("ImageText", txt.Text);
            ctrls.Add("IsDeleted", "False");
            cls.SaveImage(ctrls);
            LoadGrid();
            #endregion
        }


        ClearControls();
        btnSubmit.Text = "Submit";
        hdn.Value = string.Empty;
    }

    private void ClearControls()
    {
        txt.Text = string.Empty; txtHeight.Text = string.Empty; txtWidth.Text = string.Empty;
    }
    protected void gvImage_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label gvTempImageName = (Label)e.Row.FindControl("gvTempImageName");
            Image gvImg = (Image)e.Row.FindControl("gvImg");
            Label gvLblImageText = (Label)e.Row.FindControl("gvLblImageText");
            if (gvTempImageName != null)
            {
                if (!(gvTempImageName.Text.Equals("")))
                {
                    gvImg.ImageUrl = "~/images/" + gvTempImageName.Text;
                }
                else
                {
                    gvImg.ImageUrl = "~/images/noImage.jpg";
                }
            }
        }
    }
    protected void gvImage_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "edit")
        {
            Control ctrl = e.CommandSource as Control;
            GridViewRow _currenrtrow = ctrl.Parent.NamingContainer as GridViewRow;
            hdn.Value = Convert.ToString(e.CommandArgument);
            Label gvLblImageName = (Label)_currenrtrow.FindControl("gvLblImageName");
            Label gvTempImageName = (Label)_currenrtrow.FindControl("gvTempImageName");
            Label gvLblImageText = (Label)_currenrtrow.FindControl("gvLblImageText");
            lblImageName.Text = gvLblImageName.Text;
            lblTempImageName.Text = gvTempImageName.Text;
            txt.Text = gvLblImageText.Text;
            
            tdImage.Attributes.Add("style", "background:url(images/" + gvTempImageName.Text + ")");
            btnSubmit.Text = "Update";
        }
        else if (e.CommandName.ToLower() == "delete")
        {
            Control ctrl = e.CommandSource as Control;
            GridViewRow _currenrtrow = ctrl.Parent.NamingContainer as GridViewRow;
            ImageCls cls = new ImageCls();
            cls.DeleteImage(Convert.ToInt32(e.CommandArgument));
            LoadGrid();
        }
    }
    protected void gvImage_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void gvImage_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Essential Solve LLC
India India
I am flexible, responsive, creative and enthusiastic with ability to manage multiple initiatives with deadline. I have willingness to pick up and develop new skills and ability to balance a number of conflicting priorities and make decisions. I am results oriented - focused on productive and high-yield activities.

Comments and Discussions