This project has moved and is read-only. For the latest updates, please go here.

Write Images

Feb 23, 2012 at 10:34 AM

Can we write images stored in database?

Feb 23, 2012 at 10:37 AM

What's stopping you?

Feb 23, 2012 at 12:18 PM
MadBoy wrote:

What's stopping you?


Thank you for the reply MadBoy. I have really not explored the application i just downloaded and was checking whether this app will provide support to write images from database which are in binary format. Can you please help me with this info?

Feb 23, 2012 at 12:23 PM

Well the simplest approach (without even checking code) would be to get the file from database, save it temporary as file (.gif / .jpg or other file format whichever you keep in the database) and then pass it to DocX. This is for sure doable.

 

What I am doing for example is I have DocX template in database that I get from database and save it as MemoryStream and pass it as variable to DocX. Not sure if you can do that with jpg or similar as I haven't explored this but for sure the first way is possible..

Feb 23, 2012 at 12:31 PM

Thank you MadBoy. Yes first approach is doable.

But i have lot of images stored. So i can't afford to write the images on server and then refer. And these word documents need to be downloadable to the clients. So the first approach will not server my cause.

I had looked into OpenOffice productivity tool it has the provision of writing images using image part. But the code generated from that tool is too complex and lot of which would really not be required.

 

Feb 23, 2012 at 12:41 PM
Edited Feb 23, 2012 at 12:44 PM
Considering that you put Image into Binary in more or less this way:

            byte[] file;
            using (var stream = new FileStream(varFilePath, FileMode.Open, FileAccess.Read))
            using (var reader = new BinaryReader(stream))
                file = reader.ReadBytes((int) stream.Length);

Then you get it back more or less like this:

        public static MemoryStream databaseFileRead(string varID) {
            var memoryStream = new MemoryStream();
            using (SqlConnection varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
            using (var sqlQuery = new SqlCommand(@"SELECT [RaportPlik] FROM [dbo].[Raporty] WHERE [RaportID] = @varID", varConnection)) {
                sqlQuery.Parameters.AddWithValue("@varID", varID);
                using (SqlDataReader sqlQueryResult = sqlQuery.ExecuteReader()) {
                    sqlQueryResult.Read();
                    var blob = new Byte[(sqlQueryResult.GetBytes(0, 0, null, 0, Int32.MaxValue))];
                    sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length);
                    //using (var fs = new MemoryStream(memoryStream, FileMode.Create, FileAccess.Write)) {
                    memoryStream.Write(blob, 0, blob.Length);
                    //}
                }
            }
            return memoryStream;
        }

Then all you have to do is convert byte[] or MemoryStream into an Image:

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

public Image MemoryStreamToImage(MemoryStream ms){
    
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}



And then use something like this:


// Add an Image to the docx file
Novacode.Image img = MemoryStreamToImage(memoryStream);

// Insert an emptyParagraph into this document.
Paragraph p = document.InsertParagraph("", false);

#region pic1
Picture pic1 = p.InsertPicture(img.Id, "Donkey", "Taken on Omey island");


And that should be it :-) More or less .. any errors are likely to happen because I copied pasted from multiple sources
Feb 23, 2012 at 12:57 PM

Thank you MadBoy. Appreciate your support. Will try the solution suggested by you and update you if it works.

Can I get more help from you to understand can we have Content Controler template and merly replace the values/text in the content controler based on the ID?

 

Feb 23, 2012 at 1:08 PM

I haven't used Content Controler. Try on stackoverlow.com. It's Great site if you describe the problem properly, the best support you can get is there. Responses are almost instant.

Feb 23, 2012 at 1:59 PM

Appreciate  and Thank you for the support and info.