2011-08-30 31 views
7

Quiero convertir una imagen a base64 y volver a la imagen de nuevo. Aquí está el código que probé hasta ahora y el error también. Alguna sugerencia por favor?Convertir una imagen a base64 y viceversa

public void Base64ToImage(string coded) 
{ 
    System.Drawing.Image finalImage; 
    MemoryStream ms = new MemoryStream(); 
    byte[] imageBytes = Convert.FromBase64String(coded); 
    ms.Read(imageBytes, 0, imageBytes.Length); 
    ms.Seek(0, SeekOrigin.Begin); 
    finalImage = System.Drawing.Image.FromStream(ms); 

    Response.ContentType = "image/jpeg"; 
    Response.AppendHeader("Content-Disposition", "attachment; filename=LeftCorner.jpg"); 
    finalImage.Save(Response.OutputStream, ImageFormat.Jpeg); 
} 

El error es:

Parameter is not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Parameter is not valid.

Source Error:

Line 34:    ms.Read(imageBytes, 0, imageBytes.Length); 
Line 35:    ms.Seek(0, SeekOrigin.Begin); 
Line 36:    finalImage = System.Drawing.Image.FromStream(ms); 
Line 37:   
Line 38:   Response.ContentType = "image/jpeg"; 

Source File: e:\Practice Projects\FaceDetection\Default.aspx.cs Line: 36

Respuesta

7

Estás leyendo de una corriente de vacío, en lugar de cargar los datos existentes (imageBytes) en la corriente. Proveedores:

byte[] imageBytes = Convert.FromBase64String(coded); 
using(var ms = new MemoryStream(imageBytes)) { 
    finalImage = System.Drawing.Image.FromStream(ms); 
} 

Además, debe intentar asegurar que finalImage está dispuesto; Yo propondría:

System.Drawing.Image finalImage = null; 
try { 
    // the existing code that may (or may not) successfully create an image 
    // and assign to finalImage 
} finally { 
    if(finalImage != null) finalImage.Dispose(); 
} 

Y, por último, señalar que System.Drawing is not supported en ASP.NET; YMMV.

Caution

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions. For a supported alternative, see Windows Imaging Components.

+0

Aún así conseguir el mismo error en la línea finalImage = System.Drawing.Image.FromStream (ms); –

2

El MemoryStream.Read Method lee bytes desde el MemoryStream en la matriz de bytes especificado.

Si desea escribir la matriz de bytes a la MemoryStream, utilice el MemoryStream.Write Method:

ms.Write(imageBytes, 0, imageBytes.Length); 
ms.Seek(0, SeekOrigin.Begin); 

Como alternativa, simplemente puede envolver la matriz de bytes en un MemoryStream:

MemoryStream ms = new MemoryStream(imageBytes); 
+0

Gracias por la respuesta, pero no funcionó! ¿Puedes sugerir alguna alternativa? –

Cuestiones relacionadas