Reputation:
I have an ObservableCollection
in my CameraWindow
where I'm manually capturing images from my usb webcam. This is my collection:
public ObservableCollection<BitmapImage> CameraWindowCapturedImages { get; } = new ObservableCollection<BitmapImage>();
and this is how I'm capturing images
void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
try
{
System.Drawing.Image img = (Bitmap)eventArgs.Frame.Clone();
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg);
ms.Seek(0, SeekOrigin.Begin);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
bi.Freeze();
this.latestFrame = bi;
Dispatcher.BeginInvoke(new ThreadStart(delegate
{
previewWindow.Source = bi;
}));
}
catch (Exception ex)
{
}
}
private void manualCapture_Click(object sender, RoutedEventArgs e)
{
if (captureImage != null)
{
captureImage(latestFrame);
}
Bitmap bm = BitmapImage2Bitmap(latestFrame);
CapturedImages.Add(latestFrame);
}
private Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
{
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapImage));
enc.Save(outStream);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
return new Bitmap(bitmap);
}
}
I also have ObservableCollection
in my MainWindow
public ObservableCollection<BitmapImage> MainWindowCapturedImages { get; } = new ObservableCollection<BitmapImage>();
and I want to store manually captured images in my CameraWindow
in my MainWindow
's ObservableCollection
.
Is this possible and if it is could someone please help me with that? Thanks!
Upvotes: 2
Views: 163
Reputation: 1358
The quick way to do this is(C# 6 and WPF):
(App.Current.MainWindow as MainWindow)?.Items.Add(image);
Also I wouldn't recommend doing it this way. You should use MVVM pattern which is a recommended developing technique for WPF apps
Upvotes: 0
Reputation: 169400
You need to get a reference to the window somehow. The easiest way to do this is probably to use the Application.Current.Windows
collection:
MainWindow mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
if(mainWindow != null)
{
mainWindow.MainWindowCapturedImages.Add(latestFrame);
}
Upvotes: 1