2011-08-30 18 views
7

मैं एक छवि को बेस 64 में परिवर्तित करना चाहता हूं और फिर छवि पर वापस जाना चाहता हूं। यहां वह कोड है जिसे मैंने अभी तक कोशिश की और त्रुटि भी। कोई सुझाव कृपया?एक छवि को बेस 64 में बदलें और इसके विपरीत

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); 
} 

त्रुटि है:

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

उत्तर

7

आप एक खाली धारा से पढ़ना, बल्कि मौजूदा डेटा (imageBytes) धारा में लोड हो रहा है की तुलना में कर रहे हैं। प्रयास करें:

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

इसके अलावा, आप यह सुनिश्चित करें कि finalImage निपटान किया जाता है प्रयास करना चाहिए; मेरा प्रस्ताव है जाएगा:

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(); 
} 

और अंत में, कि System.Drawing is not supported ध्यान दें 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

फिर भी एक ही हो रही है लाइन अंतिम त्रुटि में त्रुटि = System.Drawing.Image.FromStream (एमएस); –

2

MemoryStream.Read Method निर्दिष्ट बाइट सरणी में MemoryStream से बाइट्स पढ़ता है।

आप, MemoryStream को बाइट सरणी लिखने के लिए चाहते हैं MemoryStream.Write Method का उपयोग करें: वैकल्पिक रूप से

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

, तो आप बस एक MemoryStream में बाइट सरणी लपेट कर सकते हैं:

MemoryStream ms = new MemoryStream(imageBytes); 
+0

उत्तर के लिए धन्यवाद, लेकिन काम नहीं किया! क्या आप कृपया किसी भी विकल्प का सुझाव दे सकते हैं? –

संबंधित मुद्दे