PngEncoder saves an empty image

Jan 4, 2011 at 6:36 AM

Hello,

I'm using the ImageTools PngEncoder to encode a bitmap to a png, but at the end the image saved is nothing but an empty image.

I'm using the following piece of code:

 

MemoryStream ms = new MemoryStream();
FileStream ffStream = new FileStream("C:\\temp.png", System.IO.FileMode.Create, System.IO.FileAccess.Write);

BinaryReader br = new BinaryReader(fStream);
byte[] data = br.ReadBytes((int)numBytes);

ImageTools.IO.Png.PngEncoder encoder = new ImageTools.IO.Png.PngEncoder();
ImageTools.ExtendedImage extendedIMG = new ImageTools.ExtendedImage(writableBitmap.PixelWidth, writableBitmap.PixelHeight);
//extendedIMG.UriSource = new Uri(@path, UriKind.Absolute);
ms = new MemoryStream(data);
extendedIMG.SetSource(ms);
ms.Close();

MemoryStream stream = new MemoryStream();
encoder.Encode(extendedIMG, stream);

ffStream.Write(stream.GetBuffer(), 0, (int)stream.Length);

So, I have a file stream from which I load the bitmap file. I use this to take the file bytes, and send them to a memoryStream that I'll use as a source for the extended Image. After the encoding process is over, I write another memory stream to the png file. I've tried also to set the extended Image UriSource, but the same result. Am I missing something?

I'm using imagetools 0.3

Coordinator
Jan 4, 2011 at 12:11 PM
loading an image is done asynchronously. The loading may not be
completed when you encode. Subscribe to the LoadingCompleted Event:

ExtendedImage image = new ExtendedImage();
image.LoadingCompleted += image_LoadingCompleted;
image.SetSource(ffStream);

private void image_LoadingCompleted(object sender, EventArgs e)
{
ExtendedImage image = (ExtendedImage)sender;

MemoryStream stream = new MemoryStream();
encoder.Encode(image, stream);
}
Jan 4, 2011 at 12:40 PM
Edited Jan 4, 2011 at 1:10 PM

Now I have another problem:

I set the source to the following stream

FileStream ffStream = new FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);

 

and I'm getting an error "Sequence contains no elements"

"   at System.Linq.Enumerable.Max(IEnumerable`1 source)
   at System.Linq.Enumerable.Max[TSource](IEnumerable`1 source, Func`2 selector)
   at ImageTools.ExtendedImage.Load(Stream stream)
   at ImageTools.ExtendedImage.<>c__DisplayClass18.<LoadAsync>b__17(Object objectState)"

in the LoadingFailed event. If I'm using the memoryStream, I get the exact same error. Although I have 6 mb of information in there and I'm able to use the stream and read it.

Any ideas why?

Coordinator
Jan 4, 2011 at 12:42 PM
No, sorry, can u give me the stack trace of the exception?
Jan 4, 2011 at 1:13 PM

"   at System.Linq.Enumerable.Max(IEnumerable`1 source)
   at System.Linq.Enumerable.Max[TSource](IEnumerable`1 source, Func`2 selector)
   at ImageTools.ExtendedImage.Load(Stream stream)
   at ImageTools.ExtendedImage.<>c__DisplayClass18.<LoadAsync>b__17(Object objectState)"

this is the stack trace. No more information exists on this error.

Jan 5, 2011 at 5:38 AM

What I am trying to do is converting a bmp file to png. So I read a bitmap image file, and use that stream to create and extended image that later will be encoded into a png image file. But I can't get it to work, since I get all the time the above error. From what I've seen it has something to do with Linq (maybe you use the Single method somewhere, and that raises sometimes this error).

Jan 5, 2011 at 5:49 AM

I've found the problem: I forgot to add the decoder for bitmap.

same problem as here: http://imagetools.codeplex.com/Thread/View.aspx?ThreadId=231661

Jan 8, 2014 at 5:46 AM
Which prorgamming language are you using? Here are my methods for image saving withn Visual Baic project, this is just one of the functions of my prorgam, and you will see i used an intermediary step to save image, however, you can get rid of it or change to another name.


Shared Sub SaveImageFile(image As REImage, filePath As String)
End Sub
Public Shared Sub SaveImageFile(image As REImage, filePath As String, enc As BaseEncoder)
End Sub