Drag image within it's parent border? - silverlight

I have created image viewer control, that provide zoom in/out the image, and when the image zoomed and be larger than it's viewer (you can't see all the image) I provided the ability to drag the image (like Windows Photo Viewer) using "TranslateTransform" but I wanna the image to stop dragging when it's border (left or right or left or down) is appeared, I have done some calculations of the width and height of the fourth sides and it's work fine but when you speedy drag the image it stop after it's border pass it's container border, and the distance increased by greater drag speed. For example: open an image in "Windows Photo Viewer" and zoomed it then drag it it's stop exactly when you reach it's border".
So I wanna the image to stop exactly when it's border appeared regardless of drag speed?
Appreciated your Helps,
Best Regards.

This looks like problem with your code. Can you just apply range check when you compute parameters for translate transform to avoid moving picture outside the region?

This is my code I have Image(name: imgView), insode grid(name: grdImage), I wanna the image(zoomed image) to move with mouse move in inside the Grid, and when it's borders reach the border of grid to stop, exactly like "Windows Photo Viewer". FYI: Im zoomed the image using the scaleTransform:
Private Sub imgView_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
_IsMouseCapture = True
imgView.CaptureMouse()
Dim tt = DirectCast(DirectCast(imgView.RenderTransform, TransformGroup).Children.First(Function(tr) TypeOf tr Is TranslateTransform), TranslateTransform)
start = e.GetPosition(grdImage)
origin = New Point(tt.X, tt.Y)
End Sub
Private Sub imgView_MouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
_IsMouseCapture = False
imgView.ReleaseMouseCapture()
End Sub
Private Sub imgView_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
If (Not _IsMouseCapture) Then
Return
End If
Dim tt = DirectCast(DirectCast(imgView.RenderTransform, TransformGroup).Children.First(Function(tr) TypeOf tr Is TranslateTransform), TranslateTransform)
Dim vx As Double = start.X - e.GetSafePosition(grdImage).X
Dim vy As Double = start.Y - e.GetSafePosition(grdImage).Y
tt.Y = origin.Y - vy
tt.X = origin.X - vx
End Sub`

Related

WPF RenderTargetBitmap black bars on right side and bottom

I want to save an image from my WPF user control. It works but I got black bars on the right side and the bottom. If I change the dpiX (96) and dpiY (96) it works but when I maximize the window, it's wrong again (then a bit of the usercontrol is missing ).
This is how I save the image as bitmap:
Dim parentWindow As Window = Window.GetWindow(_Map)
Dim rtb As New RenderTargetBitmap(parentWindow.ActualWidth, parentWindow.ActualHeight, 96, 96, PixelFormats.Pbgra32)
rtb.Render(_Map)
Dim ms As New MemoryStream()
Dim bp As New BmpBitmapEncoder()
bp.Frames.Add(BitmapFrame.Create(rtb))
bp.Save(ms)
Dim saveMap As New Bitmap(ms)
The width and height of the window are a bit larger than your UserControl because it includes the titlebar and borders. if you use the dimensions of your control instead it works fine:
Dim rtb As New RenderTargetBitmap(_Map.ActualWidth, _Map.ActualHeight, 96, 96, PixelFormats.Pbgra32)
I recommend you also make sure the control is layouted properly by calling Measure and Arrange with the current or desired size before rendering.
Additional size calculations may be neccessary if you are using the LayoutTransform or RenderTransform properties of your control.
Btw: WPF always uses 96 DPI for size calculations so you shouldn't change that.

Position WPF Window relative to the calling button

I have a button which opens a new Window. How can I position the new Window relative to that button so that the Window right side is aligned with the button right side and Window top is aligned with the button bottom?
I tried to calculate the positions like this in the calling window, first of all to try make them aligned relative to the calling window itself but it seems not to be correct.
Dim SecondWin As New SecondWindow()
SecondWin.Top = Me.Top + Me.ActualHeight - SecondWin.Height
SecondWin.Left = Me.Left + Me.ActualWidth - SecondWin.Width
SecondWin.Show()
This sounds like a job the Popup control is made for. SO related

Window Location Animation

I'm trying to animate my window's location.
SCENARIO:
I have a window, and when i click on my button, I want the window to move smoothly 100px from it's current location.
EXAMPLE:
Private Sub minimize_button_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles minimize_button.Click
Dim windowsize as integer = window1.top
windowsize = windowsize - 100
End Sub
But with XAML animation. I have no idea how to make this animation in this type of scenario.
Any answer is appreciated, Sincerely VenoMDee.
I don't believe you're going to be able to do this with a XAML animation, but you could still achieve this effect through either a DispatcherTimer or a Storyboard.
With every tick (time-based frame) you can reset the position of the window in code. Just fire the ticks of the timer to reproduce about 40 frames per second (every 150 milliseconds).
Hope that helps!
Here are a bit of a simpler solution:
if your trying to make you animation smooth, you need a Timer, but for every timer1.tick you need to decrease the interval unless it's > screen height. This will make it go faster the further it currently is. I've worked with this method before and the animation is really nice.
Timer1.Interval -= 1
Me.top = Me.Top * 1.05
If Me.Top > My.Computer.Screen.Bounds.Height Then
Timer1.Enabled = False
End If
Hope this helped!

Get Mouse Position on Canvas (But NOT on window)?

I have a project in WPF 4 and vb.net 2010.
I have a canvas inside a window. The window is full screen, but the canvas is set to a solid 640x480 in the center of the window. I need to get the mouse position inside of the canvas, but NOT inside of the window. How do I do this?
Doesn't this work?
Point p = Mouse.GetPosition(canvas);
The position of the mouse pointer is
calculated relative to the specified
element with the upper-left corner of
element being the point of origin,
Hi the important thing is the
NOT on the Window
the canvas is part of the window as well.
one example:
the Window.AllowsTransparency state is on true
the Window.Background is #00000000 (completely transparent)
the Window.Style is None
the Window.State is Maximized and
there are NO controls or elements on the window!
...
so if you start the application you will see Nothing
now tell me how to get the mouseposition on the screen in pixel
!Warning!
if you juse Mouse.GetPosition(this); it will return x0 y0 every time
so I solved the Problem by using System.Windows.Forms.Control.MousePosition it's a bit a mix of wpf and Windows.Forms but I've given up xD.
Sorry for yelling :/
To make it easy for me I made a Extension:
<DebuggerHidden> _
<System.Runtime.CompilerServices.Extension> _
Public Function toWfpPoint(p As System.Drawing.Point) As Point
Return new Point(p.X, p.Y)
End Function
Now I just can juse it like this:
Dim MousPos As Point = System.Windows.Forms.Control.MousePosition.toWfpPoint

WPF: Allow user to resize images in RichTextBox

Is there a method within the RichTextBox control in WPF to allow for the user to resize inserted images, or do you have to devise your own method for this.
What I'm trying to achieve is shown below, a screenshot of a WordPad doing what I want:
Notes:
Reading the RTF file as plain text I find that the control tags related to image size is \picscalex100 and \picscaley100 (where 100 denotes scaled to 100%).
So yeah, is there a proper way or trick to this? Any advice on how to go about programming it? Or am I looking at the wrong control altogether?
Turns out you need to wrap your image in a ResizingAdorner.
A beautiful and simple implementation of this code can be found at http://msdn.microsoft.com/en-us/library/ms771714%28loband%29.aspx by Marco Zhou (second post).
The code for this ResizingAdorner is available as an MSDN sample at http://msdn.microsoft.com/en-us/library/ms771714%28loband%29.aspx
Here's a VB.net equivalent of the code I am now using
Dim img As Image
Sub AddImg() Handles btnAddImage.Click
Dim dlg As New Microsoft.Win32.OpenFileDialog
dlg.Filter = "Image Files(*.*) | *.*"
If dlg.ShowDialog Then
img = New Image
AddHandler img.Loaded, AddressOf imgloaded
img.Source = New BitmapImage(New Uri(dlg.FileName, UriKind.Absolute)) With {.CacheOption = BitmapCacheOption.OnLoad}
Dim container As New BlockUIContainer(img)
rtb.Document.Blocks.Add(container)
End If
End Sub
Private Sub imgloaded(ByVal sender As Object, ByVal e As Windows.RoutedEventArgs)
Dim al As AdornerLayer = AdornerLayer.GetAdornerLayer(img)
If Not (al Is Nothing) Then
al.Add(New SDKSample.ResizingAdorner(img))
End If
End Sub
The ResizingAdorner sample will require some great hacking to meet my needs, but what a great start.
Hope someone else finds this useful!
Maybe copy image to Paint and resize accordingly and then post to the RichTextBox in VB6. Images posted directly to VB6 tend to get distorted. Any image copied from Paint to VB6 is pasted as it was in Paint. I found this out when copying from a PDF image to a RichTextBox.

Resources