0
votes

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();
    }
1
Can you show some source code?Roger Rowland
It sounds like the image has not been downloaded completely when you are saving it. You may check the BitmapImage's IsDownloading property and perhaps move the save code to a DownloadCompleted event handler.Clemens
I have added the save code to the DownloadCompleted event handler but it still is saving it wrong.GANDA1F

1 Answers

0
votes

I have found the answer. I just need to use the correct url in the request and then it works perfectly. public void ReceiveImage(string imageURL) { HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageURL); imageRequest.Method = "GET"; HttpWebResponse imageResponse = (HttpWebResponse)imageRequest.GetResponse(); using (Stream inputStream = imageResponse.GetResponseStream()) { using (Stream outputStream = File.OpenWrite("SonyCamera.jpg")) { byte[] buffer = new byte[4096]; int bytesRead; do { bytesRead = inputStream.Read(buffer, 0, buffer.Length); outputStream.Write(buffer, 0, bytesRead); } while (bytesRead != 0); } } }