2012-12-11 90 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