On September 20, 2
Inhibiting P2X4 re
--- abstract: 'The
C. V. Meenakshi Su
A few days ago, we
On the latest Mari
The invention rela
An evaluation of t
Ads Sunday, Augus
When: Friday, Janu

This is a new appl
The World Trade Or
Recommended Readin
The world's only r
Penn State junior
Q: How do I chang
Invasive Species
In many parts of t
1. Field of the In
/* SPDX-License-Id
Q: WCF, is it possible to send byte[] in XML instead of image/jpg (using MTOM)? I have a WCF service that receives (by a web service) and uploads an image. The problem is that sometimes the client needs to receive a very large image. Is there any way to transfer the large image using a byte[] or somthing? I saw there is an MTOM option but this only works if you use it to transfer an image, and I don't want to. I would like to send a byte[], is there anyway to do this? Thanks! A: Well, the MTOM solution could also accept byte arrays, but it would use Base64 encoding of it. There are plenty of tools to do this conversion for you. Even this encoder is only 100KB. So, yes, there is a way to do this, you'd have to use a tool to do this. A: This blog post describes an example of encoding a byte[] to a Bitmap and writing it to an output stream. You could try using a MemoryStream for writing the result to your web service. I haven't tried it yet, but maybe it works. A: Just some more info, it seems that in the case of the large image the MTOM content is converted to Base64. MTOM, Multi-part, Optimised Transfer Only supports Binary and ImageMime Formats. In my specific case, using WCF with a client application written in .Net 3.5, the following code converts a image in binary to an Byte array. Then the BinaryArray is converted to a MemoryStream (MTOM) and finally to a Base64 representation of the Image: using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Web; using System.Web.Script.Serialization; namespace WcfService1 { [ServiceContract(Namespace = "WcfService1")] public interface IService1 { [OperationContract] string PostBinaryData(BinaryArray binaryArray); } [Serializable] [DataContract] public class BinaryArray { private int _size; [DataMember] public int Size { get { return _size; } set { _size = value; } } [DataMember] public byte[] Data { get; set; } [DataMember] public string MimeType; public BinaryArray(byte[] data, string mimeType) { Data = data; MimeType = mimeType; _size = data.Length; } public BinaryArray() { } } [Serializable] public class ImageMime : MultipartFormDataStreamProvider { public ImageMime() : base(new Dictionary()) { } public override string GetLocalFileName(string fileName) { return GetUniqueFileName("Image.png"); } } [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)] [ServiceContract] public interface IService1 { [OperationContract] string PostBinaryData(BinaryArray binaryArray); } public class Service1 : IService1 { public string PostBinaryData(BinaryArray binaryArray) { if (binaryArray == null || binaryArray.Data == null) return "1"; BinaryFormatter binaryFormatter = new BinaryFormatter(); MemoryStream binaryStream = new MemoryStream(); binaryFormatter.Serialize(binaryStream, binaryArray); var memoryStreamClient = new MemoryStream(); binaryStream.Position = 0; binaryStream.CopyTo(memoryStreamClient); var imageData = new Image(); imageData.Load(memoryStreamClient); return imageData.ToString(); } } } And this is the Client code: public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client(); String img = sc.PostBinaryData(new BinaryArray(stream)); } } Note the usage of the MultiPartFormDataStreamProvider from here: http://msdn.microsoft.com/en-us/library/system.web.httprequest.multipartformdatastreamprovider.aspx Using this helper allows to have a clear structure and support for chunking the data in an easier way. I hope this will help someone that needs the same. A: The short answer is that you cannot send the bytes of the image in an MTOM format without sending the bytes of the image. There is not even a separate way to send binary content. I have tried sending just the content type of the binary data and that didn't work. An MTOM representation requires a separate message header along with the bytes you are sending that would describe the MIME representation of the data so that when a client receives it it knows what to do with it. MTOM, Multi-part, Optimised, Binary Only supports Binary and ImageMime Formats. This is a screenshot from a SO question for more clarity. To answer your second question, you do not need to send the full content of the image in the request. For example I will illustrate a case of sending just the header and some information and then the bytes of the image content itself. You may want to use different fields for this example, and perhaps different byte formats and I used binary serialization here to make it easy. Sending just the header, there is a separate header for every part of the message you want to send with this one header. I could just use BinaryFormatter and send the header, but I use protobuf-net as it is more efficient and is easier to maintain. I am just showing this to illustrate the concept of sharing data between clients and servers. The key to that is the MessageHeader property