Solved

Image shows as black square after saving to a file.

Posted on 2016-10-26
11
49 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

776 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