how to capture part of the screen in Silverlight - silverlight

i want to do a screen capture of a running silverlight 3 application, from within the app, and then i want to present this to the user as a thumbnail, say in an Image control.
am i dreaming?

For a simple page:
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel>
<Ellipse Fill="Red" Width="100" Height="100"></Ellipse>
<Button x:Name="btnCapture" Click="btnCapture_Click" Width="30" Height="25"></Button>
<Image x:Name="imgThumbnail" Width="50" Height="50"></Image>
</StackPanel>
</Grid>
with the event handler:
private void btnCapture_Click(object sender, RoutedEventArgs e)
{
WriteableBitmap bmp = new WriteableBitmap(LayoutRoot, null);
this.imgThumbnail.Source = bmp;
}

You are dreaming if you want to do a true screen capture (outside the plugin).
The WriteableBitmap answer is correct if you just want to capture a partial or complete visual tree rendering of the Silverlight app only.

Related

WPF - show an image and set different clickable areas on it

the case is this one:
I have an image representing a schema, let's say a cross like the following
I need to include the image in a WPF UserControl and let the user click on each of the branches (red, green or blue...) and, according to the branch selected, do something different.
What would be the best way to solve this?
I tried with canvas but I don't find a way to trace correctly the background image with shapes (also because the real image is not so simple as the sample cross here)
thanks for any suggestion
It depends on the comlexity of shapes but it is not difficult to find the shape where mouse up event is fired by VisualTreeHelper.HitTest method.
Let's say there is a Canvas which has two Rectangles inside.
<Canvas Background="Transparent"
PreviewMouseUp="Canvas_PreviewMouseUp">
<Rectangle x:Name="Rect1" Canvas.Left="20" Canvas.Top="20"
Width="20" Height="20" Fill="Red"/>
<Rectangle x:Name="Rect2" Canvas.Left="80" Canvas.Top="80"
Width="20" Height="20" Fill="Green"/>
</Canvas>
Catching its PreviewMouseUp event, you can tell the Rectangle where that event is fired.
private void Canvas_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
var position = e.GetPosition((IInputElement)sender);
var result = VisualTreeHelper.HitTest((Visual)sender, position);
if (result.VisualHit is Rectangle rect)
{
Debug.WriteLine($"Hit {rect.Name}");
}
}

How could one let the Manipulation or GestureListener work in the page which contains a WebBrowser in it?

My application page contains a WebBrowser. I want to add a GestureListener in order to handle the Flick event. But when Flick on the WebBrowser region, it doesn't work. I have tried many ways to let it work, but I failed. I have also tried to use Manipulation instead, but to no effect.
Could someone tell me how to do or whether there is another solution instead?
Following code is the working solution for the Flick or Hold event on the WebBrowser control.
Try the same, it may help you.
Assuming xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" being present in the phone:PhoneApplicationPage tag.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0" Grid.RowSpan="2">
<phone:WebBrowser x:Name="myWebBrowser" Visibility="Visible" IsScriptEnabled="True" IsHitTestVisible="True" Margin="-12,6,0,6" />
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener Flick="GestureListener_Flick" Hold="GestureListener_Hold"></toolkit:GestureListener>
</toolkit:GestureService.GestureListener>
</Grid>
And xaml.cs has functions as follows.
private void GestureListener_Flick(object sender, FlickGestureEventArgs e)
{
MessageBox.Show("Flick");
}
private void GestureListener_Hold(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
{
MessageBox.Show("Hold");
}

databinding in silverlight

I have a xaml with a button like this:
Button.xaml
<Grid x:Name="LayoutRoot" >
<StackPanel >
<Button Content="Button1" Click="Button1_Click" />
</StackPanel >
</Grid>
and Button.xaml.cs:
private void Button1_Click(object sender, RoutedEventArgs e)
{
// Get a instance of ClientOversikt
CustomerView childWindow = m_container.Resolve<CustomerView >();
childWindow.Show();
}
It's working fine. But I want to use Databinding in Button.xaml instead of Click="Button1_Click". How could I do it?
I appreciate all the help
Since you're using Silverlight 4, you can use commands. You bind the Command property of the Button to an instance of ICommand, which will open the child window when executed. Then, when you click on the button, the command will be executed.
This page contains a reasonably good introduction to commanding.

Silverlight 4 web cam permission on load of User Control

I am using Silverlight 4 to access the web cam. Everything works ok when I start the web cam on a button click event, I get the prompt for permission. I would like the web cam to start when User Control loads, but for some reason when I run the same code on the Loaded event, I don't get a prompt when executing the following code:'
CaptureDeviceConfiguration.RequestDeviceAccess()
Does anyone have a work around for this?
I found a workaround to the problem. I am automatically clicking the button that starts the webcam streaming on the Loaded event of the control.
ButtonAutomationPeer peer = new ButtonAutomationPeer(btnStartWebcam);
IInvokeProvider invokeProv =
peer.GetPattern(PatternInterface.Invoke)
as IInvokeProvider;
invokeProv.Invoke();
This does the job for me because I don't mind having a button on the UI. But I guess you can create a dummy one and hide it if it's necessary.
The security around accessing local devices is very tight. Starting the capture must be preceded by a user action.
Instead of starting the capture from the loaded event, you'll have to move it to a Click event.
Code behind:
public void StartCam()
{
VideoCaptureDevice dev = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
if(CaptureDeviceConfiguration.RequestDeviceAccess() &&
CaptureDeviceConfiguration.AllowedDeviceAccess)
{
CaptureSource capture = new CaptureSource();
capture.VideoCaptureDevice = dev;
VideoBrush videoBrush = new VideoBrush();
videoBrush.SetSource(capture);
capture.Start();
WebCamRectangle.Fill = videoBrush;
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
StartCam();
}
Xaml:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="49*" />
<RowDefinition Height="251*" />
</Grid.RowDefinitions>
<Rectangle Name="WebCamRectangle"
Stroke="Black" StrokeThickness="1" Grid.Row="1" />
<Button Content="Start" Height="25" HorizontalAlignment="Left"
Margin="12,12,0,0" Name="button1" VerticalAlignment="Top"
Width="135" Click="button1_Click" />
</Grid>

Why I cannot drop files from explorer to FlowDocumentReader and how to fix it?

I'm trying to implement a piece of functionality that will let the user to drag files into an application to be opened in the FlowDocumentReader.
My problem is that is though I have AllowDrop=true on the FlowDocumentReader, the cursor does not change to the "drop here" icon but changes instead to "drop is not allowed" icon.
This happens only to the FlowDocumentReader, all other parts og the UI (window itself, other controls) work as expected. The FlowDocumentReader actually receives the events, and it is possible to handle the drop, but the user does not have a visual indication that he can release the mouse here.
I also cannot hide the "drop is not allowed" cursor by setting Cursor=Cursors.None
Need to handle DragOver event in FlowDocument to allow dropping here.
xaml:
<!--
<FlowDocumentReader x:Name="fdr" Background="White">
<FlowDocument x:Name="doc" AllowDrop="True" DragEnter="doc_DragOver" Drop="doc_Drop" Background="White"/>
</FlowDocumentReader>
-->
<FlowDocumentReader x:Name="fdr" Background="White">
<FlowDocument x:Name="doc" AllowDrop="True" DragOver="doc_DragOver" Drop="doc_Drop" Background="White"/>
</FlowDocumentReader>
code behind:
private void doc_DragOver(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.All;
e.Handled = true;
}
private void doc_Drop(object sender, DragEventArgs e)
{
}
I couldn't find any direct way to solve this, so here is what I have ended up with:
I placed a grid on top of the FlowDocumentReader. This grid has a sold color, opacity of 0 (transparent) and Visibility=Collapsed. The purpose of this grid is to serve as a drop target.
When FlowDocument within the FlowDocumentReader received the DragEnter event, I switch the grid's visibility to Visible. The grid starts receiving drag events and the cursor stays in the "drop here" form.
When grid receives Drop or DragLeave events, its visibility is turned back to Collapsed to allow the FlowDocument receive mouse events
<FlowDocumentReader x:Name="fdr" Grid.Row="1" Background="White">
<FlowDocument x:Name="doc" DragEnter="doc_DragEnter" Background="White"/>
</FlowDocumentReader>
<Grid x:Name="dtg" Grid.Row="1" Background="White" Opacity="0"
Drop="dtg_Drop" DragLeave="dtg_DragLeave" Visibility="Collapsed"/>

Resources