0

I have a string value in cs code from [Image] column of mytable in mydatabase that type is varbinary(Max), now want to set value to source of Image control.

I wrote this code, but not set image to source :

string strImage = "0xFFD8FFE000104A46494600010....."

static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

In constructor :

var bytes = GetBytes(strImage);
var myImage = new MyImageModel {Content = bytes};
MyImg.DataContext = myImage;

In Xaml:

<Image x:Name="MyImg" Source="{Binding Path=Content}"/>     

The above code does not work and is not an error.

Mohsen
  • 231
  • 5
  • 17
  • Are you implementing PropertyChanged for your Content property? – Krishna May 20 '15 at 09:41
  • @Krishna That isn't necessary here. – Clemens May 20 '15 at 09:49
  • 1
    What is the actual format of the binary data? Is it an encoded image buffer? How is it encoded? – Clemens May 20 '15 at 09:54
  • @Clemens my question not duplicate with [How do you convert Byte Array to Hexadecimal String, and vice versa?](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa) – Mohsen May 20 '15 at 11:26
  • Your problem is the conversion from `strImage` to a byte array that contains a valid encoded image buffer. You still haven't told us *anything* about the encoding. When you run your application in debugger, take a look at the Output window in Visual Studio. You should see a data binding error message there, resulting from a failed conversion. – Clemens May 20 '15 at 11:47

2 Answers2

0

You can use a converter and bind your byte string to the Source property of the image. (So Content is your byte string)

<converters:BytesToImageConverter x:Key="BytesToImageConverter" />
<Image x:Name="MyImg" Source="{Binding Path=Content, Converter={StaticResource BytesToImageConverter}}"/>     

Converter code:

   public class BytesToImageConverter : IValueConverter
   {
       public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
       {
           if (value == null)
               return null;

           if (!(value is byte[]))
               throw new ArgumentException("Only byte[] values are supported.");

           if (((byte[])value).Length == 0)
               return null;

           // return new ImageSourceConverter().ConvertFrom(value);
           BitmapImage image = new BitmapImage();
           using (MemoryStream ms = new MemoryStream((byte[])value))
           {
               ms.Position = 0;
               image.BeginInit();
               image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
               image.CacheOption = BitmapCacheOption.OnLoad;
               image.UriSource = null;
               image.StreamSource = ms;
               image.EndInit();
           }
           image.Freeze();

           return image;
       }

       public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
       {
           return value;
       }
   }
Loetn
  • 3,832
  • 25
  • 41
  • I'm always wondering why people set a BitmapImage's `UriSource` property to null when this is the default value anyway. – Clemens May 20 '15 at 09:56
  • @Clemens Just to be sure? :) Or a habit.. – Loetn May 20 '15 at 09:57
  • Sure about what? Didn't you create the BitmapImage instance right before? That said, you wouldn't actually *need* a converter. You can directly bind `Image.Source` to a `byte[]` property, as OP did. Built-in type conversion will do the rest. – Clemens May 20 '15 at 10:01
  • @Loetn,now use your method,but the following error in line image.EndInit() : Exception > "No imaging component suitable to complete this operation was found." and innerException > "The component cannot be found. (Exception from HRESULT: 0x88982F50)" – Mohsen May 20 '15 at 10:22
-1

For converting string code to Image File Use the conversion Like this

public BitmapImage Base64ToImage( byte[] byteArray)
{
  byteArray;  // your bytes
                        BitmapImage img = new BitmapImage();
                        using (MemoryStream stream = new MemoryStream(byteArray))
                        {
                            img.BeginInit();
                            img.StreamSource = stream;
                            img.CacheOption = BitmapCacheOption.OnLoad;
                            img.EndInit();
                            img.Freeze();
                        }
                       return img;
}

It will Give you a Image you can Directly Bind this Image to UI

Dinesh balan
  • 495
  • 4
  • 15