以下代码中的未知异常

时间:2016-02-22 23:37:43

标签: c# bitmap

我正在编写Windows 10 UWP,用于拍摄用户的照片,然后将该照片放在另一个用户的照片中。 (一旦我有特定的图片,它会更有用)。基本上,程序似乎无法访问我的计算机中的图片,无论它们存储在何处。以下一行:

input2 = new FileStream(file2.Path, FileMode.Open, FileAccess.Read);

也会抛出一个异常,有时候说file2正在使用其他时候说我应该将它放在task.run框中。

连连呢?我对C#完全不熟悉。谢谢!

private async void processing()
    {
        CameraCaptureUI dialog = new CameraCaptureUI();
        Size aspectRatio = new Size(1, 1);
        dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;

        StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
        StorageFile file2;

        Image playbutton = new Image();
        try
        {
            IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
            BitmapImage bmp = new BitmapImage();
            bmp.SetSource(stream);
            playbutton.Source = bmp;
            stream = null;
        }
        catch (Exception ex)
        {
            return;
        }

        Image frame = new Image();
        try
        {
            FileOpenPicker picker = new FileOpenPicker();
            picker.ViewMode = PickerViewMode.Thumbnail;
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".png");
            file2 = await picker.PickSingleFileAsync();
            file2 = await StorageFile.GetFileFromPathAsync("C:\\Users\\Kyle\\Downloads\\BingWallpaper-2016-02-07");
            IRandomAccessStream stream = await file2.OpenAsync(FileAccessMode.ReadWrite);
            BitmapImage bmp = new BitmapImage();
            bmp.SetSource(stream);
            playbutton.Source = bmp;
            frame.Source = bmp;
            stream = null;
        }
        catch (Exception ex)
        {
            return;
        }
        FileStream input2;
        var t = Task.Run(() => {
        input2 = new FileStream(file2.Path, FileMode.Open, FileAccess.Read);
        byte[] arrayForImage2;
        byte[] buffer2 = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input2.Read(buffer2, 0, buffer2.Length)) > 0)
            {
                ms.Write(buffer2, 0, read);
            }
            arrayForImage2 = ms.ToArray();
        }

        byte[] arrayForImage1;
        FileStream input = new FileStream(file.Path, FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            arrayForImage1 = ms.ToArray();
        }


        if (arrayForImage1.Length > arrayForImage2.Length)
        {
            for (int x = 0; x < arrayForImage2.Length; x++)
            {
                arrayForImage1[x] = arrayForImage2[x];
            }
            FileStream fSO = new FileStream("C:/Users/Kyle/Pictures/imageCompiled.png", FileMode.Create, FileAccess.Write);
            fSO.Write(arrayForImage1, 0, arrayForImage1.Length);
        }
        else if (arrayForImage1.Length < arrayForImage2.Length)
        {
            for (int x = 0; x < arrayForImage1.Length; x++)
            {
                arrayForImage2[x] = arrayForImage1[x];
            }
            FileStream fSO = new FileStream("C:/Users/Kyle/Pictures/imageCompiled.png", FileMode.Create, FileAccess.Write);
            fSO.Write(arrayForImage2, 0, arrayForImage2.Length);
        }
        });
        t.Wait();
    }
}

}

1 个答案:

答案 0 :(得分:0)

  

也会抛出异常,有时会说file2正在使用中

我看到你没有通过调用close()或dispose()方法关闭流。

 FileStream fSO = new FileStream("C:/Users/Kyle/Pictures/imageCompiled.png", FileMode.Create, FileAccess.Write);
            fSO.Write(arrayForImage2, 0, arrayForImage2.Length);

此处,工作完成后应调用fSO.close();

此外,如果正确关闭,则应检查您使用的inputinput2和其他流。