This project is read-only.

Gif stream to writeablebitmap problem

Aug 18, 2009 at 5:15 PM

Hello,

I'm trying to get a gif stream, then use imagetools to convert it to a png stream so it can be used by a writeable bitmap.

For some reason, I keep getting only a black image:

   Stream fileStream = fu.File.OpenRead();
   ImageTools.Image img = new ImageTools.Image();
   img.SetSource(fileStream);

  WriteableBitmap w = new WriteableBitmap(img.Width, img.Height);
  w.SetSource(img.ToStreamByExtension("png"));
  w.Invalidate();

Also, once it's in the writeable bitmap as a png stream, could it be saved as a a jpg?

Thanks!

Aug 18, 2009 at 5:55 PM

Hi neo,

First question: Which version do you use? I tested your code with the newest source and all my comments are about this version. I also recommend to build the latest version, you can also profit from a bug in the gif decoder, which I have fixed while testing your code.

About your code:

(1) It looks fine more or less: If you just want to display a gif, I recommend to use AnimatedImage control instead of the default Image control. It supports all file formats and also handles multithreading stuff and animates your gif (if it is animated).

(2) If you need a writablebitmap just use the ToBitmap extension Method instead of ToStreamByExtension. Its much faster.

(3) ImageTools works asynchronously be default. SetSource loads the image in background. Consume the ImageLoaded event, but have in mind, that your event handler will raised in another thread than the gui thread and you have to use the Dispatcher. If you want to make it Synchronously, the Image class has a property IsLoadingSynchronously. Set this property to true, before calling SetSource...

(4) Register the gif decoder first: Decoders.AddDecoder<GifDecoder>();

(5) Do you want to save the writeablebitmap, because it will be manipulated or the loaded image?

  If you want to save the writablebitmap just do the following:

              var image = myBitmap.ToImage(); // Where myBitmap is an instance of the WritableBitmap

 Now you can just save the image. First add the reference to the Jpeg Assembly, than register the Encoder: Encoders.AddEncoder<GifEncoder>();

 Then just call image.ToStreamByExtension("jpeg"); to get a jpeg stream.

 

I hope this answers all your questions.

 

Aug 19, 2009 at 7:57 PM
malignate88 wrote:

Hi neo,

First question: Which version do you use? I tested your code with the newest source and all my comments are about this version. I also recommend to build the latest version, you can also profit from a bug in the gif decoder, which I have fixed while testing your code.

About your code:

(1) It looks fine more or less: If you just want to display a gif, I recommend to use AnimatedImage control instead of the default Image control. It supports all file formats and also handles multithreading stuff and animates your gif (if it is animated).

(2) If you need a writablebitmap just use the ToBitmap extension Method instead of ToStreamByExtension. Its much faster.

(3) ImageTools works asynchronously be default. SetSource loads the image in background. Consume the ImageLoaded event, but have in mind, that your event handler will raised in another thread than the gui thread and you have to use the Dispatcher. If you want to make it Synchronously, the Image class has a property IsLoadingSynchronously. Set this property to true, before calling SetSource...

(4) Register the gif decoder first: Decoders.AddDecoder<GifDecoder>();

(5) Do you want to save the writeablebitmap, because it will be manipulated or the loaded image?

  If you want to save the writablebitmap just do the following:

              var image = myBitmap.ToImage(); // Where myBitmap is an instance of the WritableBitmap

 Now you can just save the image. First add the reference to the Jpeg Assembly, than register the Encoder: Encoders.AddEncoder<GifEncoder>();

 Then just call image.ToStreamByExtension("jpeg"); to get a jpeg stream.

 

I hope this answers all your questions.

 

Hey malignate88,

Thank you for the quick response! You answered my question and everything works great after I got the latest version.

Thank you for the help!

Aug 29, 2009 at 9:01 AM

Hi, malignate88,

First of all, thanks a lot for excellent library.

I try a code written in the beginning of this post, but img.SetSource(fileStream) causes NotSupportedException. How I can load an image from a file into ImageTools.Image object? As a whole my goal - resize the image, I would be very grateful, if you have advised the easiest way to reach it by the use of your library.

Aug 29, 2009 at 12:24 PM
Hi journeyman,
Have you added to decoders you need? Choose the decoders you need and
just add them. You can also send me your code, but I cannot have a look
before Wednesday evening.


Here is a code for loading an image and resize it. I have no visual
studio at the moment, so I cannot test it, but I think it should work.
Decoders.AddDecoder<GifDecoder>();

var fileDialog = new OpenFileDialog fileDialog();

if (fileDialog.ShowDialog() == true)
{
var image = new Image();
image.IsLoadingSynchronously = true; // Just load the image in the
same thread.

using (var stream = fileDialog.OpenRead())
{
image.SetSource(fileDialog.OpenRead();
}

// Image is loaded now

// Just resize the image to half width and height
Image.Resize(image, image.Width / 2, image.Height / 2, new
NearestNeighborResizer());
}
Aug 31, 2009 at 11:07 AM

Hi, malignate88,

Thanks for fast response. I still have few questions.

I try the following code:

Decoders.AddDecoder<JpegDecoder>();
ImageTools.Image image = new ImageTools.Image();
OpenFileDialog ofd = new OpenFileDialog();
bool? result = ofd.ShowDialog();
if (result.HasValue && result.Value == true)
{
    image.IsLoadingSynchronously = true;
    using (Stream stream = ofd.File.OpenRead())
    {
        image.SetSource(stream);
    } 
}

But on "image.SetSource(stream)" line ImageTools.IO.UnsupportedImageFormatException was thrown.... (I select jpg image of course)

And second question: how I can save ImageTools.Image object to a stream (e.g. for saving to file). In the "Write the content of a canvas to a file" Howto written that I can use WriteToStream() method of Image class, but I have not found it in the source that I built and use (27034).

Thanks in advance.

 

Aug 31, 2009 at 11:32 AM
Mhm, the code looks fine. Have you made your tests just with one image
or have you used different images? Can you send me this image? Perhaps
it is just a png image with a wrong extension or something like this.

I still have no visual studio, but I think I can test it wednesday
evening, if you send my the image.

To the second question: Do you have included the ImageTools.Util
assembly? The extension methods are included in this dll.
Aug 31, 2009 at 8:31 PM

I have made some tests for the various images made digital cameras, the majority of them (however not all) causes the above described exception.

One of the pictures I have placed here:  http://rapidshare.com/files/273975063/P1000078.JPG.html

I have found WriteToStream() extension method, works fine, thanks.

 

 

Aug 31, 2009 at 9:04 PM
Thanks for this sample image. I will test it as fast as I can.
Sep 3, 2009 at 9:11 PM
Hi,
I have tested your image. The problem is that it is not really a jpg
file. It is a xif image, very similar to an jpeg, but not the same. I
tried to get a specification, but I couldn't found one. Perhaps you have
more look than me. ;)
Sep 4, 2009 at 9:18 AM

Hi, malignate88,

I'm not the expert on graphics formats, can suppose only. In your code the file header is checked on presence of "JFIF" from 6 to 9 bytes.  In "wrong" images there contains "Exif". Maybe check format  function just doesn't take into consideration EXIF metadata or something like this? Because the files I tested are made by different cameras and all software recognised them as JPEG, it is unlikely that they were non-JPEG. For a while I have commented out JpegDecoder.IsSuppotredFileFormat() call - and all works perfectly. :) I wish you every success in further development of this excellent library.

Sep 4, 2009 at 10:48 AM
Thanks for this information. I will change the method as you mentioned
and make some tests :)