How to update picture in c# while code running - c#

string fileName = "Picture.bmp";
byte[] picture = new byte[9000000];
Encoding unicode = Encoding.Unicode;
byte[] PictureBits;
Debug.WriteLine(ip);
s = new SocketHandler(ip, Int32.Parse(port));
PictureBits = s.recive();
File.WriteAllBytes(fileName, PictureBits);
Thread.Sleep(1000); //will sleep for 5 sec
screenShot.Source = new BitmapImage(new Uri(#"bin\Debug\Picture.bmp", UriKind.Relative));
<Window x:Class="RJviewClient.streamImage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RJviewClient"
mc:Ignorable="d"
Title="streamImage" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
<Image Name="screenShot" Source="bin\Debug\s.jpg" HorizontalAlignment="Left" Height="100" Margin="286,177,0,0" VerticalAlignment="Top" Width="100"/>
</Grid>
</Window>
even when I display an image from begging when I run the code the pic disperse
now when the code is running I cant update while it works.
how do I do it?

Related

Why is the chart not updating

I'm using C# library LiveCharts.wpf's CartesianChart to make a chart in a wpf application. The Chart should be updated in Real-Time and I don't understand why it isn't working.
Here is my simple mainwindow, it makes a Graderview
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="13*" x:Name="LeftColoumn"/>
<ColumnDefinition Width="12*" x:Name="RightColumn"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="12*" x:Name="UpperRow"/>
<RowDefinition Height="12*" x:Name="LowerRow"/>
</Grid.RowDefinitions>
<local:GraderView Grid.ColumnSpan="1">
</local:GraderView>
</Grid>
Here is the wpf usercontrol graderView.cs. In the constructor it asynchronously starts a simulation. As this simulation progresses the event handler OnUpdateChartView gets called whenever new values are available.
public partial class GraderView : UserControl
{
public ChartValues<int> GraderOutputCount { get; set; }
public string[] GraderLabels { get; set; }
public Grader Grader { get; set; }
public GraderView()
{
InitializeComponent();
GraderOutputCount = new ChartValues<int>() { 1, 2, 3 };
GraderLabels = new string[20];
Simulator simulator = new Simulator();
simulator.Simulate();
Grader = simulator.Grader;
Grader.UpdateChartView += OnUpdateChartView;
DataContext = this;
}
//Event handler
void OnUpdateChartView(object sender, EventArgs e)
{
Trace.WriteLine("OnUpdateChartView got called");
GraderOutputCount = new ChartValues<int>(Grader.SortedOutputConveyors.Select(x => x.Item2.Count));
GraderLabels = Grader.SortedOutputConveyors.Select(x => x.Item1).ToArray();
for (int i = 0; i < GraderLabels.Length; i++)
{
Trace.WriteLine($"label:{GraderLabels[i]} amount:{GraderOutputCount[i]}");
}
}
}
Because of the Trace.Writelines I was able to verify that the event handler does get called when the app is running. This is for example what appears in the output window:
OnUpdateChartView got called
label:9,5 - 10,5 amount:4
label:8,5 - 9,5 amount:7
label:7,5 - 8,5 amount:9
label:6,5 - 7,5 amount:10
label:5,5 - 6,5 amount:7
label:4,5 - 5,5 amount:8
label:3,5 - 4,5 amount:7
label:2,5 - 3,5 amount:8
label:1,5 - 2,5 amount:7
label:0,5 - 1,5 amount:6
Here is the xaml of this WPF usercontrol GraderView. The Bindings appear correct to me. Values to GraderOutputCount and labels to GraderLabels.
<UserControl x:Class="WpfApp1.GraderView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" d:DataContext="{d:DesignInstance local:GraderView}">
<Grid>
<lvc:CartesianChart>
<lvc:CartesianChart.Series>
<lvc:ColumnSeries Title="Grader" Values="{Binding GraderOutputCount}"/>
</lvc:CartesianChart.Series>
<lvc:CartesianChart.AxisX>
<lvc:Axis Title="WeightClass" Labels="{Binding GraderLabels}"/>
</lvc:CartesianChart.AxisX>
<lvc:CartesianChart.AxisY>
<lvc:Axis Title="BoxCount" />
</lvc:CartesianChart.AxisY>
</lvc:CartesianChart>
</Grid>
My expected result was to first get the 1, 2, 3 that I put in the constructor of the graderview and then as the simulation progresses the chart would update automatically to the newer values in GraderOutputCount and Graderlabels. This is not what actually happens. The chart just doesn't update at all. Can anybody tell me what I am doing wrong here? Thanks in advance
What it looks like
I have found why the problem occured.
I changed
GraderOutputCount = new ChartValues<int>(Grader.SortedOutputConveyors.Select(x => x.Item2.Count));
To
GraderOutputCount.Clear();
foreach(var num in Grader.SortedOutputConveyors.Select(x => x.Item2.Count))
{
GraderOutputCount.Add(num);
}

Html output now show in Usercontrol of WPF

I use this code but html output not display in usercontrol of WPF. Add html code in WebBrowser using by NavigateToString but blank screen is show .and no error occurred.
<UserControl x:Class="WindowsForm_CRM_MDI.View.UserControls.DailyStatsNewUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:local="clr-namespace:WindowsForm_CRM_MDI.View.UserControls"
>
<Grid Background="#EEEEEE">
<WebBrowser HorizontalAlignment="Left"
Height="200"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="200"
x:Name="MainBrowser" />
<Button Content="ok" Click="Button_Click" Height="50" Width="80"></Button> </Grid>
CS Code
private string CreateHTML()
{
StringBuilder builder = new StringBuilder();
builder.AppendLine("<!DOCTYPE html PUBLIC ' -//W3C//DTD XHTML 1.0 Transitional//EN\''http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
builder.AppendLine("<html><head><meta http-equiv='Content - Type' content='text / html; charset = utf - 8' />< meta http - equiv = 'X-UA-Compatible' content = 'IE=edge,chrome=1' /></head>");
builder.AppendLine("<body>");
builder.AppendLine("<a href=\"http://stackexchange.com/users/5852250\">");
builder.AppendLine("<img src=\"http://stackexchange.com/users/flair/5852250.png?theme=dark\" width=\"208\" height=\"58\" alt=\"profile for Felix D. on Stack Exchange, a network of free, community-driven Q&A sites\" title=\"profile for Felix D. on Stack Exchange, a network of free, community-driven Q&A sites\">");
builder.AppendLine("</a>");
builder.AppendLine("</body>");
builder.AppendLine("<html>");
return builder.ToString();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string HtmlToDisplay = CreateHTML();
this.MainBrowser.NavigateToString(HtmlToDisplay);
}

Moving a triangle using given coordinate

I'm working on a project and I want to animate a rectangle to move a certain amount of coordinate that i have set earlier.
Ex: My rectangle is at the position (x=0,y=0). I want with a click of a button to make it move at position (x=150, y=230) in interval of 100 milliseconds. So with one click, it would go to (10,25) at first 100 milliseconds,(20,35) for second 100 milliseconds and so on until the rectangle reach the final position(x=150, y=230)...
I'm working with visual studio and blend on Visual C# wpf.
This is my progress of codes in code-behind file:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
var myRect = (Rectangle)this.FindName("myRect");
double x = Canvas.GetLeft(myRect);
double y = Canvas.GetTop(myRect);
Canvas.SetLeft(myRect,x+10);
Canvas.SetTop(myRect,y);
And this is in XAML code:
<Window x:Class="move.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:move"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Border BorderThickness="1" BorderBrush="Aqua">
<Canvas Name="PointCanvas" Width="500" Height="294" Margin="9,0,6,0">
<Rectangle x:Name="myRect" Fill="#FFF5F4F5" Height="39" Canvas.Left="170" Stroke="Black" Canvas.Top="89" Width="89"/>
</Canvas>
</Border>
<Button Name="Move" Click="Button_Click">Move</Button>
</StackPanel>

Loading image from the web at runtime in WPF

I'm trying to load an image from an url in WPF in a very simple way, but it's not working. Any help ? The code is below :
Main XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="279*"></RowDefinition>
<RowDefinition Height="41*">
</RowDefinition>
</Grid.RowDefinitions>
<Image x:Name="image1" Grid.Row="0"></Image>
<TextBox Grid.Row="1" Margin="0,0,10,0"></TextBox>
</Grid>
</Window>
Code-behind
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri("http://www.clipartkid.com/images/817/pic-of-german-flag-clipart-best-VkuN37-clipart.jpeg");
bi.EndInit();
//var img = new Image();
image1.Source = bi;
}
}
Funny indeed! Works alright at my other laptop now. Must be something with the firewall settings. And yeps, I've made things even simpler now. Binding the source of the image in XAML to just a string property I've set in a ViewModel class.
<Image x:Name="image1" Source ="{Binding MyPic}" Grid.Row="0"></Image>
class MyViewModel
{
public string MyPic {
get { return #"http://www.clipartkid.com/images/817/pic-of-german-flag-clipart-best-VkuN37-clipart.jpeg"; }
}
}
Thanks for the responses, and sorry for the confusion.
-Ron

VtkActor is not displayed on WPF project

I am trying to load a RenderWindowControl from vtk libraries on my WPF project using ActiViz.NET and Visual Studio 2013. The library works fine since I did a new project just to practice on it but when I try to integrate it into my work, the actor is not shown. This is my code:
MainWindow.xaml:
<Window x:Class="myProject.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:VtkTab="clr-namespace:myProject.Views.UITabs.VtkTab"
x:Name="Mainwindow"
MinHeight="600"
MinWidth="800"
Title="{Binding Title}"
Height="720"
Width="1280"
Icon="{StaticResource ApplicationIcon}"
Loaded="OnLoaded"
DataContext="{Binding Main, Source={StaticResource ViewModelLocator}}"
Style="{StaticResource WindowStyle}"
mc:Ignorable="d">
<DockPanel>
<TabControl>
....
....
<VtkTab:VtkTabView />
....
....
</TabControl>
</DockPanel>
</Window>
VtkTabView.xaml:
<UserControl x:Class="myProject.Views.UITabs.VtkTab.VtkTabView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vtk="clr-namespace:Kitware.VTK;assembly=Kitware.VTK"
Height="480" Width="640">
<WindowsFormsHost Name="Wfh">
<vtk:RenderWindowControl x:Name="RenderControl" />
</WindowsFormsHost>
</UserControl>
VtkTabView.xaml.cs:
public partial class UITabView
{
protected static Random _random = new Random();
vtkActor actor = vtkActor.New();
public VtkTabView()
{
InitializeComponent();
var sphere = vtkSphereSource.New();
sphere.SetThetaResolution(8);
sphere.SetPhiResolution(16);
var shrink = vtkShrinkPolyData.New();
shrink.SetInputConnection(sphere.GetOutputPort());
shrink.SetShrinkFactor(0.9);
var move = vtkTransform.New();
move.Translate(_random.NextDouble(), _random.NextDouble(), _random.NextDouble());
var moveFilter = vtkTransformPolyDataFilter.New();
moveFilter.SetTransform(move);
moveFilter.SetInputConnection(shrink.GetOutputPort());
var mapper = vtkPolyDataMapper.New();
mapper.SetInputConnection(moveFilter.GetOutputPort());
// The actor links the data pipeline to the rendering subsystem
actor.SetMapper(mapper);
actor.GetProperty().SetColor(1, 0, 0);
RenderControl.Load += RenderWindowControlOnLoad;
}
void RenderWindowControlOnLoad(object sender, EventArgs e)
{
var renderer = RenderControl.RenderWindow.GetRenderers().GetFirstRenderer();
renderer.AddActor(actor);
renderer.SetBackground(0, 1, 0);
renderer.Render();
}
}
As you can see, in the method RenderWindowControlOnLoad (VtkTabView.xaml.cs) I take the actor (a Sphere) and I add it to the render declared into the WPF but it is not displayed. I tried to change the background color and call to render just in case but it's still not working :(
Call
RenderControl.RenderWindow.Render();
instead of
renderer.Render();

Categories