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

Display/Store and Retrieve Image Data from Database to Gridview, and also on Mouse Over

Rate me:
Please Sign up or sign in to vote.
4.44/5 (21 votes)
15 Oct 2011CPOL4 min read 128.1K   9.9K   50  
How to Display/Store and Retrieve Image Data from Database to Gridview, and also on mouse over
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
//using Algem.WebControls;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using Utils;

public partial class ThumbExpand : System.Web.UI.Page
{
    string sqlConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection conn;
    SqlCommand cmd;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void BindGrid()
    {
        List<User> lst = new List<User>();
        try
        {
            using (conn = new SqlConnection(sqlConnection))
            {
                conn.Open();

                StringBuilder sbQry = new StringBuilder();
                sbQry.Append("SELECT UserId, UserName,Password,LastName,");
                sbQry.Append("FirstName,MiddleName,WorksiteCode,AccessLevel,");
                sbQry.Append("Active,DateCreated,DateUpdated,Worksitedesc,Picture FROM dbo.[User]");
                using (cmd = new SqlCommand(sbQry.ToString(), conn))
                {
                    cmd.CommandType = CommandType.Text;

                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            User temp = new User();
                            temp.UserID = Tools.Tools.IifInt(dr["userID"]);
                            temp.Username = Tools.Tools.IifStr(dr["Username"]);
                            temp.Password = Tools.Tools.IifStr(dr["Password"]);
                            temp.LastName = Tools.Tools.IifStr(dr["LastName"]);
                            temp.FirstName = Tools.Tools.IifStr(dr["FirstName"]);
                            temp.MiddleName = Tools.Tools.IifStr(dr["MiddleName"]);
                            temp.WorksiteCode = Tools.Tools.IifStr(dr["WorksiteCode"]);
                            temp.AccessLevel = Tools.Tools.IifInt(dr["AccessLevel"]);
                            temp.Active = Tools.Tools.IifStr(dr["Active"]);
                            temp.DateCreated = Tools.Tools.IifDT(dr["DateCreated"]);
                            temp.DateUpdated = Tools.Tools.IifDT(dr["DateUpdated"]);
                            temp.WorksiteCode = Tools.Tools.IifStr(dr["Worksitedesc"]);
                            temp.Picture = ResizeImage(dr["Picture"], 500);
                            lst.Add(temp);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }        if (lst.Count > 0)
        {
            this.lblNoRecord.Visible = false;
        }
        else
        {
            this.lblNoRecord.Visible = true;
        }
        this.DataList1.DataSource = lst;
        this.DataList1.DataBind();
    }

    private System.Drawing.Image ResizeImage(object p, int targetSize)
    {
        using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream((byte[])p)))
        {
            Size newSize = CalculateDimensions(oldImage.Size, targetSize);
            using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
            {
                using (Graphics canvas = Graphics.FromImage(newImage))
                {
                    canvas.SmoothingMode = SmoothingMode.AntiAlias;
                    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
                    MemoryStream m = new MemoryStream();
                    newImage.Save(m, ImageFormat.Jpeg);

                    var imageFile = m;
                    var xx = System.Drawing.Image.FromStream(new MemoryStream(m.GetBuffer()));
                    System.Drawing.Image theImage = xx;
                    return theImage;

                }
            }
        }
    }


    private static Size CalculateDimensions(Size oldSize, int targetSize)
    {
        Size newSize = new Size();
        if (oldSize.Height > oldSize.Width)
        {
            newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height));
            newSize.Height = targetSize;
        }
        else
        {
            newSize.Width = targetSize;
            newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width));
        }
        return newSize;
    }


    protected void btnBack_Click(object sender, EventArgs e)
    {

        SessionManager.PageIndex = this.hfKeyId.Value;
        SessionManager.PageIndex = this.hfPageIndex.Value;
        Response.Redirect("~/Welcome.aspx");
    }
}

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
Software Developer (Senior) ***
Philippines Philippines
MCTS - Microsoft Certified Technology Specialist.
An Accountant.
Had been developed Payroll Accounting System Application
Live in: Quezon City, Metro Manila Philippines
Could reached at email address: ag_mojedo@live.com

Comments and Discussions