I am currently working on an application that has to store images from an IP camera to a file. I don't want to store the video stream. I just want to take an image every hundred or so milliseconds.
I use a specific url to get JPEG images from the IP camera which was supplied by the manufacturer. I save the image that I get from the IP camera in a BitmapImage but when I want to save the BitmapImage to a file it saves an empty .jpg file in the directory that I specify.
What I don't understand is that when I want to display the BitmapImage in an Image control then it shows the actual image, but when I want to save it, it saves an empty file.
Could anyone please advise me on how to solve this problem or maybe where it can find a solution?
I have already tried JPEGBitmapEncoder for this but no success.
Here is the code that I am currently using:
private void captureButton_Click(object sender, RoutedEventArgs e) { string photosLocation, newPhotos;
photosLocation = Directory.GetCurrentDirectory() + "\\IP Cam Photos";
newPhotos = Guid.NewGuid().ToString();
Directory.CreateDirectory(photosLocation);
camLocation = photosLocation + "\\" + newPhotos;
Directory.CreateDirectory(camLocation);
captureStatusLabel.Content = "Photo capturing started!";
for (int i = 0; i < 10; i++)
{
camImage = new BitmapImage();
camImage.BeginInit();
camImage.UriSource = new Uri("http://172.16.4.14/image?res=full&x0=0&y0=0&x1=1600&y1=1200&quality=12&doublescan=0", UriKind.RelativeOrAbsolute);
while (camImage.IsDownloading)
{
captureStatusLabel.Content = "Capture Busy";
}
camImage.DownloadCompleted += camImage_DownloadCompleted;
camImage.EndInit();
captureStatusLabel.Content = "Photo " + (i + 1) + " captured!";
}
}
void camImage_DownloadCompleted(object sender, EventArgs e)
{
a++;
camImgLoc = camLocation + "\\Image " + a + ".jpg";
FileStream camFiles = new FileStream(camImgLoc, FileMode.Create);
JpegBitmapEncoder camEncoder = new JpegBitmapEncoder();
MessageBox.Show(camImage.IsDownloading.ToString() + "\n" + camEncoder.ToString());
camEncoder.Frames.Add(BitmapFrame.Create(camImage));
camEncoder.Save(camFiles);
camFiles.Close();
}