I created a window service to put all of my TIFF files into database and stored them as Byte[]
.
Now I want to be able to display them through Silverlight Image control
So i use the Converter during binding XAML in order to convert the Byte[]
to Bitmap
because the Image.Source
only accept eitheir URI (I don't have the file stored on server so can't use this method) or Bitmap
.
BitmapImage bmi = new BitmapImage();
if (value != null)
{
ImageGallery imageGallery = value as ImageGallery;
byte[] imageContent = imageGallery.ImageContent;
string imageType = imageGallery.ImageType;
using (MemoryStream ms = new MemoryStream(imageContent))
{
bmi.SetSource(ms);
}
}
return bmi;
However, I get the exception at bmi.SetSource(ms)
because Silverlight only supports JPEG and PNG images.
So I did more research and knew that i should convert the bytes of TIFF to bytes of JPEG or PNG then it will work.
To do that I tried two methods:
- Doing the conversion on server: in my RIA service call, after retrieving the
ImageGallery
, I loop through the available image to convert the bytes of TIFF to the bytes of JPEG.
BUT IT DOESN'T WORK.... Can you tell me where I did wrong?
public IQueryable<ImageGallery> GetImageGalleries()
{
var imageGalleries = this.ObjectContext.ImageGalleries.OrderBy(i=>i.ImageName);
foreach (ImageGallery imageGallery in imageGalleries)
{
if (imageGallery.ImageType == ".tif" || imageGallery.ImageType == ".tiff")
{
//Convert the Tiff byte array format into JPEG stream format
System.Drawing.Bitmap dImg = new System.Drawing.Bitmap(new MemoryStream(imageGallery.ImageContent));
MemoryStream ms = new MemoryStream();
dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
//then convert the JPEG stream format into JPEG byte array format
byte[] buf = new byte[ms.Length];
ms.Read(buf, 0, buf.Length);
//Changing the format tiff byte[] of ImageGallery to jpeg byte[]
imageGallery.ImageContent = buf;
}
}
return imageGalleries;
}
- The other solution is to use LibTiff.Net library to convert directly the
Byte[]
of TIFF toWritableBitmap
directly on Silverlight.
However, after digging through their sample application or using Reflector to see the source code functions, I still can't figure out how to use their library to convert the bytes of TIFF to WritableBitmap
JPEG (or PNG) because their sample only show the API for using the search the TIFF in a file directory. In my case, I don't have an existing file on server.
Can someone help me how to show the TIFF file on Image control of Silverlight?
I searched the forum but didn't find any solid answer for this.
thanks
Reference LibTiff.net
Add this class:
Use the exension methods like this:
We began with LibTiff as a solution for our media manager. I wouldn't recommend it.
As you can see it creates a WriteableBitmap for each page. WB is the most performance hampering, leaking object you can use in Silverlight, so if you got more then 1 single page tiff your app will run out of memory faster then you can say Avada Kedavra.
There are viewers that appearently can load a large multipage tiff without killing your app (and browser and computer), for a decent license fee, but at this point I got nothing that allows you to decode a tiff an extract the pages.
Runner ups:
I think the LibTiff will be the way to go. Ulitmately the
Tiff.ClientData
accepts aStream
that is the tiff data. If your tiff data really is abyte[]
then you just need aMemoryStream
around it. More likely at some point thebyte[]
is pulled from a stream so you probably don't even need this intermedatorybyte[]
/MemoryStream
.