Exibir foto do campo VarBinary(MAX) em ASP.Net
21/07/2010
0
Tenho um programinha, onde gravo o nome do funcionario e sua repectiva foto em MSSQLServer2005, campo FOTO VARBINARY(MAX), para gravar beleza, esta ok, o problema esta em exibir, pois meu chefe que que apareça em um controle Image do WebForm.
eu tenho um botão assim:
protected void LocalizaID() { lblMensagem.Text = "";
SqlCommand cmd = new SqlCommand("rv_ConsultaMembroID", new SqlConnection(ConfigurationManager.ConnectionStrings["revival_ConnectionString"].ConnectionString)); cmd.Connection.Open(); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@ID", txtID.Text));
SqlDataReader dr;
try {
dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { txtID.Text = dr["ID"].ToString(); if (dr["STATUS"] != null) { ckStatus.Checked = (bool)dr["STATUS"]; } txtDataAdmissao.Text = String.Format("{0:d}", Convert.ToDateTime(dr.GetDateTime(2).ToString())); txtNome.Text = dr["NOME"].ToString(); txtDataNasc.Text = String.Format("{0:d}", Convert.ToDateTime(dr.GetDateTime(5).ToString())); dpEstadoCivil.SelectedValue = dr["ID_ESTADOCIVIL"].ToString();
//Carrega Foto this.Foto.ImageUrl = "../Image.ashx?photoid=" + txtID.Text;
********************************************************Onde Image.ashx é uma página Handler com esse conteúdo :/// <summary> /// Summary description for Handler1 /// </summary> public class Handler1 : IHttpHandler {
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpeg"; context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.BufferOutput = false;
Int32 id = -1; Stream stream = null;
if (context.Request.QueryString["photoid"] != null && context.Request.QueryString["photoid"] != "") { id = Convert.ToInt32(context.Request.QueryString["photoid"]);
stream = clsPhoto.photo(id); const int buffersize = 1024 * 16; byte[] buffer = new byte[buffersize]; int count = stream.Read(buffer, 0, buffersize); while (count > 0) { context.Response.OutputStream.Write(buffer, 0, count); count = stream.Read(buffer, 0, buffersize); } }
}
public bool IsReusable { get { return false; } }
Porem na minha máquina aparece, no site não aparece, mesmo colocando todas as permissões possíveis. Será que não teria uma maneira mais atual de recuperar essas imagens pois, uso Visual Studio 2010. Grato
Paulo Freire
Posts
22/07/2010
Netasper
this.Foto.ImageUrl = "../Image.ashx?photoid=" + txtID.Text;
Tenta usar o alias do servidor:
this.Foto.ImageUrl = "http://nomedoservidor/aplicacao/Image.ashx?photoid=" + txtID.Text;
[]s
26/07/2010
Paulo Freire
Veja imagem:
28/07/2010
Paulo Freire
Clique aqui para fazer login e interagir na Comunidade :)