I am using the below code to convert a Word Doc into an image file. But the picture appears too big, and the contents don't fit - is there a way to render the picture or save the picture to size?
private void btnConvert_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtFileName.Text))
{
MessageBox.Show("Choose a document to convert", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtFileName.Focus();
return;
}
ApplicationClass wordApp = new ApplicationClass();
object objectMissing = Missing.Value;
try
{
object fileName = txtFileName.Text;
FileStream fs = new FileStream(fileName.ToString(), FileMode.Open, FileAccess.Read);
Byte[] data = new Byte[fs.Length];
fs.Read(data, 0, data.Length);
Document doc = wordApp.Documents.Open(ref fileName, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,
ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,
ref objectMissing, ref objectMissing, ref objectMissing);
byte[] range = (byte[]) wordApp.ActiveDocument.Content.EnhMetaFileBits;
if (range != null)
{
MemoryStream ms = new MemoryStream(range);
Metafile mf = new Metafile(ms);
picImage.Height = mf.Height;
picImage.Width = mf.Width;
mf.Save("c:\\test.png", ImageFormat.Png);
picImage.Image = Image.FromFile("c:\\test.png");
}
}
finally
{
wordApp.Quit(ref objectMissing, ref objectMissing, ref objectMissing);
}
}