我在这个项目中有2个解决方案。 Windows窗体解决方案和Windows 8.1 Tablet项目。
这是应该发生的事情:


用户使用平板电脑拍照,然后以字节数组的形式将其上传到MySQL数据库。
用户启动Windows窗体应用程序并从MySQL数据库加载字节数组。
然后,它将字节数组转换为放置在图片框中的图像。


我存储这样的字节数组:

CameraCaptureUI dialog = new CameraCaptureUI();
        dialog.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
        Size aspectRatio = new Size(16, 9);
        dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;

        StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
        if (file != null)
        {
            BitmapImage bitmapImage = new BitmapImage();
            using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
            {
                var readStream = fileStream.AsStreamForRead();
                byte[] pixeBuffer = new byte[readStream.Length];
                await readStream.ReadAsync(pixeBuffer, 0, pixeBuffer.Length);

            }


字节数组已成功存储在我的数据库中。

将字节数组转换为WinForms图像时遇到问题。
这是我的代码:

using (var ms = new MemoryStream(bytes))
                {
                    Image i = Image.FromStream(ms);
                    return i;
                }


这给了我一个无效的参数异常。

我猜这与图像格式有关吗?虽然我真的很陌生,所以我不知道。

欢迎任何帮助!

PS:我知道在SQL数据库中存储可以完美运行,因为我只能使用WinForms应用程序完美存储和加载图像。

最佳答案

您是否尝试过将MemoryStream的位置重新设置为开始,然后再尝试创建Image ...

ms.Seek(0, SeekOrigin.Begin);

关于c# - 将BitmapImage字节数组转换为图像字节数组(WinRT-> WinForms),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28772842/

10-16 09:00