user3386790
user3386790

Reputation: 166

How to Return list of bytes from webservice

I want to retrieve list of images from database that are stored in the form of bytes.I am able to return only single images bytes length.How can i send list of bytes .i am using below code please let me know

public List<Byte[]> GetAllProjectStandardIcons()
        {

                var qry = (from p in dbModel.tbl_STANDARDPROJECTICONS
                           select new
                           {

                               p.ProjectIcons

                           }).ToList();


//How to return list here from WCF web service




        }

Upvotes: 1

Views: 1143

Answers (2)

Avneesh
Avneesh

Reputation: 654

I have tried to create a service with an Image Service that returns images from the images stored in the same folder as the service. You can the streaming later if you want.

  [ServiceContract]
public interface IImagesService
{
    [OperationContract]
    List<Byte[]> FetchImages();
}
public class ImagesService : IImagesService
{
    List<string> images = new List<string>();
    public ImagesService()
    {
        images.Add("Box.png");
        images.Add("Clock.png");
    }

    public List<byte[]> FetchImages()
    {
        List<Byte[]> imagesInBytes = new List<byte[]>();
        foreach (var image in images)
        {
            Image newImage = new Bitmap(image);
            byte[] b = this.imageToByteArray(newImage);
            imagesInBytes.Add(b);
        }
        return imagesInBytes;
    }
    public byte[] imageToByteArray(System.Drawing.Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        return ms.ToArray();
    }
}

Hosted the service on httpbinding

  <system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="NewBehavior0">
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service name="WCFImagesExample.ImagesService" behaviorConfiguration="NewBehavior0">
            <endpoint address="Images" binding="basicHttpBinding" bindingConfiguration=""
                contract="WCFImagesExample.IImagesService" />
            <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
                contract="IMetadataExchange" />
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:{portnumber}" />
                </baseAddresses>
            </host>
        </service>
    </services>
</system.serviceModel>

Now you can create a client proxy and save the images back

 static void Main(string[] args)
    {
        ImagesReference.IImagesService imagesService = new ImagesServiceClient();
        byte[][] bytes = imagesService.FetchImages();
           int i=0;
        foreach (byte[] byteArray in bytes)
        {
            Image image = byteArrayToImage(byteArray);
            image.Save(@"c:\Development\" + i + ".png");
            i++;
        }
    }
    public static Image byteArrayToImage(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms);
        return returnImage;
    }

Upvotes: 2

MHOOS
MHOOS

Reputation: 5306

Highly like you want to return an image. No matter what you can have a look at the following:Large Data and Streaming

Upvotes: 0

Related Questions