multiply converting and saving xaml to png how to save properly
===

Hello i have little problem that is little weird for me. For only one image it works without complications. But when i put into one function make empty png files. Dunno why.

This is code which make problems:




Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();

            dialog.Title = "Choose xaml file";
            dialog.AddExtension = true;
            dialog.Multiselect = true;
            dialog.CheckFileExists = true;
            dialog.DefaultExt = ".xaml";
            dialog.Filter = "Xaml  files |*.xaml";

            if (dialog.ShowDialog() == true)
            {
              
                foreach (string fileName in dialog.FileNames)
                {
             
                    UIElement visual = XamlReader.Load(System.Xml.XmlReader.Create(fileName)) as UIElement;

                    if (visual != null)
                    {
                        dockPanel.Children.Clear();
                        dockPanel.Children.Add(visual);
                    }
                    else MessageBox.Show("You made mistake ;(", "error", MessageBoxButton.OK);
                    
                    
                    Visual vis = dockPanel;
                    
                    double width = Convert.ToDouble(szwidth.Text);
                    double height = Convert.ToDouble(wyheight.Text);
                    if (double.IsNaN(width) || double.IsNaN(height)) throw new FormatException("Wrong date");
                    Size size = new Size(width, height);
                   
                    DrawingVisual drawingVis = new DrawingVisual();
                    VisualBrush vBrush = new VisualBrush(vis);

                    using (DrawingContext dc = drawingVis.RenderOpen())
                    {
                        dc.DrawRectangle(vBrush, null, new Rect(new Point(), size));
                    }

                    RenderTargetBitmap render = new RenderTargetBitmap(Convert.ToInt32(600), Convert.ToInt32(400), 96, 96, PixelFormats.Pbgra32);
                    render.Render(drawingVis);

                    int nameLength = fileName.Length;
                    string newFileName = fileName.Remove(nameLength - 5);
                    string nameToWrite = newFileName + ".png";

                    
           

                  PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(render));
                    Stream oStream = new FileStream(nameToWrite, FileMode.Create);
                    encoder.Save(oStream);
                    oStream.Flush();
                    oStream.Close();
                
                }
            }

This is good to export, but fails when i want to save. It saves blank png image.



In the example below I use the function that is under two diffrent buttons.One to export and other to save.That works but example above don't.

 



  private void choose(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();

            dialog.Title = "Wybierz plik xaml";
            dialog.AddExtension = true;
            dialog.Multiselect = true;
            dialog.CheckFileExists = true;
            dialog.DefaultExt = ".xaml";
            dialog.Filter = "Pliki Xaml |*.xaml";

            if (dialog.ShowDialog() == true)
            {
                sciezka = dialog.FileName;
            ;
               
                    UIElement visual = XamlReader.Load(System.Xml.XmlReader.Create(sciezka)) as UIElement;
                    if (visual != null) dockPanel.Children.Add(visual);
                  
                    else
                    {
                        MessageBox.Show("Nie może łapnąć pliku", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        this.Close();
                    }
                
            }
        }





private void export(object sender, RoutedEventArgs e)
        {
            Visual theVisual = dockPanel;

            double width = Convert.ToDouble(szerokosc.Text);
            double height = Convert.ToDouble(wysokosc.Text);

            if (double.IsNaN(width) || double.IsNaN(height)) throw new FormatException("Nie wpisałeś poprawnych danych");

            Size size = new Size(width, height);

            DrawingVisual drawingVisual = new DrawingVisual();
            VisualBrush vBrush = new VisualBrush(theVisual);

            using (DrawingContext dc = drawingVisual.RenderOpen()) 
            {
                dc.DrawRectangle(vBrush, null, new Rect(new Point(), size));
            }

            RenderTargetBitmap render = new RenderTargetBitmap(Convert.ToInt32(1000), Convert.ToInt32(800), 96, 96, PixelFormats.Pbgra32);
            render.Render(drawingVisual);


            Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
            dialog.Title = "Wybierz folder do zapisu";
            dialog.AddExtension = true;
            dialog.DefaultExt = ".png";
            dialog.Filter = "Pliki png |*.png";

            if (dialog.ShowDialog() == true)
            {               
                string nz = dialog.FileName;
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(render));
                Stream oStream = new FileStream(nz, FileMode.Create);
                encoder.Save(oStream);
                oStream.Flush();
                oStream.Close();
            }
        }

Why don't work properly? Please help
