简体   繁体   English

将图像从Android发送到ASP.NET Web服务

[英]Send an image from Android to an ASP.NET Web Service

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. 我正在开发一个Android应用程序,该应用程序应将图像发送到我的ASP.NET Web服务,图像将在其中保存为文件。 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: 我已经看到了几种方法来做到这一点,我去了:将图像转换为字节数组->将字节数组转换为字符串->使用KSOAP2将字符串发送到Web服务->接收字符串在Web服务->将其转换为字节数组-> 将其另存为图像:

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

Then I send strBase64 to the web service. 然后,我将strBase64发送到Web服务。 In the Web Service I have this: 在Web服务中,我有以下内容:

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

I get an error at this line : Image returnImage = Image.FromStream(ms); 我在这一收到一个错误: Image returnImage = Image.FromStream(ms);

This is the error I get: 这是我得到的错误:

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

Thanks 谢谢

Seems like there is something iffy with your conversion from string to image. 从字符串到图像的转换似乎有些困难。 Also you are not disposing your stream which will leak memory. 另外,您不打算处置将泄漏内存的流。

Try this instead: 尝试以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM