2012-12-11 98 views
0

我有一個矩形,並用圖像圖像反轉時的圖像轉換爲Base64

<Rectangle Height="95" Fill="{Binding Path=Image,RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay,Converter={StaticResource ConvertBase64toImage}}" Stroke="{x:Null}" x:Name="EditPhotoRectangel"> 
     <Rectangle.BitmapEffect> 
      <DropShadowBitmapEffect ShadowDepth="7" Softness="0.75"/> 
     </Rectangle.BitmapEffect> 
</Rectangle> 

圖片是一個base64字符串,我設置的圖像爲它填充它。

private string ImageToBase64(System.Drawing.Image image, ImageFormat format) 
    { 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      // Convert Image to byte[] 
      image.Save(ms, format); 
      byte[] imageBytes = ms.ToArray(); 

      // Convert byte[] to Base64 String 
      string base64String = Convert.ToBase64String(imageBytes); 
      return base64String; 
     } 
    } 

並使用轉換器將Base64轉換爲用於填充矩形的圖像刷。

ImageBrush brush = new ImageBrush(); 
BitmapImage bitmap = new BitmapImage(); 
// convert base64string to byte[]. 
byte[] binaryData = System.Convert.FromBase64String((string)value); 
bitmap.BeginInit(); 
// Initializes a new non-resizable instance of the MemoryStream class based on the binaryData array. 
bitmap.StreamSource = new MemoryStream(binaryData); 
bitmap.EndInit(); 
//set bitmapimage for brush imagesource 
brush.ImageSource = bitmap; 
return brush; 

我的問題是:當我選擇一個圖像此Rectangle與MYIMAGE翻轉填寫。

回答

1

我用

FlowDirection="LeftToRight" 

這是爲我工作:)

2

這只是胡亂猜測......需要寫一個樣本,以確認...

我注意到您使用System.Drawing.Image來編碼源位二進制數據和System.Windows.Media.BitmapImage的二進制數據的反序列化。

必須有編碼器/在GDI +(的WinForms)解碼器的世界,WPF(的DirectX)之間的支撐一定的差異....特別是你可能有問題,如果你正在使用ImageFormat.MemoryBmp,或myimage.RawFormat(即財產暴露在Image)。 DIB(設備無關位圖)通常以自下而上的方式存儲其掃描線,即顛倒(負跨度)。

因此,它可能與您創建/初始化整個過程的來源System.Drawing.Bitmap的方式有關......也許您使用了CopyPixels ......它指向數據的位置,其中從下到上的像素安排或反之亦然。

您是否嘗試過對該傳入的Bitmap上的物理BMP或JPEG文件執行.Save以查看它是否正確(您甚至可以在調試器中執行此操作)。

如果無法追查這種轉換慘敗的原因,那麼一些可能的解決方案是:

  • 做必要的變換,翻轉圖像回適當的方式 或
  • 使用不同ImageFormat /以不同的方式 或
  • 時反序列化的base64數據回位圖再次使用System.Drawing.Image類(因此您所使用的相同類型的類上的串行化PR的兩端創建傳入Bitmap ocess),然後將該對象轉換爲BitmapImage對象。

如果你能提供你如何創建傳入System.Drawing.Bitmap更詳細,這將有助於即你從文件加載它(該格式是什麼呢?),或是建立一個接着通過Graphics吸引到它上下文中,或者您使用CopyPixels並從某處(例如,從通常將掃描線從下至上存儲的DIB之外)複製原始像素數據。

一些信息鏈接:

+0

謝謝很多,我使用> System.Drawing.Image img = System.Drawing.Image.FromFile(openfile.FileName);獲取圖像。 – Niloo

+0

您使用什麼ImageFormat保存圖像? –

+0

我使用'img.RawFormat' – Niloo