Convert a graphics object to a byte array

G

Guest

Is it possible to save a graphics object to a byte array. I have captured a
user's signature and need to save the resulting graphics object in an image
field in a database.

Many thanks,
Ollie
 
D

Darren Shaffer

yes. The Signature control in OpenNETCF exposes a method
(GetBytes() I think it is), but realize that when you use this, you
do not have a well-formed bitmap. In other words, if your goal
is to convert that signature into a byte[] and then ship that byte[]
back to a server (perhaps via SQL CE replication or via web
service call), when you go to reconstitute those bytes into a bitmap,
you will be missing the necessary header information to display the bytes
in a server-side WinForms Picurebox. So the trick is to do an intermediate
save as a file on device and then read that back in to a byte[]
and save the resulting byte[] to your database/send to the server.
This provides the missing header information you need.

Here's a sample:

// let's say this button is at the bottom of a SignatureForm
private void btnOK_Click(object sender, EventArgs e)
{
// pb is an OpenNetCF PictureBoxEx class
pb.SaveImage(@"\Program Files\appName\signature.bmp");
OwnerSignature = _convertBitmapToByteArray(@"\Program
Files\appName\signature.bmp");
File.Delete(@"\Program Files\appName\signature.bmp");

this.Close();
}

private byte[] _convertBitmapToByteArray(string filename)
{
MemoryStream ms = new MemoryStream();
Image img = new Bitmap(filename);
img.Save(ms, ImageFormat.Bmp);

return ms.ToArray();
}

--
Darren Shaffer
..NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com
 
S

Sergey Bogdanov

Darren,

The OpenNETCF.Windows.Forms.PictureBoxEx doesn't support the SaveImage
method at this moment. I believe, you are talking about PictureBoxEx
from SaveImage example [1]. The PictureBoxEx class from this example has
the SaveImage method which saves a picture as a monochrome bitmap.

HTH

[1] http://www.sergeybogdanov.com/Samples/SaveImage.zip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top