0

I am having this issue and none of the solutions I've read fits in so far. I'm after some wisdom..

I have a Base64 encoded string, which is derived from a PNG image. I have to supply to crystal reports a byte array of Bitmap. Easy to get the byte array from Base64 string of course... so I need some help going from PNG byte array to Bitmap byte array...

Any ideas? Using .NET 3.5 Framework.

dineth
  • 9,822
  • 6
  • 32
  • 39

2 Answers2

0

You will need to use a PngBitmapDecoder.

http://msdn.microsoft.com/en-us/library/aa970062.aspx

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
  • I already found that. The Image class used there (System.Windows.Controls.Image) does not have a save function. So I can't seem to save it back into a byte array... – dineth Feb 14 '12 at 04:16
  • Sure you can; use `BmpBitmapEncoder` next. Its `Save` method will write it out to the storage medium of your choice (including a memory buffer). Or just build up a BMP header yourself, it's really trivial to do. – StilesCrisis Feb 14 '12 at 04:19
  • Ah... I was just going to say that. Completely didn't think of using BmpEncoder... Thanks :) – dineth Feb 14 '12 at 04:19
  • You're welcome. If this is working for you, an upvote or accept would be appreciated :) – StilesCrisis Feb 14 '12 at 04:27
  • Hey, all I get when I go from PngDecoder to BmpEncoder is a black box... PngBitmapDecoder decoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapSource source = decoder.Frames[0]; BmpBitmapEncoder encoder = new BmpBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(source)); MemoryStream bitmapStream = new MemoryStream(); encoder.Save(bitmapStream); Can you help? – dineth Feb 15 '12 at 02:28
  • When you look at the bytes in the memory stream, does it look like image data or like a solid wall of one value (probably 0)? – StilesCrisis Feb 15 '12 at 04:49
  • my issue turned out to be a lot different to what I had initially thought. The black box was being generated due to PNG's transparency being dropped during conversion. I managed to create a Graphics object and draw the PNG into that before getting out the bytes. In any case, appreciate your help and I have marked you as the answer. Thank you. – dineth Feb 15 '12 at 21:32
0

Winform Image class provide Save method.

For WPF, refer to: WPF Image to byte[]

Community
  • 1
  • 1
findcaiyzh
  • 647
  • 3
  • 7