Ok I have a problem that just baffles me yet again. I have some code that turns a DOC file to a PNG file. When I do it on a localhost, the image is fine. When I take the same code and put it on live server, the image is extremely small (same size as the DOT file what I got the DOC file from, basically DOT gets filled out and turned into a DOC). Now... here's the crazy part. If I log into the hosting server as an admin and THEN go to the live website, the image is large and crisp, even if I go to the site from an iPhone. As soon as I log out of the hosting server and refresh the live page, image is tiny again. Here's the code I am using to convert DOC to PNG. On a side note, if I use method 2, I can make the image bigger and higher resolution, but fonts are out of place.
private void ConvertDocToPNG(string startupPath, string filename1)
{
var docPath = Path.Combine(startupPath, filename1);
Application app = new Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
app.Visible = false;
doc = app.Documents.Open(docPath);
app.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMaximize;
app.ActiveWindow.ActivePane.View.Zoom.Percentage = 100;
doc.ShowGrammaticalErrors = false;
doc.ShowRevisions = false;
doc.ShowSpellingErrors = false;
//Opens the word document and fetch each page and converts to image
foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows)
{
foreach (Microsoft.Office.Interop.Word.Pane pane in window.Panes)
{
for (var i = 1; i <= pane.Pages.Count; i++)
{
Microsoft.Office.Interop.Word.Page page = null;
bool populated = false;
while (!populated)
{
try
{
// This !@#$ variable won't always be ready to spill its pages. If you step through
// the code, it will always work. If you just execute it, it will crash. So what
// I am doing is letting the code catch up a little by letting the thread sleep
// for a microsecond. The second time around, this variable should populate ok.
page = pane.Pages[i];
populated = true;
}
catch (COMException ex)
{
Thread.Sleep(1);
}
}
var bits = page.EnhMetaFileBits;
var target = Path.Combine(startupPath + "\\", string.Format("{1}_page_{0}", i, filename1.Split('.')[0]));
try
{
using (var ms = new MemoryStream((byte[])(bits)))
{
var image = System.Drawing.Image.FromStream(ms);
var pngTarget = Path.ChangeExtension(target, "png");
// Method 2
image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);
// Another way to save it using custom size
//float width = Convert.ToInt32(hfIdCardMaxWidth.Value);
//float height = Convert.ToInt32(hfIdCardMaxHeight.Value);
//float scale = Math.Min(width / image.Width, height / image.Height);
//int resizedWidth = (int)Math.Round(image.Width * scale);
//int resizedHeight = (int)Math.Round(image.Height * scale);
//Bitmap myBitmap = new Bitmap(image, new Size(resizedWidth, resizedHeight));
//myBitmap.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);
}
}
catch (System.Exception ex)
{
doc.Close(true, Type.Missing, Type.Missing);
Marshal.ReleaseComObject(doc);
doc = null;
app.Quit(true, Type.Missing, Type.Missing);
Marshal.ReleaseComObject(app);
app = null;
throw ex;
}
}
}
}
doc.Close(true, Type.Missing, Type.Missing);
Marshal.ReleaseComObject(doc);
doc = null;
app.Quit(true, Type.Missing, Type.Missing);
Marshal.ReleaseComObject(app);
app = null;
}