public void SetTargetSizeToCropHandler (CropDefinitionUISupport CropSupport, BitmapSource Source) { if (TargetSizeType == TypeOfTargetSize.AsItIs) { Width = (int) Source.Width; Height = (int) Source.Height; CropToMinWidth = (int) Source.Width; CropToMinHeight = (int) Source.Height; CropSupport.AspectRatio = new Size (Width, Height); CropSupport.Coordinate = new System.Windows.Point (0, 0); CropSupport.Width = (int) Source.Width; CropSupport.Height = (int) Source.Height; return; } double Percentage = 0.9; if (TargetSizeType == TypeOfTargetSize.FixedRelation) { Width = 600; Height = 400; } if (TargetSizeType == TypeOfTargetSize.UserDefined) { Width = CropToMinWidth; Height = CropToMinHeight; Percentage = 1.0; } CropSupport.AspectRatio = new Size (Width, Height); if (Width > Height) { CropSupport.Width = Source.Width * Percentage; } else { CropSupport.Height = Source.Height * Percentage; } CropSupport.Coordinate = new System.Windows.Point (Source.Width / 2 - CropSupport.Width / 2, Source.Height / 2 - CropSupport.Height / 2); CropSupport.AspectRatio = null; }
private void LoadAll () { TargetSizeSelector.Items.Clear (); foreach (TargetSize Entry in TargetSizes) { ComboBoxItem Item = new ComboBoxItem (); Item.Tag = Entry; Item.Content = Entry.Name; TargetSizeSelector.Items.Add (Item); } TargetSizeSelector.SelectedIndex = 0; ActuallTargetSize = TargetSizes [0]; GeometryData.Text = String.Format ("{0:F} * {1:F}, Rel = {2:F5}", Source.Width, Source.Height, Source.Width / Source.Height); ImageBrush SourceBrush = new ImageBrush (Source); SourceBrush.AlignmentX = AlignmentX.Left; SourceBrush.AlignmentY = AlignmentY.Top; SourceBrush.Stretch = Stretch.None; //Begin Initializing ContentEditor advancedContentPresenter.Background = SourceBrush; advancedContentPresenter.BindSliderToScaleing (LoopSlider); //Begin Initializing CropDefinitionUISupport CropSupport = new CropDefinitionUISupport (); advancedContentPresenter.Children.Add (CropSupport.GetControl ()); CropSupport.Background = new SolidColorBrush (System.Windows.Media.Color.FromArgb (150, 0, 0, 0)); CropSupport.Finished += CropSupport_Finished; CropSupport.BindReverseScalingToContent (); ActuallTargetSize.SetTargetSizeToCropHandler (CropSupport, Source); Auswahl.Finished += new AuswahlControl.AuswahlControlFinishedDelegate (Auswahl_Finished); advancedContentPresenter.FitToBackgroundImage (); LoopSlider.Minimum = LoopSlider.Value / 5; LoopSlider.Maximum = LoopSlider.Value * 5; }
void CropSupport_Finished (CropDefinitionUISupport sender, Rect outerRect, Rect innerRect) { if ((Source.Width < outerRect.X) || ((innerRect.X + innerRect.Width) < 0)) { MessageBox.Show ("Der Begin des ausgeschnittenen Bereiches\r\n" + "liegt links oder rechts außerhalb des Ursprungsbildes"); return; } if ((Source.Height < innerRect.Y) || ((innerRect.Y + innerRect.Height) < 0)) { MessageBox.Show ("Der Begin des ausgeschnittenen Bereiches\r\n" + "liegt oben oder unten außerhalb des Ursprungsbildes"); return; } if (Source.Width < (innerRect.X + innerRect.Width)) { MessageBox.Show ("Das Ende des ausgeschnittenen Bereiches\r\n" + "liegt rechts außerhalb des Ursprungsbildes.\r\n" + "Der Ausschnitt wird verkleinert"); innerRect.Width = Source.Width - innerRect.X; } if (innerRect.X < 0) { MessageBox.Show ("Das Ende des ausgeschnittenen Bereiches\r\n" + "liegt links außerhalb des Ursprungsbildes.\r\n" + "Der Ausschnitt wird verkleinert"); innerRect.Width += innerRect.X; innerRect.X = 0; } if (Source.Height < (innerRect.Y + innerRect.Height)) { MessageBox.Show ("Das Ende des ausgeschnittenen Bereiches\r\n" + "liegt unterhalb des Ursprungsbildes.\r\n" + "Der Ausschnitt wird verkleinert"); innerRect.Height = Source.Height - innerRect.Y; } if (innerRect.Y < 0) { MessageBox.Show ("Das Ende des ausgeschnittenen Bereiches\r\n" + "liegt oberhalb des Ursprungsbildes.\r\n" + "Der Ausschnitt wird verkleinert"); innerRect.Height += innerRect.Y; innerRect.Y = 0; } double conversionX = Source.PixelWidth / Source.Width; double conversionY = Source.PixelHeight / Source.Height; IntermediateCroppedSource = new CroppedBitmap (Source, new Int32Rect ( (int) (innerRect.X * conversionX), (int) (innerRect.Y * conversionY), (int) (innerRect.Width * conversionX), (int) (innerRect.Height * conversionY))); double CropToAspectRatio = (double) ActuallTargetSize.CropToMinWidth / (double) ActuallTargetSize.CropToMinHeight; double IntermediateCroppedAspectRatio = IntermediateCroppedSource.Width / IntermediateCroppedSource.Height; double FinalBitmapWidth = 0; double FinalBitmapHeight = 0; if (ActuallTargetSize.Width > 0) { FinalBitmapWidth = ActuallTargetSize.CropToMinWidth; FinalBitmapHeight = ActuallTargetSize.CropToMinHeight; } else { if (Math.Abs (IntermediateCroppedAspectRatio - CropToAspectRatio) < 0.001) { FinalBitmapWidth = ActuallTargetSize.CropToMinWidth; FinalBitmapHeight = ActuallTargetSize.CropToMinHeight; } else { if (IntermediateCroppedAspectRatio > CropToAspectRatio) { FinalBitmapHeight = ActuallTargetSize.CropToMinHeight; FinalBitmapWidth = ActuallTargetSize.CropToMinHeight * IntermediateCroppedAspectRatio; } else { FinalBitmapWidth = ActuallTargetSize.CropToMinWidth; FinalBitmapHeight = ActuallTargetSize.CropToMinWidth / IntermediateCroppedAspectRatio; } } } double Scale = FinalBitmapWidth / IntermediateCroppedSource.Width; if (Scale > 1) Scale = 1; CroppedSource = new TransformedBitmap (IntermediateCroppedSource, new ScaleTransform (Scale, Scale)); if ((bool) PNGRequested.IsChecked) FormatToCreate = "png"; if ((bool) JPGRequested.IsChecked) FormatToCreate = "jpg"; if ((bool) AsItIsRequested.IsChecked) FormatToCreate = "jpg"; CropSupport.Content = Auswahl.container; CropSupport.BindReverseScalingToContent (); CropSupport.IsMoveable = false; CropSupport.IsResizeable = false; }