wpfbase64bitmapimagerectanglesimagebrush

Image flipping when convert image to base64


I have a rectangle and fill it with a image

 <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>

Image is a base64 string that i set image for it.

 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;
        }
    }

and use a converter for convert Base64 to imagebrush for fill rectangle .

 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;

My problem is: when I selected a image for this Rectangle fill with myimage flipping.


Solution

  • I use

     FlowDirection="LeftToRight"
    

    it is work for me :)