0

Here is my situation :

  • I have a windows service with access to the files on the server

  • A Windows Store Apps with access to said service

  • Windows Store Apps don't have access to the bitmap object (only bitmapimage)

  • The service has access to bitmap but not bitmapimage

  • The service only has access to file.io

  • Windows Store Apps only have access to Windows.Storage

I am having difficulties finding a way to pass all my images from the service to the Windows Store App as I can't find a common ground between the 2.

I have tried to transfer a bitmap and convert it to a bitmapimage but then I could not find a way to save the BitmapImage to a specific folder.

Any idea of what could put me on the right track as far as finding a common object to transfer ?

micbobo
  • 862
  • 7
  • 24
  • 1
    Intuition suggests looking to see if you can convert the image data into bytes or a byte array and transfer them that way, either as a file or stream. – ChicagoMike Apr 30 '15 at 12:44

1 Answers1

1

Big Thanks to ChicagoMike for putting me on the right track. Here is how I did it

Object to transfer from the service to the client :

Imports System.IO

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

Code on the client calling its controller (The linq and For loop is useful in my case since I have multiple types of images in their respective folders):

    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

Code in the client controller :

    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

Code on the service :

    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

This code pretty much sends back a Byte() and a picture name from the service which is then used by the client. The client uses the buffer to write on the files and recreating the picture

micbobo
  • 862
  • 7
  • 24