Solved

Image shows as black square after saving to a file.

Posted on 2016-10-26
11
57 Views
Last Modified: 2016-11-12
I have a signature block tool in an Android App.  This image is converted to a byte[] and saved to an SQL database.

I am pretty sure the byte[] conversion is working as when I in c# take the byte array and convert it to a bitmap, it shows correctly in an image control.  However, if I take the bitmap and save it as a file, in some programs it displays correctly, i.e. Paint, or paint.net etc.  But if I use an "photo" type app, the image is just black.   I have also tried to insert the image into a printed report, and again it just shows as a black rectangle.

Below is my code, I would be grateful for any advice of what I am doing wrong.

This is the code I use to convert the image to a byte array for storing.

private static byte[] MakeBytes(Bitmap bitmap)
        {
            using (var stream = new MemoryStream())
            {
                bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
                var bitmapData = stream.ToArray();
                return bitmapData;
            }
        }

Open in new window


This is the code I use to convert it to an image for displaying in a form, work correctly.

if (inv.Signature == null) return null;

using (var ms = new MemoryStream(inv.Signature))
{
   return (Bitmap)Image.FromStream(ms);
}

Open in new window


Then finally this is the code I use to save the image as a graphics file, to insert into a report.

if (invoice.Signature != null)
 
using (var ms = new MemoryStream(invoice.Signature))
{
       var image =  (Bitmap)Image.FromStream(ms);
       image.Save(tempFileName);
}

Open in new window


As you can see the code is pretty simple, so I have no idea why I am having these issues.
0
Comment
Question by:townsma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 41867653
It is much easier to convert the image to base64 (Mime encoded) and saving the result then converting it back to an image later.

There are several image->base64->Image conversion routines here and on the internet.
0
 
LVL 6

Author Comment

by:townsma
ID: 41867752
From what I can see, converting to base64 is just one step further than converting to a byte array, as to convert to base64, you first convert to a byte array, then convert the byte array to a base64 string.  

By my understanding the purpose of base64 encoding is for places where you cannot use a byte array, i.e. on the web.  But in our case we have no problems storing byte[] or using byte[] as we do not go out on to the web etc. so I think there is no need to add the extra step.

Thanks
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 41868432
Have you seen this post?
http://www.codeproject.com/Articles/15460/C-Image-to-Byte-Array-and-Byte-Array-to-Image-Conv

Especially look at the refactored code in the comments.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:dsuyemoto
ID: 41868866
Have you tried different compression formats (i.e. 'Bitmap.CompressFormat.Png') to rule out issues with the format?  It seems that if some applications can open it ok, that the conversion is working ok.  What photo apps were you using and what kind of document were you inserting into?
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 41868996
I was wondering if he was somehow saving it in the incorrect format, i.e.
(Bitmap)Image.FromStream(ms)

and the image editor he is opening it in reads the file type from the data vs. the extension.
0
 
LVL 6

Author Comment

by:townsma
ID: 41875076
Hi Eddie,

I did think this may be a file type issue, so after I stored it as a file, I renamed the file using all the different graphics extensions, but I received the black square in all cases.
0
 
LVL 6

Author Comment

by:townsma
ID: 41875077
Hi Dsuyemoto,

I did try the other compression formats, but the results were very similar.  The image comes from a signature control in Xamarin, not from a photo application. It has a property of type Bitmap that contains the image.

What I am trying to do with the image, is insert it into a crystal reports report, i.e. on the bottom of a guest bill.

Many thanks
0
 
LVL 6

Author Comment

by:townsma
ID: 41875083
HI Eddie,

I had a look at the article you referred me to.  However, this relies on System.Drawing.Image, which does not exist in the Xamarin world.  According to all the Xamarin experts the correct way to convert an image to a byte[] is to use the Compress function I currently use.

Thanks
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 41877367
It sounds like a bug in the viewer that you are using on the Android side.
0
 
LVL 6

Accepted Solution

by:
townsma earned 0 total points
ID: 41878269
Hi Guys, I have found a solution.  The problem was the Android control, and the fact that by default it tries to use a transparent background, but saves with a file format that does not support transparency.  So by forcing it to use a white background with black text, it works perfectly.

This was very weird, and it was only through searching through literally hundreds of posts that I finally found it.
0
 
LVL 6

Author Closing Comment

by:townsma
ID: 41884556
The problem turned out to be a control problem.  Details in my previous comment.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question