예제 #1
0
        private bool InsertFrames(bool after)
        {
            try
            {
                #region Actual List

                var    actualFrame = ActualList[0].ImageLocation.SourceFrom();
                double oldWidth    = actualFrame.PixelWidth;
                double oldHeight   = actualFrame.PixelHeight;
                var    scale       = Math.Round(actualFrame.DpiX, MidpointRounding.AwayFromZero) / 96d;

                //If the canvas size changed.
                if (Math.Abs(RightCanvas.ActualWidth * scale - oldWidth) > 0 || Math.Abs(RightCanvas.ActualHeight * scale - oldHeight) > 0 ||
                    Math.Abs(RightImage.ActualWidth * scale - oldWidth) > 0 || Math.Abs(RightImage.ActualHeight * scale - oldHeight) > 0)
                {
                    StartProgress(ActualList.Count, FindResource("Editor.UpdatingFrames").ToString());

                    //Saves the state before resizing the images.
                    ActionStack.SaveState(ActionStack.EditAction.ImageAndProperties, ActualList, Util.Other.CreateIndexList2(0, ActualList.Count));

                    foreach (var frameInfo in ActualList)
                    {
                        #region Resize Images

                        // Draws the images into a DrawingVisual component
                        var drawingVisual = new DrawingVisual();
                        using (var context = drawingVisual.RenderOpen())
                        {
                            //The back canvas.
                            context.DrawRectangle(new SolidColorBrush(Settings.Default.InsertFillColor), null,
                                                  new Rect(new Point(0, 0), new Point(Math.Round(RightCanvas.ActualWidth, MidpointRounding.AwayFromZero), Math.Round(RightCanvas.ActualHeight, MidpointRounding.AwayFromZero))));

                            var topPoint  = Dispatcher.Invoke(() => Canvas.GetTop(RightImage));
                            var leftPoint = Dispatcher.Invoke(() => Canvas.GetLeft(RightImage));

                            //The image.
                            context.DrawImage(frameInfo.ImageLocation.SourceFrom(),
                                              new Rect(leftPoint, topPoint, RightImage.ActualWidth, RightImage.ActualHeight));

                            //context.DrawText(new FormattedText("Hi!", CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface("Segoe UI"), 32, Brushes.Black), new Point(0, 0));
                        }

                        // Converts the Visual (DrawingVisual) into a BitmapSource
                        var bmp = new RenderTargetBitmap((int)(RightCanvas.ActualWidth * scale), (int)(RightCanvas.ActualHeight * scale),
                                                         actualFrame.DpiX, actualFrame.DpiX, PixelFormats.Pbgra32);
                        bmp.Render(drawingVisual);

                        #endregion

                        #region Save

                        //Creates a PngBitmapEncoder and adds the BitmapSource to the frames of the encoder
                        var encoder = new PngBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(bmp));

                        // Saves the image into a file using the encoder
                        using (Stream stream = File.Create(frameInfo.ImageLocation))
                            encoder.Save(stream);

                        #endregion

                        if (_isCancelled)
                        {
                            return(false);
                        }

                        UpdateProgress(ActualList.IndexOf(frameInfo));
                    }
                }

                #endregion

                #region New List

                var newFrame = NewList[0].ImageLocation.SourceFrom();
                oldWidth  = newFrame.PixelWidth;
                oldHeight = newFrame.PixelHeight;
                scale     = Math.Round(newFrame.DpiX, MidpointRounding.AwayFromZero) / 96d;

                StartProgress(ActualList.Count, FindResource("Editor.ImportingFrames").ToString());

                var folder       = Path.GetDirectoryName(ActualList[0].ImageLocation);
                var insertFolder = Path.GetDirectoryName(NewList[0].ImageLocation);

                //If the canvas size changed.
                if (Math.Abs(LeftCanvas.ActualWidth * scale - oldWidth) > 0 || Math.Abs(LeftCanvas.ActualHeight * scale - oldHeight) > 0 ||
                    Math.Abs(LeftImage.ActualWidth * scale - oldWidth) > 0 || Math.Abs(LeftImage.ActualHeight * scale - oldHeight) > 0)
                {
                    foreach (var frameInfo in NewList)
                    {
                        #region Resize Images

                        //Draws the images into a DrawingVisual component.
                        var drawingVisual = new DrawingVisual();
                        using (var context = drawingVisual.RenderOpen())
                        {
                            //The back canvas.
                            context.DrawRectangle(new SolidColorBrush(Settings.Default.InsertFillColor), null,
                                                  new Rect(new Point(0, 0), new Point(Math.Round(RightCanvas.ActualWidth, MidpointRounding.AwayFromZero), Math.Round(RightCanvas.ActualHeight, MidpointRounding.AwayFromZero))));

                            var topPoint  = Dispatcher.Invoke(() => Canvas.GetTop(LeftImage));
                            var leftPoint = Dispatcher.Invoke(() => Canvas.GetLeft(LeftImage));

                            //The front image.
                            context.DrawImage(frameInfo.ImageLocation.SourceFrom(), new Rect(leftPoint, topPoint, LeftImage.ActualWidth, LeftImage.ActualHeight));
                        }

                        //Converts the Visual (DrawingVisual) into a BitmapSource.
                        var bmp = new RenderTargetBitmap((int)(LeftCanvas.ActualWidth * scale), (int)(LeftCanvas.ActualHeight * scale), newFrame.DpiX, newFrame.DpiX, PixelFormats.Pbgra32);
                        bmp.Render(drawingVisual);

                        #endregion

                        #region Save

                        //Creates a PngBitmapEncoder and adds the BitmapSource to the frames of the encoder.
                        var encoder = new PngBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(bmp));

                        File.Delete(frameInfo.ImageLocation);

                        var fileName = Path.Combine(folder,
                                                    $"{_insertIndex}-{NewList.IndexOf(frameInfo)} {DateTime.Now.ToString("hh-mm-ss")}.png");

                        //Saves the image into a file using the encoder.
                        using (Stream stream = File.Create(fileName))
                            encoder.Save(stream);

                        frameInfo.ImageLocation = fileName;

                        #endregion

                        if (_isCancelled)
                        {
                            return(false);
                        }

                        UpdateProgress(NewList.IndexOf(frameInfo));
                    }
                }
                else
                {
                    foreach (var frameInfo in NewList)
                    {
                        #region Move

                        var fileName = Path.Combine(folder, $"{_insertIndex}-{NewList.IndexOf(frameInfo)} {DateTime.Now.ToString("hh-mm-ss")}.png");

                        File.Move(frameInfo.ImageLocation, fileName);

                        frameInfo.ImageLocation = fileName;

                        #endregion

                        if (_isCancelled)
                        {
                            return(false);
                        }

                        UpdateProgress(NewList.IndexOf(frameInfo));
                    }
                }

                Directory.Delete(insertFolder, true);

                #endregion

                if (_isCancelled)
                {
                    return(false);
                }

                #region Merge the Lists

                if (after)
                {
                    _insertIndex++;
                }

                //Saves the state before inserting the images.
                ActionStack.SaveState(ActionStack.EditAction.Add, _insertIndex, NewList.Count);

                ActualList.InsertRange(_insertIndex, NewList);

                #endregion

                return(true);
            }
            catch (Exception ex)
            {
                LogWriter.Log(ex, "Insert Error");
                Dispatcher.Invoke(() => Dialog.Ok("Insert Error", "Something Wrong Happened", ex.Message));

                return(false);
            }
        }
예제 #2
0
        private bool InsertFrames(bool after)
        {
            try
            {
                #region Actual List

                var    actualFrame = ActualList[0].ImageLocation.SourceFrom();
                double oldWidth    = actualFrame.Width;
                double oldHeight   = actualFrame.Height;

                //If the canvas size changed.
                if (Math.Abs(LeftCanvas.ActualWidth - oldWidth) > 0 || Math.Abs(LeftCanvas.ActualHeight - oldHeight) > 0 ||
                    Math.Abs(LeftImage.ActualWidth - oldWidth) > 0 || Math.Abs(LeftImage.ActualHeight - oldHeight) > 0)
                {
                    StartProgress(ActualList.Count, "Drawing Current Images");

                    foreach (var frameInfo in ActualList)
                    {
                        #region Resize Images

                        // Draws the images into a DrawingVisual component
                        DrawingVisual drawingVisual = new DrawingVisual();
                        using (DrawingContext context = drawingVisual.RenderOpen())
                        {
                            //The back canvas.
                            context.DrawRectangle(new SolidColorBrush(Settings.Default.InsertFillColor), null,
                                                  new Rect(new Point(0, 0), new Point((int)RightCanvas.ActualWidth, (int)RightCanvas.ActualHeight)));

                            double topPoint  = Dispatcher.Invoke(() => Canvas.GetTop(LeftImage));
                            double leftPoint = Dispatcher.Invoke(() => Canvas.GetLeft(LeftImage));

                            //The image.
                            context.DrawImage(frameInfo.ImageLocation.SourceFrom(),
                                              new Rect(leftPoint, topPoint, LeftImage.ActualWidth, LeftImage.ActualHeight));

                            //context.DrawText(new FormattedText("Hi!", CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface("Segoe UI"), 32, Brushes.Black), new Point(0, 0));
                        }

                        // Converts the Visual (DrawingVisual) into a BitmapSource
                        RenderTargetBitmap bmp = new RenderTargetBitmap((int)LeftCanvas.ActualWidth, (int)LeftCanvas.ActualHeight, 96, 96, PixelFormats.Pbgra32);
                        bmp.Render(drawingVisual);

                        #endregion

                        #region Save

                        // Creates a PngBitmapEncoder and adds the BitmapSource to the frames of the encoder
                        var encoder = new BmpBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(bmp));

                        // Saves the image into a file using the encoder
                        using (Stream stream = File.Create(frameInfo.ImageLocation))
                            encoder.Save(stream);

                        #endregion

                        if (_isCancelled)
                        {
                            return(false);
                        }

                        UpdateProgress(ActualList.IndexOf(frameInfo));
                    }
                }

                #endregion

                #region New List

                var newFrame = NewList[0].ImageLocation.SourceFrom();
                oldWidth  = newFrame.Width;
                oldHeight = newFrame.Height;

                //If the canvas size changed.
                if (Math.Abs(RightCanvas.ActualWidth - oldWidth) > 0 || Math.Abs(RightCanvas.ActualHeight - oldHeight) > 0 ||
                    Math.Abs(RightImage.ActualWidth - oldWidth) > 0 || Math.Abs(RightImage.ActualHeight - oldHeight) > 0)
                {
                    StartProgress(ActualList.Count, "Drawing Current Images");

                    var folder       = Path.GetDirectoryName(ActualList[0].ImageLocation);
                    var insertFolder = Path.GetDirectoryName(NewList[0].ImageLocation);

                    foreach (var frameInfo in NewList)
                    {
                        #region Resize Images

                        // Draws the images into a DrawingVisual component
                        DrawingVisual drawingVisual = new DrawingVisual();
                        using (DrawingContext context = drawingVisual.RenderOpen())
                        {
                            //The back canvas.
                            context.DrawRectangle(new SolidColorBrush(Settings.Default.InsertFillColor), null,
                                                  new Rect(new Point(0, 0), new Point((int)RightCanvas.ActualWidth, (int)RightCanvas.ActualHeight)));

                            double topPoint  = Dispatcher.Invoke(() => Canvas.GetTop(RightImage));
                            double leftPoint = Dispatcher.Invoke(() => Canvas.GetLeft(RightImage));

                            //The front image.
                            context.DrawImage(frameInfo.ImageLocation.SourceFrom(), new Rect(leftPoint, topPoint, RightImage.ActualWidth, RightImage.ActualHeight));
                        }

                        // Converts the Visual (DrawingVisual) into a BitmapSource
                        RenderTargetBitmap bmp = new RenderTargetBitmap((int)RightCanvas.ActualWidth, (int)RightCanvas.ActualHeight, 96, 96, PixelFormats.Pbgra32);
                        bmp.Render(drawingVisual);

                        #endregion

                        #region Save

                        // Creates a PngBitmapEncoder and adds the BitmapSource to the frames of the encoder
                        BmpBitmapEncoder encoder = new BmpBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(bmp));

                        File.Delete(frameInfo.ImageLocation);

                        var fileName = Path.Combine(folder,
                                                    String.Format("{0}-{1}.bmp", _insertIndex, NewList.IndexOf(frameInfo)));

                        // Saves the image into a file using the encoder
                        using (Stream stream = File.Create(fileName))
                            encoder.Save(stream);

                        frameInfo.ImageLocation = fileName;

                        #endregion

                        if (_isCancelled)
                        {
                            return(false);
                        }

                        UpdateProgress(NewList.IndexOf(frameInfo));
                    }

                    Directory.Delete(insertFolder, true);
                }

                #endregion

                if (_isCancelled)
                {
                    return(false);
                }

                #region Merge the Lists

                if (after)
                {
                    _insertIndex++;
                }

                ActualList.InsertRange(_insertIndex, NewList);

                #endregion

                return(true);
            }
            catch (Exception ex)
            {
                LogWriter.Log(ex, "Insert Error");
                Dispatcher.Invoke(() => Dialog.Ok("Insert Error", "Something Wrong Happened", ex.Message));

                return(false);
            }
        }