0

I am using this Topaz Signature Pad and it saves ASCII string upon final signature to the Database. Now i have a Problem i want to get the ASCII string from the database and convert it to an Image and show in a Picturebox Control I have this code below :

private void button4_Click(object sender, EventArgs e)
{
    string constring = @"Data Source=DESKTOP-FJBB72F\SQLEXPRESS;Initial Catalog=SignatureCapture;Integrated Security=True";
    using (SqlConnection con = new SqlConnection(constring))
    {
        con.Open();
        string sql = "select * from SignTable where id =@id";
        using (SqlCommand cm = new SqlCommand(sql, con))
        {
            cm.Parameters.AddWithValue("@id",textBox1.Text);
            try
            {
                using (SqlDataReader rd = cm.ExecuteReader())
                {
                    if (rd.Read())
                    {
                        // byte[] imgData = (byte[])rd["signature"];
                        byte[] imgData = Convert.FromBase64String(rd["signature"].ToString());
                        using (MemoryStream ms = new MemoryStream(imgData))
                        {
                            System.Drawing.Image image = Image.FromStream(ms);
                            //image.Save(@"C:\Users\Administrator\Desktop\UserPhoto.jpg");
                            pictureBox1.Image = image;
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error: "+ex.ToString());
            }
        }
    }
}

And i get an Exception at this Line which looks thus :

enter image description here

Question is , How can i go about retrieving an ASCII and displaying on a picturebox?

Edit

SHows error at this Code .. At this line :

private void button4_Click(object sender, EventArgs e)
{
    string constring = @"Data Source=DESKTOP-FJBB72F\SQLEXPRESS;Initial Catalog=SignatureCapture;Integrated Security=True";
    using (SqlConnection con = new SqlConnection(constring))
    {
        con.Open();
        string sql = "select * from SignTable where id =@id";
        using (SqlCommand cm = new SqlCommand(sql, con))
        {
            cm.Parameters.AddWithValue("@id",textBox1.Text);
            try
            {
                using (SqlDataReader rd = cm.ExecuteReader())
                {
                    if (rd.Read())
                    {
                        // byte[] imgData = (byte[])rd["signature"];
                        //byte[] imgData = Convert.FromBase64String(rd["signature"].ToString());
                        byte[]imgData = Encoding.ASCII.GetBytes(rd["signature"].ToString());
                        using (MemoryStream ms = new MemoryStream(imgData))
                        {
                            System.Drawing.Image image = Image.FromStream(ms); //  Error shows at this Line <------
                            //image.Save(@"C:\Users\Administrator\Desktop\UserPhoto.jpg");
                            pictureBox1.Image = image;
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error: "+ex.ToString());
            }
        }
    }
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 2
    What does this mean? Display an ASCII code in a PictureBox? – Ilyes Jan 16 '19 at 07:56
  • no, is there a way i can convert the ASCII code (as it is saved to nvarchar (max)) in the database column. Is there a way i can convert ASCII to image and show in picturebox – Tarell Rovers Jan 16 '19 at 07:58
  • As you are telling that, it is encoded as ASCII, try to retrieve ASCII bytes, by the below code: byte[] imgData = Encoding.ASCII.GetBytes(s); – Venkataraman R Jan 16 '19 at 08:19
  • @Venkataraman tells me Parameter not Valid on Line 82, I will update to show where the error is ... – Tarell Rovers Jan 16 '19 at 08:26
  • How is the data actually stored? If you're storing arbitrary binary data in a *text*-oriented field in the database, that's likely to be the issue. You'd have to use base64 or something similar when *storing*, not just when retrieving. Or, better, use a binary-oriented field in the database. – Jon Skeet Jan 16 '19 at 08:31
  • @JonSkeet, I stored it as nvarchar(max) – Tarell Rovers Jan 16 '19 at 08:32
  • @TarellRovers the question doesn't make much sense. `ASCII` means just English text. It has nothing to do with images. Windows and .NET use Unicode strings natively though which means they can handle any text just fine. *Your* code on the other hand tries to do something rather strange - read a *database field* as if it were *english text* but then treat it as a *binary image format* – Panagiotis Kanavos Jan 16 '19 at 08:32
  • 1
    @TarellRovers instead of talking about ASCII, explain what `signature` is, what it contains and where it gets its values from. If you wanted to store an image, you should have used `varbinary(max)`. A `varchar(max)` can't store binary data, unless it's encoded somehow, eg as Base64 text – Panagiotis Kanavos Jan 16 '19 at 08:33
  • If `signature` contains a Base64-encoded image, you should convert it to a byte array first using eg `Convert.FromBase64String()`. This [possible duplicate](https://stackoverflow.com/questions/12901705/decoding-base64-stream-to-image) shows just that – Panagiotis Kanavos Jan 16 '19 at 08:37
  • @PanagiotisKanavos, what i am trying to do is this. Iike i said i am using Topaz signature pad, now I used the .NET SDK i found in my visual studio and i have been able to save the normal signature string to the database after signing, Now i am trying to save the base64 string so i can use it as an image somewhere on my website / ASP.NET Web application. Hope it makes sense now ? – Tarell Rovers Jan 16 '19 at 08:44
  • @PanagiotisKanavos, After the signature i saved into the database column as nvarchar(max) could it be the issue? – Tarell Rovers Jan 16 '19 at 08:46
  • 1
    @TarellRovers assuming you talk about [this](https://www.topazsystems.com/dotnet.html), the company already provides demo code that stores *images*, not strings. Those are images like SO's logo at the top left of this page, not text. Those images can be stored easily in a `varbinary(max)` field. They *can't* be stored in a `varchar(max)` field unless someone writes code that encodes them as Base64. – Panagiotis Kanavos Jan 16 '19 at 08:48
  • @PanagiotisKanavos, it saves as jpg, tif, bmp, png, etc.and when i do something like this : sigPlusNET1.GetSigString(); it gets signature string – Tarell Rovers Jan 16 '19 at 08:53
  • @TarellRovers and that string is *what*? What does the product's documentation say? Don't force people to guess or read some product's docs to understand what you say. Images aren't strings. A *signature string* isn't a picture of the signature. If you want to store and display an *image* use `GetSigImage`. – Panagiotis Kanavos Jan 16 '19 at 08:57
  • @TarellRovers if you want to load a signaturefile, the demo code shows how to do that with `ImportSigFile`. From the demo's comments `It is important to note that an image contains no biometric info, nor can be encrypted. If your goal is to store a biometric signature, please look into storing either the SigString or a SIG file` – Panagiotis Kanavos Jan 16 '19 at 08:59
  • @TarellRovers a *digital signature* isn't a picture of a signature. Faking someone's signature is trivial. The biometric data used in a signature are *statistics* about individual motions, speed, pen pressure, trajectories etc. If you want to verify signed data *and* display the signatures you'll have to store both the `SIG` data and the bitmap returned by `GetSigImage()` – Panagiotis Kanavos Jan 16 '19 at 09:12

0 Answers0