【发布时间】:2014-07-18 16:43:35
【问题描述】:
我有一个奇怪的问题。我有 .dot 文件,我填充并转向 .doc 文件。然后我将这个 .doc 文件转换为图像。问题是,图像在本地主机上是完美的(高质量),而在实时服务器上质量很差。我的问题是,如何保存高质量的 .png(或任何其他)图像而不是低质量的图像?这很奇怪,因为相同的代码在本地主机上工作,但在实时服务器上失败。这是我使用的转换方法:
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);
doc.ShowGrammaticalErrors = false;
doc.ShowRevisions = false;
doc.ShowSpellingErrors = false;
//doc.ActiveWindow.ActivePane.View.Zoom.Percentage = 500;
//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");
image.Save(pngTarget, 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;
}
【问题讨论】:
-
服务器是否拥有所有必需的库?特别是那些用于 Microsoft.Office 的?
-
确实如此。我也尝试使用缩放但无济于事。
-
我注意到您将其标记为 ASP.NET 和 Office Interop。 Office Interop 在服务器上不受支持,您可能会遇到很多问题。见MSDN for details。
标签: c# asp.net office-interop