You must Sign In to post a response.

Using wcf how to upload a image

using wcf/wcf web services to upload a images give me with example?


#676207    Author: Gaurav Agrawal    19 Jun 2012 Member Level: Gold   Points : 3

The easiest way is to convert the image to a byte array before sending it, and then converting it back to an image on the destination site.

Here are two methods for doing just that:

public byte[] ImageToByteArray( Image image)
var ms = new MemoryStream();
image.Save(ms, ImageFormat.Png);
return ms.ToArray();

public static Image ByteArrayToImage(byte[] byteArray)
var ms = new MemoryStream(byteArray);
return Image.FromStream(ms);
That means your web service can have a method something like this:

public void UploadImage( byte[] imageData )
var image = ByteArrayToImage( imageData );
//save image to disk here, or do whatever you like with it

Thanks & Regards,
Gaurav Agrawal

#676807    Author: Ajesh Madhukar Dalvi    22 Jun 2012 Member Level: Silver   Points : 4

[WebInvoke(UriTemplate = "uploadImage/{parameter1}/{parameter2}")]
void uploadImage(Stream fileStream, string fileName);


public void uploadImage(Stream fileStream, string fileName)
string filePath = @"C:\ImageUpload\";
using (FileStream filetoUpload = new FileStream(filePath + fileName, FileMode.Create))
byte[] byteArray = new byte[10000];
int bytesRead = 0;

bytesRead = fileStream.Read(byteArray, 0, byteArray.Length);
if (bytesRead > 0)
filetoUpload.Write(byteArray, 0, bytesRead);

while (bytesRead > 0);

and your client side as such:

protected void btnUpload_Click(object sender, EventArgs e)
if (FileUpload1.HasFile)
RESTService1Client client = new RESTService1Client();

client.uploadImage(FileUpload1.FileContent, Path.GetFileName(FileUpload1.FileName));

This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.