
29/06/2011, 11:50
|
 | | | Fecha de Ingreso: noviembre-2009 Ubicación: MVD
Mensajes: 18
Antigüedad: 15 años, 3 meses Puntos: 1 | |
Respuesta: Insertar y cargar imágenes en bd Cita:
Iniciado por CrazyGrungeMan Mira yo uso lo siguiente para convertir imágenes y guardar en la base de datos pero siempre y cuando las imágenes no seas demasiado grandes. Pd: esto es para winforms, pero importando System.IO; debería funcionar (creo)
public static byte[] ImageToBytes(Image img)
{
try
{
string sTemp = Path.GetTempFileName();
FileStream fs = new FileStream(sTemp, FileMode.OpenOrCreate, FileAccess.ReadWrite);
img.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
fs.Position = 0;
//
int imgLength = Convert.ToInt32(fs.Length);
byte[] bytes = new byte[imgLength];
fs.Read(bytes, 0, imgLength);
fs.Close();
return bytes;
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
public static Image BytesToImage(byte[] bytes)
{
if (bytes == null) return null;
//
MemoryStream ms = new MemoryStream(bytes);
Bitmap bm = null;
try
{
bm = new Bitmap(ms);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
return bm;
} No vayas a usar ApplicationException que se parte todo XD usa un label para mostrar error mejor XD |