[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 138
  • Last Modified:

Image shows as black square after saving to a file.

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
townsma
Asked:
townsma
  • 6
  • 4
1 Solution
 
EddieShipmanCommented:
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
 
townsmaAuthor Commented:
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
 
EddieShipmanCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dsuyemotoCommented:
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
 
EddieShipmanCommented:
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
 
townsmaAuthor Commented:
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
 
townsmaAuthor Commented:
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
 
townsmaAuthor Commented:
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
 
EddieShipmanCommented:
It sounds like a bug in the viewer that you are using on the Android side.
0
 
townsmaAuthor Commented:
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
 
townsmaAuthor Commented:
The problem turned out to be a control problem.  Details in my previous comment.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now