Ejemplo n.º 1
0
        /// <summary>
        /// Gets a render of the current UIElement
        /// </summary>
        /// <param name="source">UIElement to screenshot</param>
        /// <param name="dpi">The DPI of the source.</param>
        /// <param name="size">The size of the destination image.</param>
        /// <returns>An ImageSource</returns>
        public static RenderTargetBitmap GetRender(this Grid source, double dpi, System.Windows.Size?size = null)
        {
            var bounds = VisualTreeHelper.GetDescendantBounds(source);

            var scale  = Math.Round(dpi / 96d, 2);
            var width  = size?.Width ?? (bounds.Width + bounds.X) * scale;
            var height = size?.Height ?? (bounds.Height + bounds.Y) * scale;

            var rtb = new RenderTargetBitmap((int)Math.Round(width), (int)Math.Round(height), dpi, dpi, PixelFormats.Pbgra32);

            var dv = new DrawingVisual();

            using (var ctx = dv.RenderOpen())
            {
                var vb = new VisualBrush(source);

                var locationRect = new System.Windows.Point(bounds.X, bounds.Y);
                var sizeRect     = new System.Windows.Size(bounds.Width, bounds.Height);

                ctx.DrawRectangle(vb, null, new Rect(locationRect, sizeRect));
            }

            rtb.Render(dv);
            return((RenderTargetBitmap)rtb.GetAsFrozen());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a render of the current UIElement
        /// </summary>
        /// <param name="source">UIElement to screenshot</param>
        /// <param name="dpi">The DPI of the source.</param>
        /// <param name="size">The size of the destination image.</param>
        /// <returns>An ImageSource</returns>
        public static RenderTargetBitmap GetRender(this Grid source, double dpi, System.Windows.Size size)
        {
            Rect bounds = VisualTreeHelper.GetDescendantBounds(source);

            var scale  = dpi / 96.0;
            var width  = (bounds.Width + bounds.X) * scale;
            var height = (bounds.Height + bounds.Y) * scale;

            var rtb = new RenderTargetBitmap((int)Math.Round(width, MidpointRounding.AwayFromZero),
                                             (int)Math.Round(height, MidpointRounding.AwayFromZero), dpi, dpi, PixelFormats.Pbgra32);

            DrawingVisual dv = new DrawingVisual();

            using (DrawingContext ctx = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(source);

                var locationRect = new System.Windows.Point(bounds.X, bounds.Y);
                var sizeRect     = new System.Windows.Size(bounds.Width, bounds.Height);

                ctx.DrawRectangle(vb, null, new Rect(locationRect, sizeRect));
            }

            rtb.Render(dv);
            return((RenderTargetBitmap)rtb.GetAsFrozen());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Called when the user drags the title bar when maximized.
        /// </summary>
        private void OnBorderMouseMove(object sender, MouseEventArgs e)
        {
            var window = (Window)((FrameworkElement)sender).TemplatedParent;

            if (window != null)
            {
                if (e.LeftButton == MouseButtonState.Pressed && window.WindowState == WindowState.Maximized)
                {
                    System.Windows.Size maxSize = new System.Windows.Size(window.ActualWidth, window.ActualHeight);
                    System.Windows.Size resSize = window.RestoreBounds.Size;

                    double curX = e.GetPosition(window).X;
                    double curY = e.GetPosition(window).Y;

                    double newX = curX / maxSize.Width * resSize.Width;
                    double newY = curY;

                    window.WindowState = WindowState.Normal;

                    window.Left = curX - newX;
                    window.Top  = curY - newY;
                    window.DragMove();
                }
            }
        }
Ejemplo n.º 4
0
        private void OnTeraScreenChanged(System.Drawing.Point oldPos, System.Drawing.Point newPos, Size size)
        {
            var op = new Point(oldPos.X, oldPos.Y);                    //sigh
            var np = new Point(newPos.X, newPos.Y);                    //sigh
            var s  = new System.Windows.Size(size.Width, size.Height); //sigh

            WindowSettings.ApplyScreenOffset(op, np, s);
            ReloadPosition();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets a render of the current UIElement
        /// </summary>
        /// <param name="source">UIElement to screenshot</param>
        /// <param name="scale">The scale of the screen.</param>
        /// <param name="dpi">The DPI of the output.</param>
        /// <param name="size">The size of the destination image.</param>
        /// <returns>An ImageSource</returns>
        public static RenderTargetBitmap GetScaledRender(this UIElement source, double scale, double dpi, System.Windows.Size size)
        {
            var bounds = VisualTreeHelper.GetDescendantBounds(source);

            //var width = (bounds.Width + bounds.X) * scale;
            //var height = (bounds.Height + bounds.Y) * scale;

            #region If no bounds

            if (bounds.IsEmpty)
            {
                var control = source as FrameworkElement;

                if (control != null)
                {
                    bounds = new Rect(new System.Windows.Point(0d, 0d), new System.Windows.Point(control.ActualWidth * scale, control.ActualHeight * scale));
                }
            }

            #endregion

            var rtb = new RenderTargetBitmap((int)Math.Round(size.Width), (int)Math.Round(size.Height), dpi, dpi, PixelFormats.Pbgra32);

            source.Clip         = new RectangleGeometry(new Rect(0, 0, rtb.Width, rtb.Height));
            source.ClipToBounds = true;

            var dv = new DrawingVisual();

            using (var ctx = dv.RenderOpen())
            {
                var vb = new VisualBrush(source)
                {
                    AutoLayoutContent = false,
                    Stretch           = Stretch.None
                };

                //I still need to fix this, when there's an element outside the bounds, it gets stretched.
                //var locationRect = new System.Windows.Point(0 * scale, 0 * scale);
                //var sizeRect = new System.Windows.Size(rtb.Width * scale, rtb.Height * scale);

                var locationRect = new System.Windows.Point(bounds.X * scale, bounds.Y * scale);
                var sizeRect     = new System.Windows.Size(bounds.Width * scale, bounds.Height * scale);

                ctx.DrawRectangle(vb, null, new Rect(locationRect, sizeRect));
            }

            rtb.Render(dv);
            return((RenderTargetBitmap)rtb.GetAsFrozen());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets a render of the current UIElement
        /// </summary>
        /// <param name="source">UIElement to screenshot</param>
        /// <param name="dpi">The DPI of the source.</param>
        /// <returns>An ImageSource</returns>
        public static RenderTargetBitmap GetRender(this UIElement source, double dpi)
        {
            var bounds = VisualTreeHelper.GetDescendantBounds(source);

            //TODO: Fix bounds when values are not rounded.

            var scale  = Math.Round(dpi / 96d, 2);
            var width  = (bounds.Width + bounds.X) * scale;
            var height = (bounds.Height + bounds.Y) * scale;

            #region If no bounds

            if (bounds.IsEmpty)
            {
                var control = source as Control;

                if (control != null)
                {
                    width  = control.ActualWidth * scale;
                    height = control.ActualHeight * scale;
                }

                bounds = new Rect(new System.Windows.Point(0d, 0d), new System.Windows.Point(width, height));
            }

            #endregion

            var rtb = new RenderTargetBitmap((int)Math.Round(width), (int)Math.Round(height), dpi, dpi, PixelFormats.Pbgra32);

            var dv = new DrawingVisual();
            using (var ctx = dv.RenderOpen())
            {
                var vb = new VisualBrush(source);

                var locationRect = new System.Windows.Point(bounds.X, bounds.Y);
                var sizeRect     = new System.Windows.Size((int)Math.Round(bounds.Width), (int)Math.Round(bounds.Height));

                ctx.DrawRectangle(vb, null, new Rect(locationRect, sizeRect));
            }

            rtb.Render(dv);
            return((RenderTargetBitmap)rtb.GetAsFrozen());
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets a render of the current UIElement
        /// </summary>
        /// <param name="source">UIElement to screenshot</param>
        /// <param name="dpi">The DPI of the source.</param>
        /// <param name="size">The size of the destination image.</param>
        /// <returns>An ImageSource</returns>
        public static RenderTargetBitmap GetRender(this UIElement source, double dpi, System.Windows.Size size)
        {
            Rect bounds = VisualTreeHelper.GetDescendantBounds(source);

            var scale  = dpi / 96.0;
            var width  = (bounds.Width + bounds.X) * scale;
            var height = (bounds.Height + bounds.Y) * scale;

            #region If no bounds

            if (bounds.IsEmpty)
            {
                var control = source as Control;

                if (control != null)
                {
                    width  = control.ActualWidth * scale;
                    height = control.ActualHeight * scale;
                }

                bounds = new Rect(new System.Windows.Point(0d, 0d), new System.Windows.Point(width, height));
            }

            #endregion

            var rtb = new RenderTargetBitmap((int)Math.Round(width, MidpointRounding.AwayFromZero),
                                             (int)Math.Round(height, MidpointRounding.AwayFromZero), dpi, dpi, PixelFormats.Pbgra32);

            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext ctx = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(source);

                var locationRect = new System.Windows.Point(bounds.X, bounds.Y);
                var sizeRect     = new System.Windows.Size(bounds.Width, bounds.Height);

                ctx.DrawRectangle(vb, null, new Rect(locationRect, sizeRect));
            }

            rtb.Render(dv);
            return((RenderTargetBitmap)rtb.GetAsFrozen());
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets a render of the current UIElement
        /// </summary>
        /// <param name="source">UIElement to screenshot</param>
        /// <param name="scale">The scale of the UI element.</param>
        /// <param name="dpi">The DPI of the source.</param>
        /// <param name="size">The size of the destination image.</param>
        /// <returns>An ImageSource</returns>
        public static RenderTargetBitmap GetScaledRender(this Grid source, double scale, double dpi, System.Windows.Size size)
        {
            var rtb = new RenderTargetBitmap((int)Math.Round(size.Width), (int)Math.Round(size.Height), dpi, dpi, PixelFormats.Pbgra32);

            var dv = new DrawingVisual();

            using (var ctx = dv.RenderOpen())
            {
                var vb = new VisualBrush(source);

                //Gets the child bounds.
                var bounds       = VisualTreeHelper.GetDescendantBounds(source);
                var locationRect = new System.Windows.Point(bounds.X * scale, bounds.Y * scale);
                var sizeRect     = new System.Windows.Size(bounds.Width * scale, bounds.Height * scale);

                ctx.DrawRectangle(vb, null, new Rect(locationRect, sizeRect));
            }

            rtb.Render(dv);
            return((RenderTargetBitmap)rtb.GetAsFrozen());
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets a render of the current UIElement
        /// </summary>
        /// <param name="source">UIElement to screenshot</param>
        /// <param name="scale"></param>
        /// <param name="dpi">The DPI of the source.</param>
        /// <param name="size">The size of the destination image.</param>
        /// <returns>An ImageSource</returns>
        public static RenderTargetBitmap GetScaledRender(this UIElement source, double scale, double dpi, System.Windows.Size size)
        {
            var bounds = VisualTreeHelper.GetDescendantBounds(source);

            //var width = (bounds.Width + bounds.X) * scale;
            //var height = (bounds.Height + bounds.Y) * scale;

            #region If no bounds

            if (bounds.IsEmpty)
            {
                var control = source as FrameworkElement;

                //if (control != null)
                //{
                var width  = control.ActualWidth * scale;
                var height = control.ActualHeight * scale;
                //}

                bounds = new Rect(new System.Windows.Point(0d, 0d), new System.Windows.Point(width, height));
            }

            #endregion

            var rtb = new RenderTargetBitmap((int)Math.Round(size.Width), (int)Math.Round(size.Height), dpi, dpi, PixelFormats.Pbgra32);

            var dv = new DrawingVisual();
            using (var ctx = dv.RenderOpen())
            {
                var vb = new VisualBrush(source);

                var locationRect = new System.Windows.Point(bounds.X * scale, bounds.Y * scale);
                var sizeRect     = new System.Windows.Size(bounds.Width * scale, bounds.Height * scale);

                ctx.DrawRectangle(vb, null, new Rect(locationRect, sizeRect));
            }

            rtb.Render(dv);
            return((RenderTargetBitmap)rtb.GetAsFrozen());
        }
Ejemplo n.º 10
0
        /// <summary>
        ///     Convert any control to a PngBitmapEncoder
        /// </summary>
        /// <param name="controlToConvert"> The control to convert to an ImageSource </param>
        /// <returns> The returned ImageSource of the controlToConvert </returns>
        /// <see cref="http://www.dreamincode.net/code/snippet4326.htm" />
        public static PngBitmapEncoder GetImageFromControl(FrameworkElement controlToConvert)
        {
            // get size of control
            var sizeOfControl = new System.Windows.Size(controlToConvert.ActualWidth, controlToConvert.ActualHeight);
            // measure and arrange the control
            controlToConvert.Measure(sizeOfControl);
            // arrange the surface
            controlToConvert.Arrange(new Rect(sizeOfControl));

            // craete and render surface and push bitmap to it
            var renderBitmap = new RenderTargetBitmap((Int32)sizeOfControl.Width, (Int32)sizeOfControl.Height, 96d, 96d, PixelFormats.Pbgra32);
            // now render surface to bitmap
            renderBitmap.Render(controlToConvert);
            // encode png data
            var pngEncoder = new PngBitmapEncoder();
            // puch rendered bitmap into it
            pngEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));
            //pngEncoder.Metadata = new BitmapMetadata("png") {
            //                                                    ApplicationName = "Marvelous",
            //                                                    DateTaken = DateTime.Now.ToString(CultureInfo.InvariantCulture),
            //                                                    Subject = "Casual Loop Analysis using Marvel",
            //                                                    Title = "Marvelous screenshot",
            //                                                    Author = new ReadOnlyCollection<string>(new List<string> {
            //                                                                                                                 Properties.Settings.Default.UserName
            //                                                                                                             })
            //                                                };

            // return encoder
            return pngEncoder;
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Gets a render of the current UIElement
        /// </summary>
        /// <param name="source">UIElement to screenshot</param>
        /// <param name="scale">The scale of the screen.</param>
        /// <param name="dpi">The DPI of the output.</param>
        /// <param name="size">The size of the destination image.</param>
        /// <returns>An ImageSource</returns>
        public static RenderTargetBitmap GetScaledRender(this UIElement source, double scale, double dpi, System.Windows.Size size)
        {
            var bounds = VisualTreeHelper.GetDescendantBounds(source);

            //var width = (bounds.Width + bounds.X) * scale;
            //var height = (bounds.Height + bounds.Y) * scale;

            #region If no bounds

            if (bounds.IsEmpty)
            {
                if (source is FrameworkElement control)
                {
                    bounds = new Rect(new System.Windows.Point(0d, 0d), new System.Windows.Point(control.ActualWidth * scale, control.ActualHeight * scale));
                }
            }

            #endregion

            var rtb = new RenderTargetBitmap((int)Math.Round(size.Width), (int)Math.Round(size.Height), dpi, dpi, PixelFormats.Pbgra32);

            //source.Clip = new RectangleGeometry(new Rect(0, 0, rtb.Width, rtb.Height));
            //source.ClipToBounds = true;

            var dv = new DrawingVisual();

            using (var ctx = dv.RenderOpen())
            {
                var vb = new VisualBrush(source)
                {
                    AutoLayoutContent = false,
                    Stretch           = Stretch.Fill
                };

                var uiScale = source.Scale();

                //Test with high dpi.
                //For some reason, an InkCanvas with Strokes going beyond the bounds will report a strange bound even if clipped.
                if (bounds.Width > size.Width / uiScale)
                {
                    bounds.Width = size.Width / uiScale;
                }

                if (bounds.Height > size.Height / uiScale)
                {
                    bounds.Height = size.Height / uiScale;
                }

                if (bounds.X < 0)
                {
                    bounds.X = 0;
                }

                if (bounds.Y < 0)
                {
                    bounds.Y = 0;
                }

                var locationRect = new System.Windows.Point(bounds.X * scale, bounds.Y * scale);
                var sizeRect     = new System.Windows.Size(bounds.Width * scale, bounds.Height * scale);

                ctx.DrawRectangle(vb, null, new Rect(locationRect, sizeRect));
            }

            rtb.Render(dv);

            //source.Clip = null;

            return((RenderTargetBitmap)rtb.GetAsFrozen());
        }
Ejemplo n.º 12
0
	private void InsertPictureLayer (DataRow PictureRow, DrawingGroup FramingGroup,
					DataRow ConnectPicturesRow, double OriginalWidth, double OriginalHeight,
					String LocalWPMediaRoot, DataTable MaterialTable)
		{

		System.Windows.Point StartingPoint = new System.Windows.Point
			((Convert.ToInt32 (PictureRow ["PositionLeft"]) * OriginalWidth) / 100,
			(Convert.ToInt32 (PictureRow ["PositionTop"]) * OriginalHeight) / 100);
		bool FixPositioned = false;
		if (ConnectPicturesRow ["PositionedByFileName"] != null)
			if (ConnectPicturesRow ["PositionedByFileName"].ToString () == "Yes")
				{
				StartingPoint.X = (Convert.ToInt32 (ConnectPicturesRow ["XPositioningPercentage"]) * OriginalWidth) / 100;
				StartingPoint.Y = (Convert.ToInt32 (ConnectPicturesRow ["YPositioningPercentage"]) * OriginalHeight) / 100;
				FixPositioned = true;
				}
		String PhysicalFileName = ConnectPicturesRow ["PictureFileName"].ToString ();
		Stream PictureSource = null;
		if (MaterialTable != null)
			{
			DataRow [] Materials = MaterialTable.Select ("NameID = '" + PhysicalFileName + "'");
			if (Materials.Length != 1)
				return;
			PictureSource = new MemoryStream (Materials [0] ["MaterialByteArray"] as Byte []);
			}
		else
			{
			if (!Path.IsPathRooted (PhysicalFileName))
				PhysicalFileName
					= Path.Combine (LocalWPMediaRoot, PhysicalFileName);
			if (String.IsNullOrEmpty (PhysicalFileName))
				return;
			if (!File.Exists (PhysicalFileName))
				return;
			PictureSource = new FileStream (PhysicalFileName, FileMode.Open, FileAccess.Read);
			
			}


		DrawingGroup PictureDrawingGroup = new DrawingGroup ();


		BitmapImage PicturePart = new BitmapImage ();
		PicturePart.BeginInit ();
		PicturePart.CacheOption = BitmapCacheOption.OnLoad;
		PicturePart.StreamSource = PictureSource;
		PicturePart.EndInit ();

		PictureSource.Close ();
		PictureSource.Dispose ();

		double RealWidth = PicturePart.PixelWidth;
		double RealHeight = PicturePart.PixelHeight;
		double RealAspectRatio = RealWidth / RealHeight;
		double MeasuredWidth = PicturePart.Width;
		double MeasuredHeight = PicturePart.Height;


		double PositionLeft = Convert.ToDouble (PictureRow ["PositionLeft"]);
		double PositionTop = Convert.ToDouble (PictureRow ["PositionTop"]);
		double PositionRight = Convert.ToDouble (PictureRow ["PositionRight"]);
		double PositionBottom = Convert.ToDouble (PictureRow ["PositionBottom"]);

		double TargetWidth = ((PositionRight - PositionLeft) * OriginalWidth) / 100;
		double TargetHeight = ((PositionBottom - PositionTop) * OriginalHeight) / 100;
		double PositioningFactor = 1;
		if (!FixPositioned)
			{
			double FramingAspectRatio = TargetWidth/TargetHeight;
			if (FramingAspectRatio >= RealAspectRatio)
				{
				RealHeight = TargetHeight;
				RealWidth = RealHeight * RealAspectRatio;
				}
			else
				{
				RealWidth = TargetWidth;
				RealHeight = RealWidth / RealAspectRatio;
				}

			}


		System.Windows.Point DrawingStartingPoint = StartingPoint;
		System.Windows.Size DrawingSize = new System.Windows.Size
			(RealWidth, RealHeight);
		double TargetAspectRatio = DrawingSize.Width / DrawingSize.Height;
		if (TargetAspectRatio >= RealAspectRatio)
			{
			DrawingSize.Width = DrawingSize.Height*RealAspectRatio;
			if (!FixPositioned)
				{
				DrawingStartingPoint.X += (TargetWidth - DrawingSize.Width) / 2;
				}
			else
				{
				}
			}
		else
			{
			DrawingSize.Height = DrawingSize.Width / RealAspectRatio;
			if (!FixPositioned)
				{
				DrawingStartingPoint.Y += (TargetHeight - DrawingSize.Height) / 2;
				}
			else
				{
				}
			}
		ImageDrawing PositionableEntry = new ImageDrawing();
		PositionableEntry.ImageSource = PicturePart; ;
		PositionableEntry.Rect = new Rect (DrawingStartingPoint, DrawingSize);
		PictureDrawingGroup.Children.Add (PositionableEntry);
		PictureDrawingGroup.Opacity = Convert.ToDouble (PictureRow ["Transparency"])/100;
		FramingGroup.Children.Add (PictureDrawingGroup);
		PictureDrawingGroup.Freeze ();
		InsertDisposeableElement (PositionableEntry);
		InsertDisposeableElement (PictureDrawingGroup);
		InsertDisposeableElement (PicturePart);
		}