如何从webservice返回字节列表

时间:2014-06-09 07:36:59

标签: web-services wcf byte

我想从数据库中检索以字节形式存储的图像列表。我只能返回单个图像字节长度。如何发送字节列表。我使用下面的代码请告诉我< / p>

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




        }

2 个答案:

答案 0 :(得分:2)

我尝试使用图像服务创建服务,该服务从存储在与服务相同的文件夹中的图像返回图像。如果你愿意,你可以稍后播放。

  [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();
    }
}

在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>

现在您可以创建客户端代理并将图像保存回来

 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;
    }

答案 1 :(得分:0)

非常想要返回图像。无论您如何看待以下内容:Large Data and Streaming