I'm retrieving the image display by using image hadler and show the image in Razor view...
*Image Display Using Handler From BackEnd*
Model Classe:
ImageData.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVC3HandlerImageDisplay.Models
{
public class ImageData
{
public int Id { get; set; }
}
}
Cerate a ImageHandlerClass in model Folder
ImageHandler.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
namespace MVC3HandlerImageDisplay.Models
{
/// <summary>
/// Summary description for ImageHandler
/// </summary>
public class ImageHandler : IHttpHandler
{
const string connect = @"Data Source=JAGAN-PC;Initial Catalog=MVC3;Integrated Security=True";
public void ProcessRequest(HttpContext context)
{
try
{
SqlConnection con = new SqlConnection(connect);
con.Open();
string imageid = HttpContext.Current.Request.QueryString["Photo_ID"].ToString();
SqlCommand cmd = new SqlCommand("select filecontent from filestore where id='" + imageid + "'", con);
byte[] image_byte = (byte[])cmd.ExecuteScalar();
con.Close();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite((Byte[])image_byte);
context.Response.End();
}
catch
{
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Craete A Table in Backend:
Id int
MimeType varchar(50)
FileName Varchar(50)
FileContent varbinary(MAX)
Insert the Image(filecontent) value as a varbinary formate.
if u not insert it won’t display any images..
Controller Class:
ImageDataController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC3HandlerImageDisplay.Models;
namespace MVC3HandlerImageDisplay.Controllers
{
public class ImageDataController : Controller
{
//
// GET: /ImageData/
public ActionResult Display()
{
ImageData img = new ImageData();
img.Id = 6;
return View(img);
}
}
}
Views:
Display.cshtml
@{
ViewBag.Title = "Display";
}
<h2>Display Particular Image Using Handler From Backend</h2>
<img src = "../../Models/ImageHandler.ashx?Photo_ID=@Model.Id" width = "400 px " height = "500px "id = "im"/>
No comments:
Post a Comment