Reputation: 800
Rotating a bitmapImage using UriSource works using the following code
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;
}
But when I change the way a bitmapImage is stored to StreamSource it doesn't work and the image disappears
private void RotateDocumentImageSourceRight()
{
...same code...
biRotated.StreamSource= biOriginal.StreamSource;
...same code...
}
Upvotes: 2
Views: 244
Reputation: 22739
You can use a TransformedBitmap
, which is used by BitmapImage
internally when you specify the Rotation
property. It will create a new image, but it will be faster (it won't need to read the stream again).
private void RotateDocumentImageSourceRight()
{
var biOriginal = (BitmapSource)documentImage.Source;
if (biOriginal == null)
return;
var angle = 0.0;
var biRotated = biOriginal as TransformedBitmap;
if (biRotated != null)
{
biOriginal = biRotated.Source;
angle = ((RotateTransform)biRotated.Transform).Angle;
}
angle -= 90;
if (angle < 0) angle += 360;
biRotated = new TransformedBitmap(biOriginal, new RotateTransform(angle));
documentImage.Source = biRotated;
}
Upvotes: 2