更新时间:2021-09-13 05:47:23
旋转Image元素不会在其Source属性中旋转ImageSource。
Rotating the Image element does in no way rotate the ImageSource in its Source property.
要创建旋转的ImageSource,请使用 TransformedBitmap
:
To create a rotated ImageSource, use a TransformedBitmap
:
var sourceBitmap = new BitmapImage();
using (var stream = new MemoryStream(ImageSource))
{
sourceBitmap.BeginInit();
sourceBitmap.CacheOption = BitmapCacheOption.OnLoad;
sourceBitmap.StreamSource = stream;
sourceBitmap.EndInit();
}
// This should be another view model property that the Slider is bound to.
// Only multiples of 90 degrees are valid values.
var rotationAngle = 90d;
var rotation = new RotateTransform(rotationAngle);
var rotatedBitmap = new TransformedBitmap(sourceBitmap, rotation);
为避免您必须创建新来源每次转换的位图,您应该将ImageSource属性的类型从 byte []
更改为 ImageSource
。
为了将其写回另一个 byte []
,请使用 BitmapEncoder之一
类,例如 PngBitapEncoder
。
In order to write this back into another byte[]
, use one of the BitmapEncoder
classes, e.g. PngBitapEncoder
.