将bitmapimage保存到文件时出现问题

本文关键字:问题 存到文件 bitmapimage | 更新日期: 2023-09-27 18:14:07

我目前正在研究一个必须将图像从IP摄像机存储到文件的应用程序。我不想存储视频流。我只想每隔一百毫秒拍一张照片。

我使用特定的url从制造商提供的IP摄像机获取JPEG图像。我将从IP摄像机获得的图像保存在BitmapImage中,但当我想将BitmapImage保存到文件时,它会在我指定的目录中保存一个空的。jpg文件。

我不明白的是,当我想在图像控件中显示BitmapImage时,它会显示实际的图像,但当我想保存它时,它会保存一个空文件。

谁能告诉我如何解决这个问题,或者在哪里可以找到一个解决方案?

我已经尝试了JPEGBitmapEncoder,但没有成功。

下面是我当前使用的代码:

private void captureButton_Click(对象发送者,RoutedEventArgs e){

        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();
    }

将bitmapimage保存到文件时出现问题

我找到了答案。我只需要在请求中使用正确的url,然后它的工作完美。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); } } }