我目前正在研究一個應用程序,它需要將圖像從一臺IP攝像機存儲到一個文件中。我不想存儲視頻流。我只想每隔幾百毫秒拍一張圖片。保存bitmapimage到文件的問題
我使用特定的URL從製造商提供的IP攝像機獲取JPEG圖像。我將從IP攝像機獲得的圖像保存在BitmapImage中,但是當我想將BitmapImage保存到文件時,它會在我指定的目錄中保存一個空的.jpg文件。
我不明白的是,當我想在Image控件中顯示BitmapImage時,它會顯示實際圖像,但是當我想保存它時,它會保存一個空文件。
任何人都可以請告訴我如何解決這個問題,或者它可以找到解決方案?
我已經嘗試過JPEGBitmapEncoder,但沒有成功。
這裏是我目前使用的代碼:
私人無效captureButton_Click(對象發件人,RoutedEventArgs E) { 串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();
}
你能顯示一些源代碼嗎? –
這聽起來像是在保存圖像時並未完全下載圖像。您可以檢查BitmapImage的[IsDownloading](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.isdownloading.aspx)屬性,也許可以將保存代碼移到[DownloadCompleted ](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.downloadcompleted.aspx)事件處理程序。 – Clemens
我已將保存代碼添加到DownloadCompleted事件處理程序,但它仍然保存錯誤。 – GANDA1F