【发布时间】:2016-07-26 17:52:18
【问题描述】:
使用 UriSource 旋转 bitmapImage 可以使用以下代码
private void RotateDocumentImageSourceRight()
{
var biOriginal = (BitmapImage)documentImage.Source;
if (biOriginal == null)
return;
var biRotated = new BitmapImage();
biRotated.BeginInit();
biRotated.UriSource = biOriginal.UriSource;
switch (biOriginal.Rotation)
{
case Rotation.Rotate0:
biRotated.Rotation = Rotation.Rotate270;
break;
case Rotation.Rotate270:
biRotated.Rotation = Rotation.Rotate180;
break;
case Rotation.Rotate180:
biRotated.Rotation = Rotation.Rotate90;
break;
case Rotation.Rotate90:
biRotated.Rotation = Rotation.Rotate0;
break;
default:
break;
}
biRotated.EndInit();
documentImage.Source = biRotated;
}
但是当我改变 bitmapImage 存储到 StreamSource 的方式时,它不起作用并且图像消失了
private void RotateDocumentImageSourceRight()
{
...same code...
biRotated.StreamSource= biOriginal.StreamSource;
...same code...
}
【问题讨论】:
-
您没有在
Image控件上使用RenderTransform是否有原因?更便宜、更快。 -
是的,跟项目有关,就不做改造了。
-
当您尝试使用
StreamSource时,是否从流中加载了原始图像?否则它将不起作用 - 它将是null。 -
是的,但旋转后图像变为空并消失:/
-
这个怎么样:
var biRotated = new TransformedBitmap(biOriginal, new RotateTransform(90))。当您指定Rotation属性时,BitmapImage在内部使用类TransformedBitmap。它将创建一个新图像,但速度会更快(不需要再次读取流)。