2015-10-16 97 views
0

我有保存在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; 
      } 
     } 
    } 
} 

我應該使用別的東西來轉換,然後旋轉圖像?

+0

包括錯誤或什麼是不工作....還我不使用REF上的粉絲引用類型,如果我是你,我會將其轉換爲靜態方法。 – Seabizkit

+0

我沒有收到錯誤信息,圖像只是以原始方向顯示,或根本不顯示。我在代碼中的其他地方使用了ID,但這裏並不相關。 – mike

+0

是的,我認爲它不會使用實際上被修改的圖像!將其更改爲返回圖像的靜態輔助類。 – Seabizkit

回答

0

像這樣:這是爲只有代碼沒有測試!

public static class ImageHelpers 
{ 
    punlic byte[] ConvertFromBase64(btye[] data) 
    { 
     return Convert.FromBase64String(data) 
    } 

    public Image Rotate90FromData(byte[] data) 
    { 
     Image image = null; 
     if (data != null) 
     {    
      BitmapImage _ImageBitmap = new BitmapImage(); 
      _ImageBitmap.BeginInit(); 
      _ImageBitmap.StreamSource = new MemoryStream(data); 
      _ImageBitmap.EndInit(); 

      image.Source = _ImageBitmap; 

      // If the image is portrait, rotate it 
      if(image.Source.Width < image.Source.Height) 
      { 
       RotateTransform _RotateTransform = new RotateTransform(90); 
       image.RenderTransform = _RotateTransform; 
      } 
     } 
     return Image; 
    } 
} 

public class Main 
{ 
    public void Start() 
    { 
     var data64 = ImageHelpers.ConvertFromBase64(somevar); 
     Image rotatedImage = ImageHelpers.Rotate90FromData(data64); 

    } 
} 
0

這裏梳理建議後,和其他的問題,我想出了一個可行的解決方案。

這些都是我的「usings」

using System; 
using System.Xml.Serialization; 
using System.IO; 
using System.Windows.Media.Imaging; 
using System.Drawing; 

這裏是我的滿級

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 System.Windows.Controls.Image SetupImage() 
    { 
     System.Windows.Controls.Image _Image = new System.Windows.Controls.Image(); 

     if (this.Data != null) 
     { 
      BitmapImage _BitmapImage = this.CreateBitmapImage(); 
      Bitmap _Bitmap = this.BitmapImage2Bitmap(_BitmapImage); 

      // If the image is portrait, rotate it 
      if (_Bitmap.Width < _Bitmap.Height) 
      { 
       _Bitmap = this.RotateImage(_Bitmap, 90); 
      } 

      _Image.Source = this.BitmapToBitmapImage(_Bitmap); 
     } 

     return _Image; 
    } 

    /// <summary> 
    /// Convert a Bitmap into a BitmapImage 
    /// </summary> 
    private BitmapImage BitmapToBitmapImage(Bitmap bitmap) 
    { 
     using (MemoryStream _MemoryStream = new MemoryStream()) 
     { 
      bitmap.Save(_MemoryStream, System.Drawing.Imaging.ImageFormat.Bmp); 
      _MemoryStream.Position = 0; 
      BitmapImage _BitmapImage = new BitmapImage(); 
      _BitmapImage.BeginInit(); 
      _BitmapImage.StreamSource = _MemoryStream; 
      _BitmapImage.CacheOption = BitmapCacheOption.OnLoad; 
      _BitmapImage.EndInit(); 

      return _BitmapImage; 
     } 
    } 

    /// <summary> 
    /// Convert a Base64 String into a BitmapImage 
    /// </summary> 
    private BitmapImage CreateBitmapImage() 
    { 
     byte[] _BinaryData = Convert.FromBase64String(this.Data); 

     BitmapImage _ImageBitmap = new BitmapImage(); 
     _ImageBitmap.BeginInit(); 
     _ImageBitmap.StreamSource = new MemoryStream(_BinaryData); 
     _ImageBitmap.EndInit(); 

     return _ImageBitmap; 
    } 

    /// <summary> 
    /// Convert a BitmapImage into a Bitmap 
    /// </summary> 
    private Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage) 
    { 
     using (MemoryStream _OutStream = new MemoryStream()) 
     { 
      BitmapEncoder _Encoder = new BmpBitmapEncoder(); 
      _Encoder.Frames.Add(BitmapFrame.Create(bitmapImage)); 
      _Encoder.Save(_OutStream); 
      System.Drawing.Bitmap _Bitmap = new System.Drawing.Bitmap(_OutStream); 

      return new Bitmap(_Bitmap); 
     } 
    } 

    /// <summary> 
    /// Rotate a Bitmap 
    /// </summary 
    private Bitmap RotateImage(Bitmap bitmap, float angle) 
    { 
     int _Width = bitmap.Width; 
     int _Height = bitmap.Height; 
     float _MoveX = (float)_Height/2; 

     // Create bitmap with Height/Width revered 
     Bitmap _ReturnBitmap = new Bitmap(_Height, _Width); 

     // Make a graphics object from the empty bitmap 
     using (Graphics _Graphics = Graphics.FromImage(_ReturnBitmap)) 
     { 
      // Move image along x axis 
      _Graphics.TranslateTransform(_MoveX, 0); 

      // Rotate image 
      _Graphics.RotateTransform(angle); 

      // Move image back along x axis 
      // NB, now it's been rotated, the x and y axis have swapped from our perspective. 
      _Graphics.TranslateTransform(0, -_MoveX); 

      // Draw passed in image onto graphics object 
      _Graphics.DrawImage(bitmap, new Point(0, 0)); 
     } 
     return _ReturnBitmap; 
    } 
} 
相關問題