.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

how to retreive and show image in image control in asp.net,c#

Posted By: shweta     Posted Date: August 20, 2010    Points:2   Category :ASP.Net
Hi All,
I am saving an image into database and when i try to retreive and show it into image control..image is not displayed.My code is..


for diplaying image I am using image control:


Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;

<%@ WebHandler Language="C#" Class="ShowImage" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.IO;
using System.Data;
public class ShowImage : IHttpHandler
{
string query = null;

SqlConnection con;
SqlCommand cmd = null;
Int32 appno;

public void ProcessRequest (HttpContext context) {

if (context.Request.QueryString["id"] != null)
appno = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");

context.Response.ContentType = "image/jpeg";
Stream strm = ShowEmpImage(appno);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);

while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}

}

public Stream ShowEmpImage(int appno)
{
con = Class1.GetConnection();


query = "SELECT Image FROM tbl_App_Image WHERE ID = @ID";
cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", appno);
con.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
con.Close();
}
}

public bool IsReusable
{
get
{
return false;
}
}

}

kindly reply soon its very urgent.
regards,
shweta

Responses
Author: Nikhil Kumar             
Posted Date: August 21, 2010     Points: 5   

http://forums.asp.net/t/1438666.aspx

http://www.eggheadcafe.com/community/aspnet/2/10081584/image-display-on-imgae-control-using-fileupload-in-aspnet.aspx

Be helpful and enjoy life because it comes only onces. Give all the happiness to yours and remember it doesn't comes by money ...


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend