2

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!

mm8
  • 163,881
  • 10
  • 57
  • 88
  • You can assign/copy one observable collection to another. Where in code you are assigning/copying the required collections? And what are the hurdles in doing so? – Azaz ul Haq Apr 26 '17 at 10:06
  • Are you asking on how to add the BitmapImage objects to the MainWindowCapturedImages Collection in your main window or what is your question? – mm8 Apr 26 '17 at 10:07
  • @mm8 asking on how to add the BitmapImage objects to the MainWindowCapturedImages Collection in my Mainwindow –  Apr 26 '17 at 10:08
  • From what method? – mm8 Apr 26 '17 at 10:09
  • I'm currently adding captured images to my `CameraWindowCapturedImages` using `Action captureImage;` in `manualCapture_Click` –  Apr 26 '17 at 10:12
  • As a note, during conversion from Bitmap to BitmapImage it might be more efficient to encode the bitmap as BMP instead of JPEG. See also [this question](http://stackoverflow.com/q/30727343/1136211) for potential improvement. – Clemens Apr 26 '17 at 10:53

2 Answers2

1

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);
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • where sould I add this? (sorry for asking stupid question, I'm very beginner c# / wpf) –  Apr 26 '17 at 10:17
  • Perhaps in manualCapture_Click? Or wherver you want to be able to access the MainWindow. Only you know what you want to do... – mm8 Apr 26 '17 at 10:19
  • Please ask a new question if you have another issue. – mm8 Apr 26 '17 at 11:54
0

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

SENya
  • 1,083
  • 11
  • 26