Jpeg và Tiff có Exif (định dạng tệp hình ảnh có thể thay đổi) siêu dữ liệu chỉ định hướng hình ảnh (trong số các dữ liệu khác).
Nó không phải là "TImage xoay hình ảnh của tôi". TImage không xử lý siêu dữ liệu định hướng Exif. Lý tưởng nhất, TImage nên tự động xoay hình ảnh theo siêu dữ liệu định hướng, nhưng không. Bạn cần đọc thuộc tính định hướng Exif và xoay hình ảnh cho phù hợp.
Các Exif tag "Định hướng" (0x0112) spec là:
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
Bạn có thể sử dụng một số miễn phí Exif components như TExif/NativeJpg/CCR Exif, và xoay hình ảnh của bạn nếu cần thiết theo thẻ hướng.
Dưới đây là một ví dụ sử dụng GDI + (VCL/Windows) ví dụ:
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;
Tôi thấy điều này xảy ra tất cả các thời gian trong Windows Explorer chẵn. Khá chắc chắn nó có một cái gì đó để làm với hình ảnh chính nó, một cái gì đó mà cờ các tập tin như "xoay", hoặc dọc theo những dòng. –