发送来自Android的图像以ASP.NET Web服务 [英] Send an image from Android to an ASP.NET Web Service
问题描述
我开发一个Android应用程序,应该发送图像到我的ASP.NET Web服务在影像将被保存为一个文件。我见过一对夫妇的方法可以做到这一点,我就为这一个:将图像转换为字节数组 - >字节数组转换为字符串 - >使用KSOAP2发送字符串到Web服务 - >收到字符串在Web服务 - >将其转换为字节数组 - >将其保存为图像:
I'm developing an Android App that should send an image to my ASP.NET Web Service where the image will be saved as a file. I've seen a couple of ways to do this and I went for this one: convert the image to a byte array -> convert the byte array to a string -> send the string to the web service using KSOAP2 -> receive the String at the Web Service -> convert it to a byte array -> Save it as an image:
IVtest = (ImageView)findViewById(R.id.carticon);
BitmapDrawable drawable = (BitmapDrawable) IVtest.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
ImageView image=new ImageView(this);
image.setImageBitmap(bmp);
String strBase64 = Base64.encode(data);
然后我送strBase64到Web服务。 在Web Service我有这样的:
Then I send strBase64 to the web service. In the Web Service I have this:
public Image ConvertToImage(byte[] image)
{
MemoryStream ms = new MemoryStream(image);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
[WebMethod]
public String UploadImage(String image, String name)
{
byte[] image_byte = Encoding.Unicode.GetBytes(image);
Image convertedImage = ConvertToImage(image_byte);
try {
convertedImage.Save(Server.MapPath("generated_image.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
} catch (Exception e) {
return e.Message;
}
return "Success";
}
我得到一个错误,在这条线:图像returnImage = Image.FromStream(MS);
I get an error at this line: Image returnImage = Image.FromStream(ms);
这是我的错误:
的SOAPFault - 故障code:香皂:服务器在faultstring:System.Web.Services.Protocols.SoapException:服务器无法处理请求。 ---> System.ArgumentException:参数无效。 在System.Drawing.Image.FromStream(流流,布尔useEmbeddedColorManagement,布尔validateImageData) 在System.Drawing.Image.FromStream(流流) 在Service.ConvertToImage(字节[]图像)在E:\ FTP \ NVM \ Service.asmx:行1366 在Service.UploadImage(字符串形象,字符串名称)在E:\ FTP \ NVM \ Service.asmx:行1374 ---内部异常堆栈跟踪的结尾---faultactor:'空'的细节:org.kxml2.kdom.Node@437bf7b0
SoapFault - faultcode: 'soap:Server' faultstring: 'System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.ArgumentException: Parameter is not valid. at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) at System.Drawing.Image.FromStream(Stream stream) at Service.ConvertToImage(Byte[] image) in e:\FTP\nvm\Service.asmx:line 1366 at Service.UploadImage(String image, String name) in e:\FTP\nvm\Service.asmx:line 1374 --- End of inner exception stack trace ---' faultactor: 'null' detail: org.kxml2.kdom.Node@437bf7b0
感谢
推荐答案
好像有什么前途未卜从字符串的转换为图像。你也不能处理你的数据流,这将导致内存泄漏。
Seems like there is something iffy with your conversion from string to image. Also you are not disposing your stream which will leak memory.
试试这个来代替:
private Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
using (var ms = new MemoryStream(imageBytes, 0,
imageBytes.Length))
{
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
}
这篇关于发送来自Android的图像以ASP.NET Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!