Avatar of townsma
townsma
Flag for Indonesia

asked on 

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.
C#

Avatar of undefined
Last Comment
townsma

8/22/2022 - Mon