Solved

Image shows as black square after saving to a file.

Posted on 2016-10-26
11
42 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
  • 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
 
LVL 2

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now