将图像从服务器传输到本地文件夹Windows Store App

时间:2015-04-30 12:30:57

标签: .net vb.net web-services windows-store-apps file-transfer

以下是我的情况:

  • 我有一个 Windows服务,可以访问服务器上的文件

  • 可以访问所述服务的 Windows应用商店

  • Windows应用商店无法访问bitmap对象(仅bitmapimage

  • 该服务可以访问bitmap但不能访问bitmapimage

  • 该服务只能访问file.io

  • Windows应用商店只能访问Windows.Storage

我很难找到将所有图像从服务传递到 Windows应用商店应用的方法,因为我无法在2 共同点>

我尝试转移bitmap并将其转换为bitmapimage但我找不到将BitmapImage保存到特定文件夹的方法。

关于什么可以让我走上正确的道路,找到一个共同的物体来转移?

1 个答案:

答案 0 :(得分:1)

非常感谢ChicagoMike让我走上正轨。我是这样做的

从服务转移到客户端的对象:

Imports System.IO

    Public Class PictureSender
        Public Property PictureBytes As Byte()
        Public Property PictureName As String
    End Class

客户端调用其控制器的代码(linq和For循环在我的情况下很有用,因为我在各自的文件夹中有多种类型的图像):

    Private Async Sub btnGetPictures_Click(sender As Object, e As RoutedEventArgs)
        ' Get the list of all categories of picture to go through all folders from local saves.
        Dim lstFolders As List(Of String) = (From p In Await ImageController.GetImageDetails Select p.Categorie).Distinct.ToList()
        For i = 0 To lstFolders.Count - 1
            PicturesController.GetPicturesServer(lstFolders(i))
        Next
    End Sub

客户端控制器中的代码:

    Public Async Sub GetPicturesServer(_folderName As String)
        Dim service As New ServiceReference1.Service1Client(ServiceReference1.Service1Client.EndpointConfiguration.BasicHttpBinding_IService1)

        Dim rcv = Await service.SelectAllPicturesAsync(_folderName)

        ' Get the folder
        Dim folder = ApplicationData.Current.LocalFolder
        folder = Await folder.CreateFolderAsync("Images", CreationCollisionOption.OpenIfExists)
        Await folder.CreateFolderAsync(_folderName, CreationCollisionOption.ReplaceExisting)
        folder = Await folder.GetFolderAsync(_folderName)

        ' Run through all pictures and save them
        For i = 0 To rcv.Count - 1
            Dim myfile As StorageFile = CType(Await folder.CreateFileAsync(rcv(i).PictureName, CreationCollisionOption.ReplaceExisting), StorageFile)
            Await Windows.Storage.FileIO.WriteBufferAsync(myfile, rcv(i).PictureBytes.AsBuffer())
        Next

服务代码:

    Function Execute(TypeOfPictures As String) As List(Of PictureSender)
        Dim PicturesinDirectory As List(Of String) = Directory.GetFiles("\\SERVER\Data\Image\" + TypeOfPictures + "\", "*.jpg*").ToList()

        Dim lstPicturesToSend As List(Of PictureSender) = New List(Of PictureSender)
        For i = 0 To PicturesinDirectory.Count - 1
            Dim ByteArray As Byte() = File.ReadAllBytes(PicturesinDirectory(i))

            ' Affect the PictureSender object
            Dim PictureSenderObject As PictureSender = New PictureSender
            PictureSenderObject.PictureBytes = ByteArray
            PictureSenderObject.PictureName = PicturesinDirectory(i).Substring(PicturesinDirectory(i).LastIndexOf("\") + 1)
            lstPicturesToSend.Add(PictureSenderObject)
        Next
        Return lstPicturesToSend

    End Function

此代码几乎从服务中发回Byte()和图片名称,然后客户端使用该名称。客户端使用buffer在文件上写入并重新创建图片