将图像从服务器传输到本地文件夹 Windows Store App
Transfer images from server to local folder Windows Store App
这是我的情况:
我有一个 windows 服务 可以访问服务器上的文件
一个 Windows 商店应用程序 可以访问所述服务
Windows 应用商店 无权访问 bitmap
对象(仅 bitmapimage
)
该服务可以访问 bitmap
但不能访问 bitmapimage
该服务只能访问 file.io
Windows Store Apps 只能访问 Windows.Storage
我很难找到将所有图片从服务传递到 Windows Store App 的方法,因为我找不到 2.
之间的共同点
我尝试传输 bitmap
并将其转换为 bitmapimage
,但后来我找不到将 BitmapImage 保存到特定文件夹的方法。
关于找到要转移的共同对象,有什么可以让我走上正轨的想法吗?
非常感谢 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
在文件上写入并重新创建图片
这是我的情况:
我有一个 windows 服务 可以访问服务器上的文件
一个 Windows 商店应用程序 可以访问所述服务
Windows 应用商店 无权访问
bitmap
对象(仅bitmapimage
)该服务可以访问
bitmap
但不能访问bitmapimage
该服务只能访问
file.io
Windows Store Apps 只能访问
Windows.Storage
我很难找到将所有图片从服务传递到 Windows Store App 的方法,因为我找不到 2.
之间的共同点我尝试传输 bitmap
并将其转换为 bitmapimage
,但后来我找不到将 BitmapImage 保存到特定文件夹的方法。
关于找到要转移的共同对象,有什么可以让我走上正轨的想法吗?
非常感谢 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
在文件上写入并重新创建图片