我正在寻找一种最快的方法来创建缩小的位图,并遵守EXIF方向标签。
参考:https://weblogs.asp.net/bleroy/the-fastest-way-to-resize-images-from-asp-net-and-it-s-more-supported-ish 目前,我使用以下代码创建一个位图,以遵守EXIF方向标签。
参考:https://weblogs.asp.net/bleroy/the-fastest-way-to-resize-images-from-asp-net-and-it-s-more-supported-ish 目前,我使用以下代码创建一个位图,以遵守EXIF方向标签。
static Bitmap FixImageOrientation(Bitmap srce)
{
const int ExifOrientationId = 0x112;
// Read orientation tag
if (!srce.PropertyIdList.Contains(ExifOrientationId)) return srce;
var prop = srce.GetPropertyItem(ExifOrientationId);
var orient = BitConverter.ToInt16(prop.Value, 0);
// Force value to 1
prop.Value = BitConverter.GetBytes((short)1);
srce.SetPropertyItem(prop);
// Rotate/flip image according to <orient>
switch (orient)
{
case 1:
srce.RotateFlip(RotateFlipType.RotateNoneFlipNone);
return srce;
case 2:
srce.RotateFlip(RotateFlipType.RotateNoneFlipX);
return srce;
case 3:
srce.RotateFlip(RotateFlipType.Rotate180FlipNone);
return srce;
case 4:
srce.RotateFlip(RotateFlipType.Rotate180FlipX);
return srce;
case 5:
srce.RotateFlip(RotateFlipType.Rotate90FlipX);
return srce;
case 6:
srce.RotateFlip(RotateFlipType.Rotate90FlipNone);
return srce;
case 7:
srce.RotateFlip(RotateFlipType.Rotate270FlipX);
return srce;
case 8:
srce.RotateFlip(RotateFlipType.Rotate270FlipNone);
return srce;
default:
srce.RotateFlip(RotateFlipType.RotateNoneFlipNone);
return srce;
}
}
我首先创建了一个方向固定的图像,然后按比例调整大小以进行快速处理。
public static Bitmap UpdatedResizeImage(Bitmap source, Size size)
{
var scale = Math.Min(size.Width / (double)source.Width, size.Height / (double)source.Height);
var bmp = new Bitmap((int)(source.Width * scale), (int)(source.Height * scale));
using (var graph = Graphics.FromImage(bmp))
{
graph.InterpolationMode = InterpolationMode.High;
graph.CompositingQuality = CompositingQuality.HighQuality;
graph.SmoothingMode = SmoothingMode.AntiAlias;
graph.DrawImage(source, 0, 0, bmp.Width, bmp.Height);
}
return bmp;
}
现在,WIC 可以更快的进行图像处理。参考:https://dev59.com/Grbna4cB1Zd3GeqPXjxn#57987315
如何创建一个缩小的 BitmapImage 并保留 EXIF 标签?
更新:
if ((bitmapMetadata != null) && (bitmapMetadata.ContainsQuery("System.Photo.Orientation")))
{
object o = bitmapMetadata.GetQuery("System.Photo.Orientation");
if (o != null)
{
switch ((ushort)o)
{
case 3:
rotatedImage = new TransformedBitmap(resized, new RotateTransform(180));
break;
case 6:
rotatedImage = new TransformedBitmap(resized, new RotateTransform(90));
break;
case 8:
rotatedImage = new TransformedBitmap(resized, new RotateTransform(270));
break;
}
}
}
BitmapDecoder
吗?因为你的代码示例中使用了它... 有没有一种方法可以从BitmapFrame
创建TransformedBitmap
? - techno