How to Play a section of the WPFmediaElement - wpf

I need to play a section of the Mediaelement for example from the second 0:45 to 0:55 sec
I tried using the mediaelement position property here is the code
Dim mytimeline As New MediaTimeline
Dim mc As MediaClock
FReveal1.Visibility = Windows.Visibility.Visible
FReveal1.Source = New Uri("videos\Front-Game-Answer-Reveals.wmv", UriKind.Relative)
FReveal1.Position = New TimeSpan(0, 0, 4)
mytimeline.Source = FReveal1.Source
mytimeline.Duration = (TimeSpan.FromMilliseconds(5000))
mc = myTimeLine.CreateClock()
FReveal1.Clock = mc
FReveal1.Play()
But I get the following error : "Cannot perform this operation while a clock is assigned to the media player."
Is there another way to do this ?
Thanks

Would something like this work?
var StartTime = new TimeSpan(0, 0, 45);
var EndTime = new TimeSpan(0, 0, 55);
theMediaElement.Position = StartTime;
theMediaElement.Play();
while (theMediaElement.Position != EndTime)
{
// chill and let it play
}
theMediaElement.Stop();
EDIT: I should probably add that this is a bit cowboy.

Related

Is it possible to turn off antialiasing when using ImageBrush tiling?

Is it possible to turn off anti-aliasing in WPF when using an ImageBrush?
Given the following code:
var handleImage = new BitmapImage(new Uri($"pack://application:,,,/Resources/myimage.png"));
var imageBrush = new ImageBrush(handleImage);
imageBrush.AlignmentY = AlignmentY.Top;
imageBrush.AlignmentX = AlignmentX.Left;
imageBrush.Stretch = Stretch.Uniform;
imageBrush.Viewport = new Rect(0, 0, _handleImage.Width, _handleImage.Height);
imageBrush.ViewportUnits = BrushMappingMode.Absolute;
imageBrush.TileMode = TileMode.Tile;
drawingContext.DrawRectangle(imageBrush, null, new Rect(0, 0, width, height));
Gives me something like:
But I'm expecting:
WPF's default antialiasing makes it look terrible. I've tried UseLayoutRounding=true, SnapsToDevicePixels=true, RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality),
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.NearestNeighbor) RenderOptions.SetEdgeMode(this, EdgeMode.Unspecified); on the window. The only one that changes any visual difference is BitmapScalingMode.NearestNeighbor however it still looks odd and the tiling overlaps itself.
EDIT: Download full working sample: WpfImageBrushExample.zip
The problem here was that the BitmapImage width and height were different fractional numbers - in this case it was 5.333 x 6x666 instead of the expected 4 x 5 pixels. If I use _handleImage.PixelWidth and _handleImage.PixelHeight the problem is fixed and I don't get weird aliasing under any of those rendering options set.
var handleImage = new BitmapImage(new Uri($"pack://application:,,,/Resources/myimage.png"));
var imageBrush = new ImageBrush(handleImage);
imageBrush.AlignmentY = AlignmentY.Top;
imageBrush.AlignmentX = AlignmentX.Left;
imageBrush.Stretch = Stretch.Uniform;
imageBrush.Viewport = new Rect(0, 0, _handleImage.PixelWidth, _handleImage.PixelHeight);
imageBrush.ViewportUnits = BrushMappingMode.Absolute;
imageBrush.TileMode = TileMode.Tile;
drawingContext.DrawRectangle(imageBrush, null, new Rect(0, 0, width, height));
Produces:

DispacherTimer, Count up, visible in 00:00:00 Format in textbox Windows Phone

How to make format like this hh:mm:ss ? ?
Im really tired of getting this up : ) i hope You help me someone.
public void StartTimer()
{
System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 100); // 100 Milliseconds
myDispatcherTimer.Tick += new EventHandler(Each_Tick);
myDispatcherTimer.Start();
}
int i = 0;
// Raised every 100 miliseconds while the DispatcherTimer is active.
public void Each_Tick(object o, EventArgs sender)
{
textBox2.Text = " " + i++.ToString();
}
First you need to use TimeSpan as you created above.
Just use string formatting function like this:
TimeSpan hundredMilliSec = new TimeSpan(0,0,0,0,100);
textBox2.Text = string.Format(#"{0:hh\:mm\:ss}", hundredMilliSec);
Hope this helps.

WPF Rectangle movement with Animation

I have a requirement of moving dynamically created rectangles dynamically.
I am almost done with my implementation, and if I write the code below for each of the rectangle dynamically, I am able to achieve the desired result.
The code below moves the desired rectangle by 50 pixels on X Axis.
TranslateTransform translateTransform1 = new TranslateTransform(50, 0); aRectangle.RenderTransform = translateTransform1;
The problem is I want this to be animated. The code below is written to have the same rectanle moved with animation, but gives entirelly different result all together. Any help will be much appreciated. I want it to manage through code as my rectangles are going to be dynamic.
TranslateTransform translateTransform1 = new TranslateTransform(50, 0);
Duration duration = new Duration(new TimeSpan(0, 0, 0, 1, 0));
DoubleAnimation anim = new DoubleAnimation(30, duration);
translateTransform1.BeginAnimation(TranslateTransform.XProperty, anim);
aRectangle.RenderTransform = translateTransform1;
Try
Duration duration = new Duration(new TimeSpan(0, 0, 0, 1, 0));
DoubleAnimation anim = new DoubleAnimation(30, duration);
aRectangle.RenderTransform = new TranslateTransform();
aRectangle.BeginAnimation(TranslateTransform.XProperty, anim);

WPF thickness keyframe not working

I used to animate my border with a from to animation but now I wanted to add a thicknesskeyframe to make it look better. I'm using .net 4.0 and vb.
Dim t As New ThicknessAnimationUsingKeyFrames()
t.RepeatBehavior = RepeatBehavior.Forever
Dim instance As ThicknessKeyFrame
instance.Value = New Thickness(0, 0, 0, 0)
instance.KeyTime = TimeSpan.FromSeconds(0.0)
t.KeyFrames.Add(instance)
Dim instance2 As ThicknessKeyFrame
instance2.Value = New Thickness(1, 1, 1, 1)
instance2.KeyTime = TimeSpan.FromSeconds(0.5)
t.KeyFrames.Add(instance)
I can't instantiate the thickness keyframe I think it's an interface but I don't know how to use it in this context.
fistly, you need to instantiate your instances
Dim instance As New DiscreteThicknessKeyFrame()
the bottom line is incorrect:
t.KeyFrames.Add(instance)
it should be
t.KeyFrames.Add(instance2)

Saving an Image from WPF's WebBrowser control - how do you do it?

everyone. There's probably a simple solution to this but I can't seem to find one. I'm playing around with the WebBrowser control in WPF that ships with Visual Studio 2010 and am trying to save an image that might appear on a webpage to disk programmatically.
Many thanks in advance!
Luck
Add System.Drawing as reference and perform the following oprations in the method that should capture the image:
Rect bounds = VisualTreeHelper.GetDescendantBounds(browser1);
System.Windows.Point p0 = browser1.PointToScreen(bounds.TopLeft);
System.Drawing.Point p1 = new System.Drawing.Point((int)p0.X, (int)p0.Y);
Bitmap image = new Bitmap((int)bounds.Width, (int)bounds.Height);
Graphics imgGraphics = Graphics.FromImage(image);
imgGraphics.CopyFromScreen(p1.X, p1.Y,
0, 0,
new System.Drawing.Size((int)bounds.Width,
(int)bounds.Height));
image.Save("C:\\a.bmp", ImageFormat.Bmp);
Here are the adaptions to solution of #luvieere:
WebBrowser browser1;
browser1 = this.Browser;
// I used the GetContentBounds()
Rect bounds = VisualTreeHelper.GetContentBounds(browser1);
// and the point to screen command for the top-left and the bottom-right corner
System.Windows.Point pTL = browser1.PointToScreen(bounds.TopLeft);
System.Windows.Point pBR = browser1.PointToScreen(bounds.BottomRight);
System.Drawing.Bitmap image = new
// The size is then calculated as difference of the two corners
System.Drawing.Bitmap(
System.Convert.ToInt32(pBR.X - pTL.X),
System.Convert.ToInt32(pBR.Y - pTL.Y));
System.Drawing.Graphics imgGraphics = System.Drawing.Graphics.FromImage(image);
imgGraphics.CopyFromScreen(pTL.X, pTL.Y, 0, 0, new System.Drawing.Size(image.Width, image.Height));
fileName = System.IO.Path.GetFileNameWithoutExtension(fileName) + ".bmp";
image.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
For those, who prefer VB-dialect
Dim browser1 As WebBrowser
browser1 = Me.Browser
Dim bounds As Rect = VisualTreeHelper.GetContentBounds(browser1)
Dim pTL As System.Windows.Point = browser1.PointToScreen(bounds.TopLeft)
Dim pBR As System.Windows.Point = browser1.PointToScreen(bounds.BottomRight)
Dim image As System.Drawing.Bitmap = New System.Drawing.Bitmap(CInt(pBR.X - pTL.X), CInt(pBR.Y - pTL.Y))
Dim imgGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(image)
imgGraphics.CopyFromScreen(pTL.X, pTL.Y, 0, 0, New System.Drawing.Size(image.Width, image.Height))
fileName = IO.Path.GetFileNameWithoutExtension(fileName) & ".bmp"
image.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp)

Resources