10

IOS デバイスから .net アプリケーションに写真をアップロードしてサイズ変更するためのコードを以下に示します。ユーザーは縦向きで写真を撮っていましたが、すべての写真が間違った回転でアプリに表示されます。これを修正する方法はありますか?

            string fileName = Server.HtmlEncode(FileUploadFormbilde.FileName);
            string extension = System.IO.Path.GetExtension(fileName);
            System.Drawing.Image image_file = System.Drawing.Image.FromStream(FileUploadFormbilde.PostedFile.InputStream);
            int image_height = image_file.Height;
            int image_width = image_file.Width;
            int max_height = 300;
            int max_width = 300;

            image_height = (image_height * max_width) / image_width;
            image_width = max_width;

            if (image_height > max_height)
            {
                image_width = (image_width * max_height) / image_height;
                image_height = max_height;
            }

            Bitmap bitmap_file = new Bitmap(image_file, image_width, image_height);
            System.IO.MemoryStream stream = new System.IO.MemoryStream();

            bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            stream.Position = 0;

            byte[] data = new byte[stream.Length + 1];
            stream.Read(data, 0, data.Length);
4

3 に答える 3

31

ほら、友よ。

Image originalImage = Image.FromStream(data);

 if (originalImage.PropertyIdList.Contains(0x0112))
        {
            int rotationValue = originalImage.GetPropertyItem(0x0112).Value[0];
            switch (rotationValue)
            {
                case 1: // landscape, do nothing
                    break;

                case 8: // rotated 90 right
                    // de-rotate:
                    originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate270FlipNone);
                    break;

                case 3: // bottoms up
                    originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate180FlipNone);
                    break;

                case 6: // rotated 90 left
                    originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate90FlipNone);
                    break;
            }
        }
于 2013-08-06T09:02:56.330 に答える
1

Image.PropertyItemsコレクションの EXIF データから画像の方向の値を読み取り、それに応じて回転する必要があります。

于 2013-06-19T09:13:41.223 に答える