我想使用.NET webservices上传图像,并且将从iphone图像调用。
iphone发给我的文字格式是这样的。
http://d8768157.u118.c6.ixwebhosting.com/iphoneimg/image.txt
我必须在哪种数据类型中转换此数据,然后将其保存为图像格式。
如果您有任何其他方法,请告诉我。
我已尝试在byte []中转换此数据,但它给了我错误。这是我的代码。对于我所尝试的。请帮帮我。
[WebMethod]
public XmlDocument testuploadimage(string image)
{
XmlDocument login = new XmlDocument();
XmlDeclaration dec = login.CreateXmlDeclaration("1.0", null, null);
login.AppendChild(dec);
XmlElement root = login.CreateElement("CreateUser");
login.AppendChild(root);
try
{
string actFolder = Server.MapPath("~/iphoneimg/");
string imgname = DateTime.UtcNow.ToString().Replace(" ", "").Replace("AM", "").Replace("PM", "").Replace("/", "").Replace("-", "").Replace(":", "") + ".png";
Bitmap map;
using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(image)))
using (FileStream fs = File.Create(actFolder + imgname))
{
map = (Bitmap)Image.FromStream(stream);
map.Save(fs, System.Drawing.Imaging.ImageFormat.Gif);
}
XmlElement root1 = login.CreateElement("uploaded");
root1.InnerText = "true";
root.AppendChild(root1);
XmlElement root2 = login.CreateElement("path");
root2.InnerText = "http://d8768157.u118.c6.ixwebhosting.com/iphoneimg/" + imgname;
root.AppendChild(root2);
return login;
}
catch (Exception ex)
{
throw ex;
}
}
这是我得到的错误
Base-64字符串中的字符无效。
由于
BHAVIK GOYAL
答案 0 :(得分:1)
[WebMethod]
public XmlDocument testuploadimage(string image)
{
XmlDocument login = new XmlDocument();
XmlDeclaration dec = login.CreateXmlDeclaration("1.0", null, null);
login.AppendChild(dec);
XmlElement root = login.CreateElement("CreateUser");
login.AppendChild(root);
try
{
string actFolder = Server.MapPath("~/iphoneimg/");
string imgname = DateTime.UtcNow.ToString().Replace(" ", "").Replace("AM", "").Replace("PM", "").Replace("/", "").Replace("-", "").Replace(":", "") + ".Png";
byte[] imageBytes = Convert.FromBase64String(image.Replace(" ","+"));
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image2 = System.Drawing.Image.FromStream(ms, true);
image2.Save(actFolder + imgname);
XmlElement root1 = login.CreateElement("uploaded");
root1.InnerText = "true";
root.AppendChild(root1);
XmlElement root2 = login.CreateElement("path");
root2.InnerText = "http://d8768157.u118.c6.ixwebhosting.com/iphoneimg/" + imgname;
root.AppendChild(root2);
return login;
}
catch (Exception ex)
{
throw ex;
}
}
我得到了答案......
感谢所有人......
答案 1 :(得分:0)