Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将位图图像保存到文件时出现问题_C#_Wpf - Fatal编程技术网

C# 将位图图像保存到文件时出现问题

C# 将位图图像保存到文件时出现问题,c#,wpf,C#,Wpf,我目前正在开发一个应用程序,它必须将IP摄像头中的图像存储到文件中。我不想存储视频流。我只想每隔大约100毫秒拍一张照片 我使用特定的url从制造商提供的IP摄像机获取JPEG图像。我将从IP摄像头获取的图像保存在BitmapImage中,但当我想将BitmapImage保存到文件时,它会在指定的目录中保存一个空的.jpg文件 我不明白的是,当我想在图像控件中显示位图图像时,它会显示实际图像,但当我想保存它时,它会保存一个空文件 有谁能告诉我如何解决这个问题,或者在哪里可以找到解决方案 我已经为

我目前正在开发一个应用程序,它必须将IP摄像头中的图像存储到文件中。我不想存储视频流。我只想每隔大约100毫秒拍一张照片

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

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

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

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

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

私有无效捕获按钮\单击(对象发送者,路由目标) { 字符串位置,新照片

        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();
    }
photolocation=Directory.GetCurrentDirectory()+“\\IP Cam照片”;
newPhotos=Guid.NewGuid().ToString();
目录.CreateDirectory(photosLocation);
camLocation=photosLocation+“\\\”+新照片;
创建目录(camLocation);
captureStatusLabel.Content=“照片捕获已开始!”;
对于(int i=0;i<10;i++)
{
camImage=新的位图图像();
camImage.BeginInit();
camImage.UriSource=新Uri(“http://172.16.4.14/image?res=full&x0=0&y0=0&x1=1600&y1=1200&quality=12&doublescan=0“,UriKind.相对溶质);
while(camImage.isDownload)
{
captureStatusLabel.Content=“捕获忙”;
}
camImage.DownloadCompleted+=camImage_DownloadCompleted;
camage.EndInit();
captureStatusLabel.Content=“Photo”+(i+1)+“captured!”;
}
}
void camImage_下载完成(对象发送方,事件参数e)
{
a++;
camimglocation=camLocation+“\\Image”+a+“.jpg”;
FileStream camFiles=newfilestream(caimgloc,FileMode.Create);
JpegBitmapEncoder camEncoder=新的JpegBitmapEncoder();
MessageBox.Show(camImage.IsDownload.ToString()+“\n”+camEncoder.ToString());
添加(BitmapFrame.Create(camImage));
camEncoder.Save(camFiles);
camFiles.Close();
}

我找到了答案。我只需要在请求中使用正确的url,然后它就能完美地工作。
public void ReceiveImage(字符串imageURL)
{
HttpWebRequest imageRequest=(HttpWebRequest)WebRequest.Create(imageURL);
imageRequest.Method=“GET”;
HttpWebResponse imageResponse=(HttpWebResponse)imageRequest.GetResponse();
使用(Stream inputStream=imageResponse.GetResponseStream())
{
使用(Stream outputStream=File.OpenWrite(“SonyCamera.jpg”))
{
字节[]缓冲区=新字节[4096];
int字节读取;
做
{
bytesRead=inputStream.Read(buffer,0,buffer.Length);
写入(缓冲区,0,字节读取);
}while(bytesRead!=0);
}
}
}

您能展示一些源代码吗?在保存图像时,听起来图像还没有完全下载。您可以检查BitmapImage的属性,或者将保存代码移动到事件处理程序中。我已将保存代码添加到DownloadCompleted事件处理程序中,但它仍然保存错误。