Streamed file from StreamedFileDataRequest

Liam O'Conor
Liam O'Conor used Ask the Experts™
on
private void FileDataRequest(StreamedFileDataRequest r)

{

MemoryStream m = new MemoryStream();

r = m.AsOutputStream as StreamedFileDataRequest;
}
void loop(){
StorageLibrary l = await StorageLibrary.GetLibraryAsync(FromKnownId.Pictures);

Scrollviewer scrollviewer = new Scrollviewer();

RenderTargetBitmap r = null;

 r = new RenderTargetBitmap();

                await r.RenderAsync(scrollviewer);

                

                var file0 = await l.SaveFolder.CreateFileAsync("poikidiki");

                await FileIO.WriteBufferAsync(file0, await r.GetPixelsAsync());

                Windows.Storage.Streams.IRandomAccessStream b = await file0.OpenAsync(FileAccessMode.ReadWrite);

               

                var file = await StorageFile.CreateStreamedFileAsync("effort06.jpg", new StreamedFileDataRequestedHandler(FileDataRequest),Windows.Storage.Streams.RandomAccessStreamReference.CreateFromFile(file0));





   

              
                await file.CopyAsync(l.SaveFolder);

}

Open in new window

I have this:

it doesn't copy the bytes to the streamed file.

help appreciated
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Developer
Commented:
Please embed you code into a CODE section..
Experts Exchange  How to Add a Code Snippet to a Question or Comment

Author

Commented:
ok thanks for commenting.

Author

Commented:
this is the full code with the issue in the underlined sentence:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Storage;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Media.Animation;
using Windows.Graphics.Imaging;
using Windows.UI;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace New_Photo_Viewer
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        StorageLibrary l = null;

        IStorageFile f = null;


        BitmapImage b = null;

        Stream s = null;

        Storyboard story = new Storyboard();

        DoubleAnimation anim = new DoubleAnimation();

        BitmapDecoder d = null;

        BitmapEncoder en = null;

        List<Color> colors = new List<Color>();

        int position = 0;

        IStorageFile fip = null;

        Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap r = null;

        double degrees = 0;

        int focus = 0;

        int horizontaloffset = 0;

        int verticaloffset = 0;


        byte[] bytes = new byte[Convert.ToInt32(Math.Pow(2,16))];

        bool StartFilling = false;

        MemoryStream m = new MemoryStream();

        public MainPage()
        {
            this.InitializeComponent();

            this.Loading += MainPage_Loading;

            this.KeyDown += MainPage_KeyDown;

            this.RightTapped += MainPage_RightTapped;

            listview.SelectionChanged += Listview_SelectionChanged;

            listbox.SelectionChanged += Listbox_SelectionChanged;

          

            

            listview.Items.Add("Zoom In");

            listview.Items.Add("Zoom Out");



            listview.Items.Add("Play Projection");

            listview.Items.Add("Stop Projection");

            listview.Items.Add("Get Items");

            listview.Items.Add("Set flow direction from right to left");

            listview.Items.Add("Set flow direction from left to right");

            listview.Items.Add("Start Changing Backround");

            listview.Items.Add("Stop Changing Backround");

            listview.Items.Add("Validate image");




            horizontaloffset = Convert.ToInt32(image.Width / 2);

            verticaloffset = Convert.ToInt32(image.Height / 2);

            colors.Add(Colors.Yellow);

            colors.Add(Colors.GreenYellow);

            colors.Add(Colors.Yellow);

            colors.Add(Colors.Gold);

            colors.Add(Colors.Gray);

            colors.Add(Colors.White);

            colors.Add(Colors.Black);

            colors.Add(Colors.Pink);
                




           



            listview.Visibility = Visibility.Collapsed;

            data.Visibility = Visibility.Collapsed;

            slider.ValueChanged += Slider_ValueChanged;

            slider.Maximum = 4;

            slider.Minimum = 0;

            slider.Visibility = Visibility.Collapsed;

            data.DoubleTapped += Data_DoubleTapped;



            heighttb.Visibility = Visibility.Collapsed;

            widthtb.Visibility = Visibility.Collapsed;

            heighttextbox.Visibility = Visibility.Collapsed;

            widthtextbox.Visibility = Visibility.Collapsed;

       



        }

        private  void FileDataRequest(StreamedFileDataRequest r)
        {
            MemoryStream m = new MemoryStream();

            

            r = m.AsOutputStream() as StreamedFileDataRequest;
        }

        private void MainPage_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Escape)
            {
                heighttb.Visibility = Visibility.Visible;

                heighttextbox.Visibility = Visibility.Visible;

                widthtb.Visibility = Visibility.Visible;

                widthtextbox.Visibility = Visibility.Visible;
            }
            if (e.Key == Windows.System.VirtualKey.Insert)
            {
                if (position < colors.Count)
                {
                    position += 1;

                    scrollviewer.Background = new SolidColorBrush(colors[position]);
                }
                if (position == colors.Count)
                {
                    position = 0;

                    scrollviewer.Background = new SolidColorBrush(colors[position]);
                }

            }
            if (e.Key == Windows.System.VirtualKey.Delete)
            {
                if (position > 0)
                {
                    position -= 1;

                    scrollviewer.Background = new SolidColorBrush(colors[position]);
                }
                if (position == 0)
                {
                    position = colors.Count;

                    scrollviewer.Background = new SolidColorBrush(colors[position]);
                }
            }
            
            
            
            
        }

        private void Data_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {
            data.Visibility = Visibility.Collapsed;
        }

        private void Slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            int focus = Convert.ToInt32(e.NewValue);

            scrollviewer.ChangeView(-150,-150, focus);

            slider.Visibility = Visibility.Collapsed;
        }

        

        private async void Listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            b = new BitmapImage();

            f = await l.SaveFolder.GetFileAsync(listbox.SelectedValue.ToString());

             s = await f.OpenStreamForReadAsync();

            b.SetSource(s.AsRandomAccessStream());

            d = await BitmapDecoder.CreateAsync(s.AsRandomAccessStream());

            en = await BitmapEncoder.CreateForTranscodingAsync(s.AsRandomAccessStream(), d);

            image.Source = b;
        }

        private async   void Listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listview.SelectedItem == listview.Items[0])
            {
                slider.Visibility = Visibility.Visible;

                listview.Visibility = Visibility.Collapsed;

                listview.SelectedItem = null;
            }
            if (listview.SelectedItem == listview.Items[1])
            {
                slider.Visibility = Visibility.Visible;
                listview.Visibility = Visibility.Collapsed;

                listview.SelectedItem = null;
            }
            if (listview.SelectedItem == listview.Items[2])
            {
                listbox.Visibility = Visibility.Collapsed;

                this.RequestedTheme = ElementTheme.Dark;



                listview.Visibility = Visibility.Collapsed;

                listview.SelectedItem = null;
            }
            if (listview.SelectedItem == listview.Items[3])
            {

                this.RequestedTheme = ElementTheme.Light;

                image.Arrange(new Rect(listbox.Width, 0, image.Width, image.Height));

                listbox.Visibility = Visibility.Visible;
                    

                listview.Visibility = Visibility.Collapsed;

                listview.SelectedItem = null;
                
            }
            if (listview.SelectedItem == listview.Items[4])
            {
                data.Visibility = Visibility.Visible;

                data.Items.Clear();

                data.Items.Add("Horizontal resolution:" + d.DpiX);

                data.Items.Add("Vertical resolution:" + d.DpiY);

                data.Items.Add("Pixel Height:" + b.PixelHeight);

                data.Items.Add("Pixel Width" + b.PixelWidth);

                data.Items.Add("Animated Bitmap" + b.IsAnimatedBitmap);

                data.Items.Add("Byte Size:" + s.Length);

                data.Items.Add("Source:" + image.Source);



                listview.Visibility = Visibility.Collapsed;

                listview.SelectedItem = null;


            }
            if(listview.SelectedItem == listview.Items[5])
            {


                scrollviewer.FlowDirection = FlowDirection.RightToLeft;

                listview.Visibility = Visibility.Collapsed;

                listview.SelectedItem = null;









            }
            if (listview.SelectedIndex == 6)
            {



                scrollviewer.FlowDirection = FlowDirection.LeftToRight;

                listview.Visibility = Visibility.Collapsed;

                listview.SelectedItem = null;

                














            }
            if (listview.SelectedIndex == 7)
            {

                StartFilling = true;


                listview.Visibility = Visibility.Collapsed;
            
                listview.SelectedItem = null;

            }
            if (listview.SelectedIndex == 8)
            {

                StartFilling = false;


                listview.Visibility = Visibility.Collapsed;

                listview.SelectedItem = null;

            }
            if (listview.SelectedIndex == 9)
            {
                r = new RenderTargetBitmap();

                await r.RenderAsync(scrollviewer);

                image.Source = r;

                var file0 = await l.SaveFolder.CreateFileAsync("poikidiki");

                await FileIO.WriteBufferAsync(file0, await r.GetPixelsAsync());

                Windows.Storage.Streams.IRandomAccessStream b = await file0.OpenAsync(FileAccessMode.ReadWrite);

               

              [u]  var file = await StorageFile.CreateStreamedFileAsync("effort06.jpg", new StreamedFileDataRequestedHandler(FileDataRequest),Windows.Storage.Streams.RandomAccessStreamReference.CreateFromFile(file0));
[/u]




   

              
                await file.CopyAsync(l.SaveFolder);

                listview.Visibility = Visibility.Collapsed;

                listview.SelectedItem = null;

                
            }

        }
       


        private void MainPage_RightTapped(object sender, RightTappedRoutedEventArgs e)
        {
            listview.Visibility = Visibility.Visible;
        }

        private async void MainPage_Loading(FrameworkElement sender, object args)
        {
            l = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);

            foreach(var file in await l.SaveFolder.GetFilesAsync())
            {
                listbox.Items.Add(file.Name);
            }
        }
    }
}

Open in new window

Author

Commented:
I have worked it out that the issue is in the StreamedFileDataRequest

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial