【问题标题】:Uwp Display Image From Table SQLUwp 显示来自表 SQL 的图像
【发布时间】:2016-07-04 19:03:15
【问题描述】:

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

这是我的 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;
        }

【问题讨论】:

  • 我们真的需要看一些代码来帮助你。
  • 这是我的完整代码

标签: c# sql wcf uwp


【解决方案1】:

它以字节形式返回图像我不知道如何在我的图像控件中显示它

根据您的代码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.

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

【讨论】:

    猜你喜欢
    • 2019-02-14
    • 1970-01-01
    • 2018-12-03
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多