Foros del Web » Programación para mayores de 30 ;) » .NET »

mostrar foto desde sql a un picturebox

Estas en el tema de mostrar foto desde sql a un picturebox en el foro de .NET en Foros del Web. hola, cuando programo el boton consultar usuario me sale este error NO SE PUEDE CONVERTIR IMPLICITAMENTE EL TIPO 'SYSTEM.DATA.LINQ.BINARY' EN 'SYSTEM.DRAWING.IMAGE', yo trabajo con linq ...
  #1 (permalink)  
Antiguo 20/09/2010, 13:28
 
Fecha de Ingreso: octubre-2009
Mensajes: 23
Antigüedad: 14 años, 6 meses
Puntos: 0
Pregunta mostrar foto desde sql a un picturebox

hola, cuando programo el boton consultar usuario me sale este error

NO SE PUEDE CONVERTIR IMPLICITAMENTE EL TIPO 'SYSTEM.DATA.LINQ.BINARY' EN
'SYSTEM.DRAWING.IMAGE', yo trabajo con linq to sql y en capas, cual es el codigo de la conversion? aqui les dejo el codigo
using Power_Gym_2010_Negocio;
using Power_Gym_2010_Entidades;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data.Linq;
boton consultar
Usuario ousuario = new Usuario();
Registro_Usuario_Negocio blregistro_usuario = new Registro_Usuario_Negocio();
ousuario = blregistro_usuario.ConsultarRegistro_Usuario(txtce dula.Text);
if (ousuario == null)
{
MessageBox.Show("error");
}
else
{
txtnombre.Text = ousuario.Nombre;
txtapellido.Text = ousuario.Apellido;
dtpfecha_nacimiento.Value = Convert.ToDateTime(ousuario.Fecha_Nacimiento);
txtedad.Text = ousuario.Edad;
txtsexo.Text = ousuario.Sexo;
txtdireccion.Text = ousuario.Direccion;
txtelefono.Text = Convert.ToString(ousuario.Telefono);
txtocupacion.Text = ousuario.Ocupacion;
txtemail.Text = ousuario.Email;
pictureBox1.Image = ousuario.Foto; aqui me muestra el mensaje anterior

gracias
  #2 (permalink)  
Antiguo 20/09/2010, 14:07
Avatar de gedarufi  
Fecha de Ingreso: diciembre-2008
Ubicación: Colombia
Mensajes: 540
Antigüedad: 15 años, 4 meses
Puntos: 22
Respuesta: mostrar foto desde sql a un picturebox

Yo lo que hago es escribir la variable Byte[] donde esta la imagen en un MemoryStream y luego ese Stream lo leo en un Objeto Bitmap.

Te pongo un ejemplo que tengo a mano, se encarga de descomprimir un archivo que esta en la DB

Código C#:
Ver original
  1. public virtual System.IO.MemoryStream GetLayout()
  2.         {
  3.             System.IO.MemoryStream sw = new System.IO.MemoryStream();
  4.  
  5.             using (System.IO.MemoryStream ms = new System.IO.MemoryStream(this.Plantilla))
  6.             {
  7.                 using (System.IO.Compression.GZipStream gzipStream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
  8.                 {
  9.                     int buffer = 1000000;
  10.                     int offset = 0;
  11.                     int totalBytesRead = 0;
  12.                     Byte[] data = new Byte[buffer];
  13.                     bool ft = true;
  14.  
  15.                     while (true)
  16.                     {
  17.                         int bytesRead = gzipStream.Read(data, offset, buffer);
  18.  
  19.                         if (ft == true && bytesRead == 0)
  20.                         {
  21.                             bytesRead = gzipStream.Read(data, offset, buffer);
  22.                         }
  23.  
  24.                         Byte[] tempData = null;
  25.  
  26.                         ft = false;
  27.  
  28.                         if (bytesRead == 0)
  29.                         {
  30.                             tempData = data;
  31.                             data = new Byte[totalBytesRead];
  32.  
  33.                             for (int i = 0; i < totalBytesRead; i++)
  34.                             {
  35.                                 data[i] = tempData[i];
  36.                             }
  37.  
  38.                             break;
  39.                         }
  40.  
  41.                         tempData = data;
  42.                         data = new Byte[data.Length + bytesRead];
  43.  
  44.                         tempData.CopyTo(data, 0);
  45.  
  46.                         offset += bytesRead;
  47.                         totalBytesRead += bytesRead;
  48.                     }
  49.  
  50.                     sw.Write(data, 0, data.Length);
  51.  
  52.                     return sw;                        
  53.                 }
  54.             }
  55.         }

este metodo retorna una variable que luego la puedes usar con la clase Bitmap.

Saludos

Etiquetas: foto, picturebox, sql
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:04.