Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Image shows as black square after saving to a file.

Posted on 2016-10-26
11
Medium Priority
?
105 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
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.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 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