1

I am doing a project in which a built in class for DICOM giving me the ImageSource,I want to use that ImageSource in My silverlight Image control. This conversion I am doing through WCF services. I found WCF does not support ImageSource, so I need to convert the output of built in class into Image or else in byte[]. So that I can send that output to Silverlight and in Silverlight client I'll reconvert it to ImageSource and can assign it to Image Control easily.

I googled for this but I did not find any help in there. Can anybody help me to fix this problem or provide me any alternate solution for this. Any help will be appreciated, Thanks in advance.

Note:- I do not have any permission for code modification on the built in class. As its a third party library.

UPDATE:- Brief Description: I have a class let say GetImageSource and in that I have a method say giveImgSource(). Now my questions is: In WCF I have to call this method and after getting ImageSource from this method I need to pass it to my silverlight Client. As WCF doesn't know about ImageSource, so I need to convert the output of this method to some one out of the following or any alternate if you knows:

byte[]
Image
FileStream
MemoryStream etc
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
SharpUrBrain
  • 3,180
  • 5
  • 38
  • 56
  • Possible duplicate of [How to convert ImageSource to Byte array?](https://stackoverflow.com/questions/26814426/how-to-convert-imagesource-to-byte-array) – NoWar Nov 09 '17 at 02:30

3 Answers3

3

Is it a png image? Then use this to convert to byte[]:

var image = (BitmapSource)value;
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (var ms = new MemoryStream())
{
    encoder.Save(ms);
    return ms.ToArray();
}

UPDATE: Decoding:

var bytes = (byte[])value;

var image = new BitmapImage();
image.BeginInit();

if (bytes == null) 
{
    // Processing null case
}
else
{
    using (var ms = new MemoryStream(bytes))
    {
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.StreamSource = ms;

        image.EndInit();
    }
}

return image;
dvvrd
  • 1,679
  • 11
  • 20
  • Thanks for your quick reply, but BitmapEncoder and BitmapFrame is not supporting in WCf – SharpUrBrain Aug 13 '12 at 10:39
  • Many Errors are coming for BitmapEncoder and BitmapSource after using your code like Error1:The type 'System.Windows.Freezable' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. – SharpUrBrain Aug 13 '12 at 12:45
  • @SharpUrBrain: Why do you want to use this code in WCF lib? Or silverlight framework does not support it too? – dvvrd Aug 13 '12 at 12:56
  • @SharpUrBrain: What happens if you reference WindowsBase? – dvvrd Aug 13 '12 at 12:57
  • I shows Error for encoder class i.e. The type 'System.Windows.Markup.IUriContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. – SharpUrBrain Aug 13 '12 at 12:59
  • 1
    @SharpUrBrain: Try to reference all the wanted assemblies – dvvrd Aug 13 '12 at 13:00
  • I added the System.XAML also and it seems its working let me check – SharpUrBrain Aug 13 '12 at 13:01
1

Please refer to the below links for converting ImageSource to byte[]. They use the BitmapSource and WriteableBitmap classes which are available under PresentationCore library.

(1) How to Convert ImageSource to byte[]?

(2) How to Convert ImageSource to byte[] and back to ImageSource?

Hope that it will solve your problem.

Community
  • 1
  • 1
DareToExplore
  • 249
  • 4
  • 15
  • The BitmapSource, BitmapEncoder and BitmapFrame are available under the PresentationCore.dll. So, after adding ref. to this library, you can try the method suggested by dvvrd member also. – DareToExplore Aug 13 '12 at 12:18
  • I tried with the above links but no luck. I got several errors for BitmapSource and Encoder class also like Error1:The type 'System.Windows.Freezable' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. – SharpUrBrain Aug 13 '12 at 12:47
  • 1
    @DareToExplore; PresentationCore.dll is .NET framework assembly that contains WPF implemented. The target framework in the question is Silverlight and it is incompatible with usual .NET assemblies – dvvrd Aug 13 '12 at 13:12
  • 1
    @dvvrd : Hi, As per my understanding, we need to do imageSource to byte[] conversion on the WCF services side. The client target framework (Silverlight) is where the decoding portion will come and it is a diff. story. So, request you to let me know what will the incompatiblities issues with the .NET assemblies on the service side. Also, then which library should be used here to have the BitmapSource classes, etc. Thnks. – DareToExplore Aug 13 '12 at 13:57
0

The following two helper methods should be able to do the trick:

public BitmapImage ImageFromBuffer(Byte[] bytes)
{
    MemoryStream stream = new MemoryStream(bytes);
    BitmapImage image = new BitmapImage();
    image.BeginInit();
    image.StreamSource = stream;
    image.EndInit();
    return image;
}

public Byte[] BufferFromImage(BitmapImage imageSource)
{
    Stream stream = imageSource.StreamSource;
    Byte[] buffer = null;
    if (stream != null && stream.Length > 0)
    {
        using (BinaryReader br = new BinaryReader(stream))
        {
            buffer = br.ReadBytes((Int32)stream.Length);
        }
    }

    return buffer;
}
JSJ
  • 5,653
  • 3
  • 25
  • 32