为什么TImage旋转我的图像? [英] Why is TImage rotating my image?

查看:74
本文介绍了为什么TImage旋转我的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写移动应用程序-它正在从安全的网站中提取图像,如下所示(第一个图像)未正确提取(注意Web版本与移动版本),第二个图像在网站上正确显示,但是Delphi TImage正在旋转它由于某种原因,我不知道为什么.将Rotate设置为0,并在TImage组件上设置"Fit".

Writing a mobile application - it's pulling images from a secure website, and shown below (the first image) pulls incorrectly (notice web version vs mobile version), the second image shows correctly on the website, but Delphi TImage is rotating it for some reason and I can't figure out why. Rotate is set to 0 and "Fit" is set on the TImage component.

有想法吗?

推荐答案

Jpeg和Tiff具有 Exif (可交换的图像文件格式)元数据,用于指定图像方向(以及其他数据).

Jpeg and Tiff have Exif (Exchangeable image file format) metadata that specify image orientation (among other data).

不是"TImage旋转我的图像".TImage不处理Exif方向元数据.理想情况下,TImage 应该根据方向元数据自动旋转图像,但不这样做.您需要阅读Exif方向属性并相应地旋转图像.

It's not that "TImage rotating my image". the TImage is not handling the Exif orientation metadata. Ideally, TImage should auto-rotate the image according to the orientation metadata, but it does not. You need to read the Exif orientation property and rotate the image accordingly.

Exif标签方向"(0x0112)规范是:

The Exif tag "Orientation" (0x0112) spec is:

1 = Horizontal (normal) 
2 = Mirror horizontal 
3 = Rotate 180 
4 = Mirror vertical 
5 = Mirror horizontal and rotate 270 CW 
6 = Rotate 90 CW 
7 = Mirror horizontal and rotate 90 CW 
8 = Rotate 270 CW

您可以使用一些免费的Exif 组件例如TExif/NativeJpg/CCR Exif,然后根据方向标签旋转图像.

You can use some free Exif components such TExif/NativeJpg/CCR Exif, and rotate your image if needed according to the orientation tag.

以下是使用GDI +(VCL/Windows)的示例,例如:

Here is an example using GDI+ (VCL/Windows) e.g:

uses GDIPAPI, GDIPOBJ;

procedure TForm1.Button1Click(Sender: TObject);
var
  GPImage: TGPImage;
  GPGraphics: TGPGraphics;
  pPropItem: PPropertyItem;
  BufferSize: Cardinal;
  Orientation: Byte;
  RotateType: TRotateFlipType;
  Bitmap: TBitmap;
begin
  GPImage := TGPImage.Create('D:\Test\image.jpg');
  try
    BufferSize := GPImage.GetPropertyItemSize(PropertyTagOrientation);
    if BufferSize > 0 then
    begin
      GetMem(pPropItem, BufferSize);
      try
        GDPImage.GetPropertyItem(PropertyTagOrientation, BufferSize, pPropItem);
        Orientation := PByte(pPropItem.value)^;
        case Orientation of
          1: RotateType := RotateNoneFlipNone; // Horizontal - No rotation required
          2: RotateType := RotateNoneFlipX;
          3: RotateType := Rotate180FlipNone;
          4: RotateType := Rotate180FlipX;
          5: RotateType := Rotate90FlipX;
          6: RotateType := Rotate90FlipNone;
          7: RotateType := Rotate270FlipX;
          8: RotateType := Rotate270FlipNone;
        else
          RotateType := RotateNoneFlipNone; // Unknown rotation?
        end;
        if RotateType <> RotateNoneFlipNone then
          GPImage.RotateFlip(RotateType);
        Bitmap := TBitmap.Create;
        try
          Bitmap.Width := GPImage.GetWidth;
          Bitmap.Height := GPImage.GetHeight;
          Bitmap.Canvas.Lock;
          try
            GPGraphics := TGPGraphics.Create(Bitmap.Canvas.Handle);
            try
              GPGraphics.DrawImage(GPImage, 0, 0, GPImage.GetWidth, GPImage.GetHeight);
              Image1.Picture.Assign(Bitmap);              
            finally
              GPGraphics.Free;
            end;
          finally
            Bitmap.Canvas.Unlock;
          end;    
        finally
          Bitmap.Free;
        end;
      finally
        FreeMem(pPropItem);
      end;
    end;
  finally
    GPImage.Free
  end;
end;

这篇关于为什么TImage旋转我的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆