예제 #1
0
 private void JpgButton_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         var dialog = new SaveFileDialog {
             Filter = "Jpg files (*.jpg)|*.jpg|All files|*.*"
         };
         if (dialog.ShowDialog() == true)
         {
             BitmapLoader.Save2Jpg(
                 BitmapLoader.SaveImageSource(ItemsControl, (int)ItemsControl.Width, (int)ItemsControl.Height),
                 dialog.FileName);
         }
     }
     catch (Exception exception)
     {
         this.ShowMessageAsync("Save error", exception.Message);
         Log.Error("Save error", exception);
     }
 }
예제 #2
0
        public string ExecuteThread(FileItem item, string dest, ValuePairEnumerator configData)
        {
            var conf = new OverlayTransformViewModel(configData);

            using (var fileStream = new MemoryStream(File.ReadAllBytes(item.FileName)))
            {
                BitmapDecoder bmpDec = BitmapDecoder.Create(fileStream,
                                                            BitmapCreateOptions.PreservePixelFormat,
                                                            BitmapCacheOption.OnLoad);
                WriteableBitmap writeableBitmap = BitmapFactory.ConvertToPbgra32Format(bmpDec.Frames[0]);

                Grid grid = new Grid
                {
                    Width               = writeableBitmap.PixelWidth,
                    Height              = writeableBitmap.PixelHeight,
                    ClipToBounds        = true,
                    SnapsToDevicePixels = true
                };
                grid.UpdateLayout();
                var size = new Size(writeableBitmap.PixelWidth, writeableBitmap.PixelWidth);
                grid.Measure(size);
                grid.Arrange(new Rect(size));

                Image image = new Image {
                    Width = writeableBitmap.PixelWidth, Height = writeableBitmap.PixelHeight
                };
                image.BeginInit();
                image.Source = writeableBitmap;
                image.EndInit();
                image.Stretch = Stretch.Fill;
                grid.Children.Add(image);
                grid.UpdateLayout();

                Regex           regPattern = new Regex(@"\[(.*?)\]", RegexOptions.Singleline);
                MatchCollection matchX     = regPattern.Matches(conf.Text);
                var             text       = matchX.Cast <Match>().Aggregate(conf.Text, (current1, match) => item.FileNameTemplates.Where(template => System.String.Compare(template.Name, match.Value, System.StringComparison.InvariantCultureIgnoreCase) == 0).Aggregate(current1, (current, template) => current.Replace(match.Value, template.Value)));

                TextBlock textBlock = new TextBlock
                {
                    Text       = text,
                    Foreground = (SolidColorBrush) new BrushConverter().ConvertFromString(conf.FontColor),
                    FontFamily = (FontFamily) new FontFamilyConverter().ConvertFromString(conf.Font),
                    FontSize   = conf.FontSize
                };
                if (conf.A11)
                {
                    textBlock.HorizontalAlignment = HorizontalAlignment.Left;
                    textBlock.VerticalAlignment   = VerticalAlignment.Top;
                }
                if (conf.A12)
                {
                    textBlock.HorizontalAlignment = HorizontalAlignment.Center;
                    textBlock.VerticalAlignment   = VerticalAlignment.Top;
                }
                if (conf.A13)
                {
                    textBlock.HorizontalAlignment = HorizontalAlignment.Right;
                    textBlock.VerticalAlignment   = VerticalAlignment.Top;
                }
                if (conf.A21)
                {
                    textBlock.HorizontalAlignment = HorizontalAlignment.Left;
                    textBlock.VerticalAlignment   = VerticalAlignment.Center;
                }
                if (conf.A22)
                {
                    textBlock.HorizontalAlignment = HorizontalAlignment.Center;
                    textBlock.VerticalAlignment   = VerticalAlignment.Center;
                }
                if (conf.A23)
                {
                    textBlock.HorizontalAlignment = HorizontalAlignment.Right;
                    textBlock.VerticalAlignment   = VerticalAlignment.Center;
                }
                if (conf.A31)
                {
                    textBlock.HorizontalAlignment = HorizontalAlignment.Left;
                    textBlock.VerticalAlignment   = VerticalAlignment.Bottom;
                }
                if (conf.A32)
                {
                    textBlock.HorizontalAlignment = HorizontalAlignment.Center;
                    textBlock.VerticalAlignment   = VerticalAlignment.Bottom;
                }
                if (conf.A33)
                {
                    textBlock.HorizontalAlignment = HorizontalAlignment.Right;
                    textBlock.VerticalAlignment   = VerticalAlignment.Bottom;
                }

                textBlock.Margin = new Thickness(conf.Margins);
                if (File.Exists(conf.OverlayFile))
                {
                    Image overlay = new Image();
                    overlay.Source = BitmapLoader.Instance.LoadImage(conf.OverlayFile, 0, 0);
                    if (!conf.StrechOverlay)
                    {
                        overlay.HorizontalAlignment = textBlock.HorizontalAlignment;
                        overlay.VerticalAlignment   = textBlock.VerticalAlignment;
                        overlay.Stretch             = Stretch.None;
                    }
                    else
                    {
                        overlay.HorizontalAlignment = HorizontalAlignment.Stretch;
                        overlay.VerticalAlignment   = VerticalAlignment.Stretch;
                        overlay.Stretch             = Stretch.UniformToFill;
                    }
                    grid.Children.Add(overlay);
                    grid.UpdateLayout();
                }
                grid.Children.Add(textBlock);
                grid.UpdateLayout();

                BitmapLoader.Save2Jpg(
                    BitmapLoader.SaveImageSource(grid, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight), dest);
            }
            return(dest);
        }