Using Base64String:
Soap Web Service Method:-
[WebMethod]
public string UploadImage(string imgBase64String, string fname)
{
try
{
//Converts the base64String to byte array
byte[] imageBytes = Convert.FromBase64String(imgBase64String);
//store it in a memory stream
MemoryStream ms1 = new MemoryStream(imageBytes);
//generate image from stream
Image img = Image.FromStream(ms1);
//Map the folder to store image
string actFolder = Server.MapPath("~/uFiles/");
//save the image
img.Save(actFolder + fname);
return "OK";
}
catch (Exception e)
{
return e.Message;
}
}
Sample Win application code which consumes above service
// don't forget to add the web reference before using this code.
//here the reference name is localhost and service name is Service 1
string base64;
using (MemoryStream ms = new MemoryStream())
{
Image ximg = Image.FromFile(<<filepath>>);
ximg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
base64 = Convert.ToBase64String(ms.ToArray());
localhost.Service1 S1 = new localhost.Service1();
string r = S1.UploadImage(base64, "MyFile.Jpg");
MessageBox.Show(r);
}
No comments:
Post a Comment