gpt4 book ai didi

c# - 将 DOC 转换为 PNG 的问题

转载 作者:行者123 更新时间:2023-11-30 20:48:17 26 4
gpt4 key购买 nike

我有一个奇怪的问题。我有 .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;
}

最佳答案

尝试显式设置 res,

 Image image = Image.FromStream(ms);
Bitmap myBitmap = new Bitmap( image, new Size( 320,480 ) );
myBitmap.Save( "MyImage.png", System.Drawing.Imaging.ImageFormat.Png );

关于c# - 将 DOC 转换为 PNG 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24830027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com