我有保存在Base64中的图像,它们是风景和肖像的混合体。我想将它们全部显示为风景。在C#中,如何将Base64字符串转换为图像,然后旋转它?
我能够将Base64字符串转换为BitmapImages,然后将其设置为Image.Source,但我无法使旋转工作。
在这个类中,我从XML获取base64,然后调用SetupImage,它设置System.Windows.Controls.Image对象的源。
我试过两种旋转图像的方式(当宽度小于高度时)都在这个代码中。当我在BitmapImage上使用Rotation时,不会影响显示的图像。当我在图像上使用RotateTransform时,图像根本不显示。
public class TrayImage
{
[XmlAttribute("id")]
public string ID { get; set; }
[XmlAttribute("data")]
public string Data { get; set; }
/// <summary>
/// Create an Image from the Base64 Data
/// </summary>
internal void SetupImage(ref System.Windows.Controls.Image image)
{
if (this.Data != null)
{
// Convert the Base64 to a BitmapImage
byte[] _BinaryData = Convert.FromBase64String(this.Data);
BitmapImage _ImageBitmap = new BitmapImage();
_ImageBitmap.BeginInit();
_ImageBitmap.StreamSource = new MemoryStream(_BinaryData);
_ImageBitmap.EndInit();
// If the image is portrait, rotate it
if (_ImageBitmap.Width < _ImageBitmap.Height)
{
// only use one rotation method at a time!!
//_ImageBitmap.Rotation = Rotation.Rotate90;
}
image.Source = _ImageBitmap;
// If the image is portrait, rotate it
if(image.Source.Width < image.Source.Height)
{
// only use one rotation method at a time!!
RotateTransform _RotateTransform = new RotateTransform(90);
image.RenderTransform = _RotateTransform;
}
}
}
}
我应该使用别的东西来转换,然后旋转图像?
包括错误或什么是不工作....还我不使用REF上的粉丝引用类型,如果我是你,我会将其转换为静态方法。 – Seabizkit
我没有收到错误信息,图像只是以原始方向显示,或根本不显示。我在代码中的其他地方使用了ID,但这里并不相关。 – mike
是的,我认为它不会使用实际上被修改的图像!将其更改为返回图像的静态辅助类。 – Seabizkit