0

I have an image stored in my database in base64 format. The first question I want to ask here is that is it the best way to store it ?

Next, I am trying to put this image in a Word document via openxml. I have already filled the document with simple text but I am unable to convert my base64 string to an image and then put it in the word document.

Any suggestions or guidance would be appreciable.

Weedoze
  • 13,683
  • 1
  • 33
  • 63
  • Base64 seems like a bad decision, images will be around 1.3~1.4 times bigger than the original. You can save them as **blob**s. But for the converting there is **Convert.FromBase64String()**, if the converting is the problem, and not the placement. See [How do I decode a base64 encoded string?](http://stackoverflow.com/questions/7134837/how-do-i-decode-a-base64-encoded-string) – Bakudan Apr 21 '15 at 12:59
  • My problem is more about the placement but thanks for the converting tips – Weedoze Apr 21 '15 at 13:00

1 Answers1

1

To add an image use can use an ImagePart object which you then pass a stream to. This is detailed on msdn,

MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
using (FileStream stream = new FileStream(fileName, FileMode.Open))
{
    imagePart.FeedData(stream);
}
AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));

ImagePart.FeedData takes a Stream so where the example uses a FileStream, you can swap that out and use a MemoryStream.

var bytes = System.Convert.FromBase64String(yourBase64String);
using (Stream stream = new MemoryStream(byteArray))
{
    imagePart.FeedData(stream);
}
Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99