Uwp从表SQL显示图像

时间:2016-07-04 19:03:15

标签: c# sql wcf uwp

我有一个函数在我的wcf服务上它返回Image作为字节 我没有找到任何关于如何在我的图像控制中显示它的想法 如何将这些字节转换为图像并谢谢!

这是我的WCF功能:

    public List<Data.Product> Show_P()
    {

        DataTable Table = new DataTable();
        List<Data.Product> MyProductsLIST = new List<Data.Product>();
        Table = Sp.SelectData("Show_Products", null);

        if (Table.Rows.Count > 0)
                for (int i = 0; i < Table.Rows.Count; i++)
                {
                    Data.Product Log = new Data.Product();

                    Log._ID = Convert.ToInt32(Table.Rows[i]["ID"]);
                    Log._Name = Table.Rows[i]["Name"].ToString();
                    Log._Price = Table.Rows[i]["Price"].ToString();
                    Log._Qte = Table.Rows[i]["Qte"].ToString();
                    Log._CAT = Convert.ToInt32(Table.Rows[i]["Cat"]);
                    Log._Vote = Convert.ToInt32(Table.Rows[i]["Vote"]);
                    Log._Image = (byte[])Table.Rows[i]["Image"];

                    MyProductsLIST.Add(Log);
                }


                return MyProductsLIST;

    }

在我的uwp中:

public  static List<Products> Get_Products()
{
    MspService.Service1Client Serv = new MspService.Service1Client();
    MspService.Product[] Product = Serv.Show_PAsync().Result.Show_PResult;
    List<Products> Produc = new List<Design_TesTiNg.MenuBox.Products>();

    for (int i = 0; i < Product.Length; i++)
    {
        Products Prod = new Products();
        Prod._ID = Product[i]._ID;
        Prod._Name = Product[i]._Name;
        Prod._Price = Product[i]._Price;
        Prod._Qte = Product[i]._Qte;
        Prod._CAT = Product[i]._CAT;
        Prod._Vote = Product[i]._Vote;
        Prod._Image = Product[i]._Image;
        Prod.ms = new MemoryStream(Prod._Image);
        Convert(Prod.ms,Prod._Img);
        Produc.Add(Prod);

    }
    return Produc;

}

所以我试着用那种方式转换它:

public static async void Convert(MemoryStream mem, BitmapImage Img)
        {
            IRandomAccessStream a1 = await ConvertToRandomAccessStream(mem);
            Img = new BitmapImage();
            await Img.SetSourceAsync(a1);
        }


      public static async Task<IRandomAccessStream> ConvertToRandomAccessStream(MemoryStream memoryStream)
        {
            var randomAccessStream = new InMemoryRandomAccessStream();
            var outputStream = randomAccessStream.GetOutputStreamAt(0);
            var dw = new DataWriter(outputStream);
            var task = Task.Factory.StartNew(() => dw.WriteBytes(memoryStream.ToArray()));
            await task;
            await dw.StoreAsync();
            await outputStream.FlushAsync();
            return randomAccessStream;
        }

1 个答案:

答案 0 :(得分:0)

  

它将图像作为字节返回我没有找到关于如何在我的图像控件中显示它的任何想法

从您的代码Log._Image = (byte[])Table.Rows[i]["Image"];,我认为您的Bytes表示字节数组。我认为问题是您需要知道图像如何从图像转换为字节数组。我假设你的wcf用于保存数据(包括图像的字节数组)到表并检索它们,你的转换图像到字节数组工作仍然在UWP应用程序中完成,然后我已经测试了你的代码,如果我转换以这种方式在UWP应用程序中将图像转换为字节数组:

var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/1.jpeg"));
var b64 = await ConvertStorageFileToBase64String(file);

private async Task<byte[]> ConvertStorageFileToBase64String(StorageFile File)
{
    var stream = await File.OpenReadAsync();

    using (var dataReader = new DataReader(stream))
    {
        var bytes = new byte[stream.Size];
        await dataReader.LoadAsync((uint)stream.Size);
        dataReader.ReadBytes(bytes);

        return bytes;
    }
}

然后使用您的方法转换回来:

MemoryStream mem = new MemoryStream(b64);
IRandomAccessStream a1 = await ConvertToRandomAccessStream(mem);
var Img = new BitmapImage();
await Img.SetSourceAsync(a1);
myimg.Source = Img;  //myimg is the name of the image control.

图像可以在图像控制中正确显示。因此,如果您的代码不能在您身边工作,请更新您的代码以将图像转换为字节数组,或者您可以上传您的示例,以便我们进行测试。