Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WPF use Multi-thread question

$
0
0

I want save all UserControl as image, at the same time, I need to show a animation in order to make the UI better for users.

This is MainWindow:

<Window x:Class="WPF.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         xmlns:local="clr-namespace:FluidV.Components"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:vw="http://inosoft.com/visiwin7"
         d:DesignHeight="100"
         d:DesignWidth="1024"
         mc:Ignorable="d"><Grid x:Name="LayoutRoot"><Button Margin="10" Click="Button1_click"/></Grid></Window>

This is UserControl:

<UserControl x:Class="WPF.AnimationView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         Opacity="0.6"
         Background="#A8007499"
         mc:Ignorable="d"
         d:DesignHeight="300"
         d:DesignWidth="300"><Grid><DataGrid Name="dataGrid1"
                  Width="441"
                  Height="99"
                  Margin="40,70,0,0"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  AutoGenerateColumns="False" /><Canvas Name="loading"
                Width="120"
                Height="120"
                Margin="187,76,241,85"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                RenderTransformOrigin="0.5,0.5"><Ellipse Canvas.Left="55"
                     Canvas.Top="31"
                     Width="15"
                     Height="15"
                     Fill="#FFD1D1F7"
                     Opacity="1.0"
                     Stretch="Fill" /><Ellipse Canvas.Left="38"
                     Canvas.Top="39"
                     Width="15"
                     Height="15"
                     Fill="Blue"
                     Opacity="0.8"
                     Stretch="Fill" /><Ellipse Canvas.Left="36"
                     Canvas.Top="58"
                     Width="15"
                     Height="15"
                     Fill="#FF0000FE"
                     Opacity="0.7"
                     Stretch="Fill" /><Ellipse Canvas.Left="52"
                     Canvas.Top="67"
                     Width="15"
                     Height="15"
                     Fill="Blue"
                     Opacity="0.6"
                     Stretch="Fill" /><Ellipse Canvas.Left="68"
                     Canvas.Top="61"
                     Width="15"
                     Height="15"
                     Fill="#FF2E2EFF"
                     Opacity="0.5"
                     Stretch="Fill" /><Ellipse Canvas.Left="69"
                     Canvas.Top="42"
                     Width="15"
                     Height="15"
                     Fill="#FF6F6FFF"
                     Opacity="0.4"
                     Stretch="Fill" /><Canvas.RenderTransform><RotateTransform x:Name="SpinnerRotate"
                                 Angle="0" /></Canvas.RenderTransform><Canvas.Triggers><EventTrigger RoutedEvent="ContentControl.Loaded"><BeginStoryboard><Storyboard><DoubleAnimation Duration="0:0:0.8"
                                             From="0"
                                             RepeatBehavior="Forever"
                                             Storyboard.TargetName="SpinnerRotate"
                                             Storyboard.TargetProperty="(RotateTransform.Angle)"
                                             To="360" /></Storyboard></BeginStoryboard></EventTrigger></Canvas.Triggers></Canvas></Grid></UserControl>

This is MainWindow.xaml.cs:

 private void Button1_click(object sender, RoutedEventArgs e)
          {

              Thread t = new Thread(new ThreadStart(() => { 
                      Dispatcher.BeginInvoke(new Action(() => { 
                          AnimationView w = new AnimationView();
                          w.Show(); })); })); 
              t.Start();



              foreach (UserControl UC in UserControl)
              {
                  String fileName = UC.Name;

                  String filePath = "D:\ScreenShot";

                   SaveView(view, fileName,  filePath);

              }

        }

public static void SaveView(UserControl view, string fileName,  string destFolder)
        {

                Rect bounds = VisualTreeHelper.GetDescendantBounds(view);

                int width = (int)view.RenderSize.Width;
                int height = (int)view.RenderSize.Height;

                if (width == 0 || height == 0)
                {
                    width = 1920;
                    height = 1080;
                }

                RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);

                DrawingVisual dv = new DrawingVisual();

                using (DrawingContext ctx = dv.RenderOpen())
                {
                    Size size = new Size(500, 500);
                    if (bounds.Size.IsEmpty == false)
                    {
                        if (bounds.Size.Height > 100)
                        {
                            size = new Size(bounds.Size.Width, bounds.Size.Height - 100);
                        }
                        else
                            size = new Size(bounds.Size.Width, bounds.Size.Height);
                    }

                    VisualBrush vb = new VisualBrush(view);
                    ctx.DrawRectangle(vb, new Pen(Brushes.Blue, 2), new Rect(new Point(0, 100), size));
                    ctx.DrawText(text, new Point(0, 20));
                    ctx.Close();
                }

                //  rtb.Render(view);
                rtb.Render(dv);

                try
                {
                    PngBitmapEncoder jpgEncoder = new PngBitmapEncoder();
                    jpgEncoder.Frames.Add(BitmapFrame.Create(rtb));

                    Byte[] _imageArray;

                    using (MemoryStream outputStream = new MemoryStream())
                    {
                        jpgEncoder.Save(outputStream);
                        _imageArray = outputStream.ToArray();
                    }

                    //Try Find Save Path, if doesn't exists, create it.
                    if (Directory.Exists(destFolder) == false)
                        Directory.CreateDirectory(destFolder);

                    FileStream fileStream = new FileStream(Path.Combine(destFolder, fileName), FileMode.Create, FileAccess.ReadWrite);

                    fileStream.Write(_imageArray, 0, _imageArray.Length);

                    jpgEncoder.Save(fileStream);
                    fileStream.Close();


                }
                catch (Exception e)
                {
                    Log4Net.Instance.Info(System.Reflection.MethodInfo.GetCurrentMethod().ToString());
                    Log4Net.Instance.Info("Exception Generate Screenshot: " + fileName);
                    Log4Net.Instance.Info(e.StackTrace.ToString());
                    Debug.WriteLine(e.ToString());
                }
            }), DispatcherPriority.Loaded);
        }

I hope I can get your help.

Many thanks in advance.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>