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
4

3 回答 3

3

是png图片吗?然后使用它转换为 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();
}

更新:解码:

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;
于 2012-08-13T10:30:53.140 回答
1

请参考以下链接将 ImageSource 转换为 byte[]。它们使用 PresentationCore 库下提供的 BitmapSource 和 WriteableBitmap 类。

(1)如何将 ImageSource 转换为 byte[]?

(2)如何将 ImageSource 转换为 byte[] 并返回 ImageSource?

希望它能解决您的问题。

于 2012-08-13T11:59:39.693 回答
0

以下两个辅助方法应该可以解决问题:

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;
}
于 2012-08-13T13:19:00.443 回答