Ver Mensaje Individual
  #3 (permalink)  
Antiguo 23/10/2009, 14:15
Avatar de AWesker
AWesker
 
Fecha de Ingreso: octubre-2008
Mensajes: 177
Antigüedad: 16 años, 3 meses
Puntos: 27
Posible solución

Qué tal de nuevo!

Logré convertir los bytes a imagen y asignarla al control <asp:image>. Aquí dejo el código (completo ) por si a alguien le interesa .

//Utilizo el un IHttpHandler que procesa de la solicitud al mismo tiempo que selecciono el elemento del cual voy a presentar la imagen

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.IO;

Código:
namespace Master
{
    public class HImage : IHttpHandler
    {
        
        public void ProcessRequest(HttpContext context)
        {
             //Valido que el Id no sea nulo
             if(context.Request.QueryString["Id"] != null)
            {
                Convierto a entero el valor del Request.QueryString
                Int32 empno = Convert.ToInt32(context.Request.QueryString["Id"]);

                //Ejecuto la funcion Bytes() cuyo parámetro es el Id de la Imagen o cualquier identificador
                Stream strm = Bytes(empno);
                //Creo un variable byte[] y le asigno el valor [4096]
                byte[] buffer = new byte[4096];
                int byteSeq = strm.Read(buffer, 0, 4096);
 
                //Lazo que me se ejecuta mientras que byteSeq  sea mayor a 0
                while (byteSeq > 0)
                {
                    context.Response.OutputStream.Write(buffer, 0, byteSeq);
                    byteSeq = strm.Read(buffer, 0, 4096);
                }  
            }
        }

        //Aqui obtengo los bytes desde la Base de Datos por medio de LINQ
        private Stream Bytes(Int32 empno)
        {
            var Parametro = (from Para in MiDataContext.Table_Parametros
                             where empno == Para.Id_Parametro
                             select Para).Single();
            byte[] Img = (Parametro.ImagenParametro).ToArray();
            if (Img != null)
            {
                return new MemoryStream((byte[])Img);
            }
            else
            {
                return null;
            }
        }

        //Codigo que crea el IHttpHandler automaticamente
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
Lo olvidaba... para asignar la propiedad ImageUrl al control <asp:imge> se utiliza esto:
Código:
//Imagenes.ashx es el nombre del IHttpHandler.
Img_Parametro.ImageUrl = "~/Imagenes.ashx?Id="+Convert.ToInt32(MiGV.SelectedDataKey.Value).ToString();
Lógicamente, se puede hacer de formas más efectivas, este código es un poco superficial, pero funciona bien.

Solo era cuestion de investigar, analizar e intentar. Bien, caso cerrado.

Última edición por AWesker; 23/10/2009 a las 14:20