将图像转换为字节时,它不会旋转

时间:2019-12-23 18:48:44

标签: c# bitmap

所以我的问题是:当我尝试从文件对话框将图像部署到程序时,它将图像转换为字节,它可以正常工作,但是当我在Windows照片上旋转该图像时,它旋转了,但是当我再次部署它时从文件对话框中恢复过来。

图像到字节->

       private byte[] ConvertImageToBinary(Image img)
        {
            using (MemoryStream ss = new MemoryStream())
            {
                img.Save(ss, System.Drawing.Imaging.ImageFormat.Jpeg);
                var s = ss.ToArray();
                var jpegQuality = 50;
                Image image;
                using (var inputStream = new MemoryStream(s))
                {
                    image = Image.FromStream(inputStream);
                    var jpegEncoder = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders()
                      .First(c => c.FormatID == System.Drawing.Imaging.ImageFormat.Jpeg.Guid);
                    var encoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
                    encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, jpegQuality);
                    Byte[] outputBytes;
                    using (var outputStream = new MemoryStream())
                    {
                        image.Save(outputStream, jpegEncoder, encoderParameters);
                        return outputBytes = outputStream.ToArray();
                    }
                }
            }

        }

字节到位图->

 public BitmapImage ConvertBinaryToImage(byte[] array)
        {
            var image = new BitmapImage();
            using (var ms = new MemoryStream(array))
            {
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad; // here
                image.StreamSource = ms;
                image.EndInit();
                image.Freeze();
            }
            return image;
        }

示例-> 在该示例中,您可以清楚地看到我旋转了图像并将其拖放到程序上,但是它没有旋转

0 个答案:

没有答案