I want to create a video from a few `RenderTargetBitmap`s in UWP. I am doing that by using `MediaClips`.
From `RenderTargetBitmap` i can get an `IBuffer` or byte array of pixels.
To create a `MediaClip` I need either an image file or an `IDirect3DSurface`.
Creating an image just to create a clip is very expensive, so I thought of using `IDirect3DSurface`. 
How can I do this?
I have tried this:

```
        RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
        await renderTargetBitmap.RenderAsync(RenderedGrid, 100, 100);

        IBuffer pixels = await renderTargetBitmap.GetPixelsAsync();
        var values = Enum.GetValues(typeof(DirectXPixelFormat));
        CanvasBitmap bitmap=null;

        foreach (DirectXPixelFormat format in values)
        {
            try
            {
                videoClip = new MediaComposition();
                bitmap = CanvasBitmap.CreateFromBytes(myWidget.Device, pixels, renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight, format);
                StorageFile video2 = await storageFolder.CreateFileAsync("video2" + ".mp4", CreationCollisionOption.ReplaceExisting);
                MediaClip d = MediaClip.CreateFromSurface(bitmap, DateTime.Now - previousFrame+new TimeSpan(100));
                videoClip.Clips.Add(d);
                await videoClip.RenderToFileAsync(video2);
                break;
            }
            catch(Exception e)
            {

            }
        }

```

I try all the formats in `DirectXPixelFormat` but none works.

I have a `CanvasControl` named `myWidget` that is empty.

I create a `CanvasBitmap` from `Ibuffer` (`CanvasBitmap` implements `IDirect3DSurface`)

Create a `Mediaclip` from `CanvasBitmap`

`Add it to MediaComposition`.

Then I try to render to video file.When i try to save to a file it throws an error

> 
> System.Runtime.InteropServices.COMException Stream is not in a state
>  to handle the request.
> 
> 
> 

EDIT:
I figured out where the problem is, but not why and not how to fix it.

```
            await videoClip.SaveAsync(video2);
            videoClip= await MediaComposition.LoadAsync(video2);
            var x=await videoClip.RenderToFileAsync(video2);

```

Now with these three lines i can save the video, but using only the third line it throws the error above. I cannot make sense of it. Why does saving and loading fix the problem??