2

I'm faced in a problem which i think is simple to solve, just i did something wrong, so: I have an Android tablet, where the user can draw signature, i get the image (.JPEG) by adb.

    ProcessStartInfo adb_copy = new ProcessStartInfo("C:/SCR/adb/adb.exe");
    adb_copy.Arguments = "pull \"mnt/sdcard/sign.jpg\" \"C:\\SCR\\temp\\sign.jpg\"";
    adb_copy.WindowStyle = ProcessWindowStyle.Hidden;
    Process.Start(adb_copy);

I have two Image variables:

Image WORKER_sign;
Image EMPLOYER_sign;

I load the image in these, and in a picturebox too:

    using (FileStream stream = new FileStream("C:/SCR/temp/sign.jpg", FileMode.Open, FileAccess.Read))
    {
        WORKER_sign = Image.FromStream(stream);
        stream.Close();
    }
    pictureBox3.Image = WORKER_sign;
    pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;

The picturebox shows the images perfectly, however i can't write in a byte array. I tried the following code:

public static byte[] ImageToByte(Image img)
{
    if (img == null) return null;
    byte[] result;
    using (MemoryStream stream = new MemoryStream())
    {
        img.Save(stream, img.RawFormat);
        result = stream.GetBuffer();
    }
    return result;
}
byte[] temparray = ImageToByte(WORKER_SIGN);

But the last line throws me a Generic GDI+ exception, and before this the IntelliTrace shows some System.ObjectDisposedException ("Closed file") too.

What's wrong with my code? Thank you for all the help! :)

OFF: Sorry for my bad ENG...

EDIT: The errors:

Exception:Thrown: "Cannot access a closed Stream." (System.ObjectDisposedException) A System.ObjectDisposedException was thrown: "Cannot access a closed Stream." Time: 2014.08.11. 14:37:49 Thread:Main Thread[7276]

Exception:Thrown: "Generic error in: GDI+." (System.Runtime.InteropServices.ExternalException) A System.Runtime.InteropServices.ExternalException was thrown: "Generic error in: GDI+." Time: 2014.08.11. 14:37:49 Thread:Main Thread[7276]

2 Answers 2

3

Change your code to:

public static byte[] ImageToByte(Image img)
{
    if (img == null) return null;
    byte[] result;
    using (MemoryStream stream = new MemoryStream())
    {
        img.Save(stream, img.RawFormat);
        result = stream.ToArray();
    }
    return result;
}
Sign up to request clarification or add additional context in comments.

3 Comments

img.Save(stream, img.RawFormat); throws the same exceptions :( But thank you :)
@AtanasDesev The problem is not the code you posted but that the stream that was used to load the image was closed. Image.FromStream takes ownership of the stream and the stream must not be closed.
@Dirk Thank you, I just saw the exception in the update post.
0

Thanks for everbody who helped me, I'm figured out the things by this code:

    EMPLOYER_SIGN = new Bitmap(426, 155);
    using (Graphics gr = Graphics.FromImage(EMPLOYER_SIGN))
    {
        gr.SmoothingMode = SmoothingMode.HighQuality;
        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
        gr.DrawImage(Image.FromFile("C:/SCR/temp/sign.jpg"), new Rectangle(0, 0, 426, 155));
    }
    MemoryStream ms = new MemoryStream();
    EMPLOYER_SIGN.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    BARRAY_EMPSIGN = ms.ToArray();
    ms.Dispose();
    pictureBox3.Image = EMPLOYER_SIGN;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.