WebControl.Image to Itextsharp.Image

2

I have the following code that converts a blob from a database to an image url ( WebControls.Image ):

    OracleCommand cmd2 = new OracleCommand();
                cmd2.Connection = conn1;
                cmd2.CommandText = "select simbolo from simbolos_ll where idsimb=" + id;
                OracleDataReader dr2 = cmd2.ExecuteReader();
                dr2.Read();
                OracleLob blob = null;
                blob = dr2.GetOracleLob(0);
                byte[] bytes = (byte[])blob.Value;
                string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                url = "data:image/png;base64," + base64String;

This way I pass it to a List<string> to have the urls of the Img stored (I do it in the code but I do not want to show a tochaco):

  List<string> imgPDF = new List<string>();

Starting from this point, I have to: With the URL of the image create a itextSharp.Image .

  iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagenesPDF[idBlob-1]);

And I miss the following error:

"The URI prefix is not recognized."

The question is this: Having the url of the image, how can I create the image of itextSharp.Image ?

    
asked by Geraniego 26.04.2017 в 17:08
source

2 answers

0

I already solved my problem by myself. The problem was that the format of uri was not correct, so, as iTextSharp.text.Image.GetInstance(); has many ways to generate and one of them is to pass a string of bits, was what I did as follows:

    while (dr2.Read())
    {
        Byte[] bytes = (Byte[])dr2[0];
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bytes);
        imagenesPDF.Add(img);
    }

This way I charge the Blob in bits and I pass it directly to the constructor.

Thank you very much for helping everyone. I hope someone serves you this

    
answered by 27.04.2017 / 10:50
source
1

The problem with which you are finding is that the URI that can interpret iTextSharp has to go without the prefix data.

To solve this we would have several options:

Option 1: Remove the prefix data

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagenesPDF[idBlob-1].Replace("data:image/png;base64,",""));

We add the replace to eliminate the prefix that is bothering you by passing only the image in StringBase64 .

If the fact that you put the prefix data is only for use with iTextSharp , you could save yourself adding it and it should work correctly.

string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
url = base64String;

There we save to add the prefix and get only the image in StringBase64 .

Option 2: Directly pass the byte array of the image

With the premise of before, where only the image obtained from the database is used to process it with iTextSharp , we can directly pass it the array of bytes.

So instead of saving a list of strings, we save a list of array of bytes and use them directly.

To put a code analogous to the one you pass to us, it would be like this:

List<byte[]> imgPDF = new List<byte[]>();

OracleCommand cmd2 = new OracleCommand();
                cmd2.Connection = conn1;
                cmd2.CommandText = "select simbolo from simbolos_ll where idsimb=" + id;
                OracleDataReader dr2 = cmd2.ExecuteReader();
                dr2.Read();
                OracleLob blob = null;
                blob = dr2.GetOracleLob(0);
                byte[] bytes = (byte[])blob.Value;
                imgPDF.Add(bytes);

And the last line remains without modifications.

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagenesPDF[idBlob-1]);

imagenesPDF would now be an array of bytes and the image instance of iTextSharp should be executed correctly.

    
answered by 26.04.2017 в 18:05