图片在PictureBox

时间:2016-08-21 21:16:17

标签: c# winforms picturebox

正如问题所暗示的,当我将图像加载到pictureBox(使用对话框)时,它不会显示为原始外观。在这个屏幕截图中,左边的图像是我加载到pictureBox中的图像(右边)。

试图知道是什么原因我使用Paint应用程序绘制图像并使用Windows照片查看器旋转它,旋转的图像按原样(旋转)加载,也就是说,一些图片只是精细加载,其他图像是旋转的!我无法弄清楚为什么?! enter image description here

3 个答案:

答案 0 :(得分:8)

如果没有原始图像数据,就无法确定发生了什么。但很明显,在某些时候,图像处理中涉及的一些软件使用EXIF方向属性来旋转图像,而不是实际修改图像数据本身。这可能是照片查看器或某些处理照片的工具。

以下是可用于检测图像方向的代码,由拍摄照片的相机记录在EXIF数据中:

static ImageOrientation GetOrientation(this Image image)
{
    PropertyItem pi = SafeGetPropertyItem(image, 0x112);

    if (pi == null || pi.Type != 3)
    {
        return ImageOrientation.Original;
    }

    return (ImageOrientation)BitConverter.ToInt16(pi.Value, 0);
}

// A file without the desired EXIF property record will throw ArgumentException.
static PropertyItem SafeGetPropertyItem(Image image, int propid)
{
    try
    {
        return image.GetPropertyItem(propid);
    }
    catch (ArgumentException)
    {
        return null;
    }
}

其中:

/// <summary>
/// Possible EXIF orientation values describing clockwise
/// rotation of the captured image due to camera orientation.
/// </summary>
/// <remarks>Reverse/undo these transformations to display an image correctly</remarks>
public enum ImageOrientation
{
    /// <summary>
    /// Image is correctly oriented
    /// </summary>
    Original = 1,
    /// <summary>
    /// Image has been mirrored horizontally
    /// </summary>
    MirrorOriginal = 2,
    /// <summary>
    /// Image has been rotated 180 degrees
    /// </summary>
    Half = 3,
    /// <summary>
    /// Image has been mirrored horizontally and rotated 180 degrees
    /// </summary>
    MirrorHalf = 4,
    /// <summary>
    /// Image has been mirrored horizontally and rotated 270 degrees clockwise
    /// </summary>
    MirrorThreeQuarter = 5,
    /// <summary>
    /// Image has been rotated 270 degrees clockwise
    /// </summary>
    ThreeQuarter = 6,
    /// <summary>
    /// Image has been mirrored horizontally and rotated 90 degrees clockwise.
    /// </summary>
    MirrorOneQuarter = 7,
    /// <summary>
    /// Image has been rotated 90 degrees clockwise.
    /// </summary>
    OneQuarter = 8
}

上面的GetOrientation()方法是作为扩展方法编写的,但当然可以将其称为普通静态方法。无论哪种方式,只需将您刚刚从文件中打开的Bitmap对象传递给它,它就会返回存储在文件中的EXIF方向(如果有的话)。

有了这些,您可以根据需要旋转图像。

答案 1 :(得分:6)

在Windows照片查看器中查看图像时,如果图像方向具有Exif方向数据,则会自动更正图像方向。 PictureBox没有对此类功能的内置支持。您可以创建自定义PictureBox,即使它们具有方向数据也能正确显示图像:

using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
public class MyPictureBox : PictureBox
{
    private void CorrectExifOrientation(Image image)
    {
        if (image == null) return;
        int orientationId = 0x0112;
        if (image.PropertyIdList.Contains(orientationId))
        {
            var orientation = (int)image.GetPropertyItem(orientationId).Value[0];
            var rotateFlip = RotateFlipType.RotateNoneFlipNone;
            switch (orientation)
            {
                case 1: rotateFlip = RotateFlipType.RotateNoneFlipNone; break;
                case 2: rotateFlip = RotateFlipType.RotateNoneFlipX; break;
                case 3: rotateFlip = RotateFlipType.Rotate180FlipNone; break;
                case 4: rotateFlip = RotateFlipType.Rotate180FlipX; break;
                case 5: rotateFlip = RotateFlipType.Rotate90FlipX; break;
                case 6: rotateFlip = RotateFlipType.Rotate90FlipNone; break;
                case 7: rotateFlip = RotateFlipType.Rotate270FlipX; break;
                case 8: rotateFlip = RotateFlipType.Rotate270FlipNone; break;
                default: rotateFlip = RotateFlipType.RotateNoneFlipNone; break;
            }
            if (rotateFlip != RotateFlipType.RotateNoneFlipNone)
            {
                image.RotateFlip(rotateFlip);
                image.RemovePropertyItem(orientationId);
            }
        }
    }
    [Localizable(true)]
    [Bindable(true)]
    public new Image Image
    {
        get { return base.Image; }
        set { base.Image = value; CorrectExifOrientation(value); }
    }
}

答案 2 :(得分:0)

我将图像从文件资源管理器加载到图片框,如下所示:

 const Slider({
Key? key,
required this.value,
required this.onChanged,
this.onChangeStart,
this.onChangeEnd,
this.min = 0.0,
this.max = 1.0,
this.divisions,
this.label,
this.activeColor,
this.inactiveColor,
this.mouseCursor,
this.semanticFormatterCallback,
this.focusNode,
this.autofocus = false,
}) : _sliderType = _SliderType.material,
   assert(value != null),
   assert(min != null),
   assert(max != null),
   assert(min <= max),
   assert(value >= min && value <= max),
   assert(divisions == null || divisions > 0),
   super(key: key);

这是正确显示图像的方法:

private void btnBrowse_Click ( object sender, EventArgs e ) {
    using( OpenFileDialog openFile = new OpenFileDialog() ) {
        openFile.Title = "Select image for [user]";
        openFile.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png)|*.jpg; *.jpeg; *.jpe; *.jfif; *.png|All files (*.*)|*.*";

        if( openFile.ShowDialog() == DialogResult.OK ) {
            //image validation
            try {
                Bitmap bmp = new Bitmap( openFile.FileName );//to validate the image
                if( bmp != null ) {//if image is valid
                    pictureBox1.Load( openFile.FileName );//display selected image file
                    pictureBox1.Image.RotateFlip( Rotate( bmp ) );//display image in proper orientation
                    bmp.Dispose();
                }
            } catch( ArgumentException ) {
                MessageBox.Show( "The specified image file is invalid." );
            } catch( FileNotFoundException ) {
                MessageBox.Show( "The path to image is invalid." );
            }
        }
    }
}

这些是我的参考:

代码 参考(我修改了接受的答案):https://stackoverflow.com/a/42972969/11565087

视觉 参考(在评论中找到[我不知道如何在此处链接评论]):http://csharphelper.com/blog/2016/07/read-an-image-files-exif-orientation-data-in-c/