C# 我想使用WPF中的web服务或c中的Windows应用商店应用程序从sql server发送和检索图像

C# 我想使用WPF中的web服务或c中的Windows应用商店应用程序从sql server发送和检索图像,c#,windows-store-apps,C#,Windows Store Apps,我正在开发一个应用程序,我想通过web服务在图像中显示存储在DB中的图像。这是我向应用程序发送图片的web服务当前代码: Connection con = new Connection(); con.Start(); DataTable dt = con.Select("Select ID,Picture ,Customer_Name,Contact_No,Time_In from Customers_Data WHERE ( Date >= dateadd(day,datediff

我正在开发一个应用程序,我想通过web服务在图像中显示存储在DB中的图像。这是我向应用程序发送图片的web服务当前代码:

Connection con = new Connection();
con.Start();
DataTable dt = con.Select("Select  ID,Picture ,Customer_Name,Contact_No,Time_In from Customers_Data WHERE   ( Date >= dateadd(day,datediff(day,7,GETDATE()),0)  AND  Date < dateadd(day,datediff(day,0,GETDATE()),1)) And (Sale_Made='No') ");

con.Stop();
List<Customer> ro = new List<Customer>();

foreach (DataRow item in dt.Rows)
{
    string filePath = item["Picture"].ToString();
    FileStream fs = new FileStream(filePath, FileMode.Open);
    byte[] result;
    using (Stream stream = fs)
    {
        using (var memoryStream = new MemoryStream())
        {
            stream.CopyTo(memoryStream);
            result = memoryStream.ToArray();
            // ProfilePicture = result,
        }
    }
    ro.Add(new Customer { Id = item["ID"].ToString(), ProfilePicture = result, Name = item["Customer_Name"].ToString(), Contact = item["Contact_No"].ToString(), Time_In = item["Time_In"].ToString() });
}
return ro;

有人知道我如何展示它吗?谢谢。

在这里,我将做一个小猜测,因为我没有您示例的完整代码

假设您的Web服务方法是下面的方法,您将需要它,因为您将序列化对象列表

public string GetCustomer()
{
    Connection con = new Connection();
    con.Start();
    DataTable dt = con.Select("Select  ID,Picture ,Customer_Name,Contact_No,Time_In from Customers_Data WHERE   ( Date >= dateadd(day,datediff(day,7,GETDATE()),0)  AND  Date < dateadd(day,datediff(day,0,GETDATE()),1)) And (Sale_Made='No') ");

    con.Stop();
    List<Customer> ro = new List<Customer>();

    foreach (DataRow item in dt.Rows)
    {
        string filePath = item["Picture"].ToString();
        FileStream fs = new FileStream(filePath, FileMode.Open);
        byte[] result;
        using (Stream stream = fs)
        {
            using (var memoryStream = new MemoryStream())
            {
                stream.CopyTo(memoryStream);
                result = memoryStream.ToArray();
                // ProfilePicture = result,
            }
        }
        ro.Add(new Customer { Id = item["ID"].ToString(), ProfilePicture = result, Name = item["Customer_Name"].ToString(), Contact = item["Contact_No"].ToString(), Time_In = item["Time_In"].ToString() });
    }
    return JsonConvert.SerializeObject(ro);
}
然后,需要在页面XAML中添加图像控件:

<Image x:Id="imgData" />
之后,需要从URL反序列化JSON并将其强制转换到类中,需要复制并粘贴相同的JSON。此外,还需要将字节转换为可写的位图

public async void SetProfilePicture()
{
    HttpClient http = new HttpClient();

    string url = string.Format("YOUR URL");

    HttpResponseMessage response = await http.GetAsync(url);

    var result = await response.Content.ReadAsStringAsync();

    var deserialized = JsonConvert.DeserializeObject<List<Customer>>(result);

    // Bitmap samples are here in a byte array
    byte[] samples = deserialized[0].ProfilePicture;

    //Here you set the width and height
    WriteableBitmap bitmap = new WriteableBitmap(128, 128);

    System.IO.Stream bitmapStream = null;
    bitmapStream = bitmap.PixelBuffer.AsStream();
    bitmapStream.Position = 0;

    await bitmapStream.WriteAsync(samples, 0, 4 * 128 * 128);

    bitmapStream.Flush();
    imgData.Source = bitmap;
}
请查看,在这里您可以阅读更多信息:


请提供您的问题所在。我只想将存储在数据库中的字节发送到应用程序以显示该图片。Federico Navarrete:这是web服务或应用程序的方法…因为我需要将web服务中的字节发送到windows应用程序,然后将这些字节转换为图像。谢谢你看到我的问题,我只是检查你的代码,你没有提供任何来自WS的代码,只提供了存储字节数组的DB读数。我们可以在skype上聊天吗?我可以告诉你我的整个问题吗?关于skype no@shahzad_Ali,我不分享。然而,我做了一个小的更新,包括一个带有JSON.NET的Web服务。