MFC调用C#dll类库中new StreamWriter()函数写入TXT失败

小白827 2017-11-20 10:18:41
写了一个mfc框架需要将PDF分解为图片,然后将图片地址保存在TXT中,然后在mfc中使用这些路径进行下一步操作,可是,PDF分解为图片实现了,但是将图片路径写入TXT的操作没有解决,不知道哪里出错了,求大神指教

C#核心代码


public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution)
{
StreamWriter sw = new StreamWriter("E:\\test.txt", false, Encoding.ASCII);
Acrobat.CAcroPDDoc pdfDoc = null;
Acrobat.CAcroPDPage pdfPage = null;
Acrobat.CAcroRect pdfRect = null;
Acrobat.CAcroPoint pdfPoint = null;

// Create the document (Can only create the AcroExch.PDDoc object using late-binding)
// Note using VisualBasic helper functions, have to add reference to DLL
pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");

// validate parameter
if (!pdfDoc.Open(pdfInputPath)) { throw new FileNotFoundException(); }
if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
if (startPageNum <= 0) { startPageNum = 1; }
if (endPageNum > pdfDoc.GetNumPages() || endPageNum <= 0) { endPageNum = pdfDoc.GetNumPages(); }
if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; }
if (imageFormat == null) { imageFormat = ImageFormat.Jpeg; }
if (resolution <= 0) { resolution = 1; }

// start to convert each page
for (int i = startPageNum; i <= endPageNum; i++)
{
pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1);
pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");

int imgWidth = (int)((double)pdfPoint.x * resolution);
int imgHeight = (int)((double)pdfPoint.y * resolution);
sw.WriteLine("hellowe world");
pdfRect.Left = 0;
pdfRect.right = (short)imgWidth;
pdfRect.Top = 0;
pdfRect.bottom = (short)imgHeight;

// Render to clipboard, scaled by 100 percent (ie. original size)
// Even though we want a smaller image, better for us to scale in .NET
// than Acrobat as it would greek out small text
pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * resolution));

IDataObject clipboardData = Clipboard.GetDataObject();

if (clipboardData.GetDataPresent(DataFormats.Bitmap))
{
Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
pdfBitmap.Save(Path.Combine(imageOutputPath, imageName) + i.ToString() + "." + imageFormat.ToString(), imageFormat);
pdfBitmap.Dispose();
}
}

pdfDoc.Close();
Marshal.ReleaseComObject(pdfPage);
Marshal.ReleaseComObject(pdfRect);
Marshal.ReleaseComObject(pdfDoc);
Marshal.ReleaseComObject(pdfPoint);

}



mfc调用代码
Program::ConvertPDF2Image(str_pdf, "E:\\data1\\", "", 0, 0, ImageFormat::Jpeg, 1);
...全文
438 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-11-21
  • 打赏
  • 举报
回复
全部折叠全部展开 代码:全部 代码:多个 代码:Visual Basic 代码:C# 代码:Visual C++ 代码:J# 代码:JScript Visual Basic C# Visual C++ J# JScript .NET Framework 开发人员指南 streamWriterBufferedDataLost MDA 请参见 发送反馈意见 当对 StreamWriter 执行写入,但是随后未在销毁 StreamWriter 的实例之前调用 Flush 或 Close 方法时,将激活 streamWriterBufferedDataLost 托管调试助手 (MDA)。当启用此 MDA 时,运行库将确定 StreamWriter 中是否仍然存在任何缓冲数据。如果不存在缓冲数据,则激活该 MDA。调用 Collect 和 WaitForPendingFinalizers 方法可以强制终结器运行。否则终结器似乎将在任意时间运行,并且可能在进程退出时根本就不运行。在启用此 MDA 的情况下显式运行终结器将有助于更可靠地重现此类问题。 症状 StreamWriter 未将最后 1 至 4 KB 数据写到文件。 原因 StreamWriter 在内部缓冲数据,这需要调用 Close 或 Flush 方法将缓冲数据写到基础数据存储区。如果没有适当地调用 Close 或 Flush,StreamWriter 实例中缓冲的数据可能不会按预期写出。 下面是此 MDA 应该捕获的编写得很糟糕的代码的示例。 复制代码 // Poorly written code. void Write() { StreamWriter sw = new StreamWriter("file.txt"); sw.WriteLine("Data"); // Problem: forgot to close the StreamWriter. } 如果触发垃圾回收,然后将其挂起直至终结器完成,则上面的代码将更可靠地激活此 MDA。若要跟踪此类问题,可以在调试版本中将下面的代码添加到上面的方法的结尾处。这样将有助于可靠地激活 MDA,不过它并未解决此类问题的根源。 复制代码 GC.Collect(); GC.WaitForPendingFinalizers(); 解决办法 在关闭具有 StreamWriter 的实例的应用程序或任何代码块之前,确保调用 StreamWriter 的 Close 或 Flush。达到此目的的最佳机制之一是用 C# using 块(在 Visual Basic 中为 Using)创建该实例,这样将确保调用编写器的 Dispose 方法,从而正确关闭该实例。 复制代码 using(StreamWriter sw = new StreamWriter("file.txt")) { sw.WriteLine("Data"); } 下面的代码演示能达到相同效果的解决方案,不过它使用的是 try/finally 而不是 using。 复制代码 StreamWriter sw; try { sw = new StreamWriter("file.txt")); sw.WriteLine("Data"); } finally { if (sw != null) sw.Close(); } 如果不能使用这其中任一种方案(例如,如果 StreamWriter 存储在静态变量中,并且在其生存期结束时很难运行代码),那么在最后使用 StreamWriter 之后调用其上的 Flush,或者在第一次使用它之前将 AutoFlush 属性设置为 true,应该会避免此问题。 复制代码 private static StreamWriter log; // static class constructor. static WriteToFile() { StreamWriter sw = new StreamWriter("log.txt"); sw.AutoFlush = true; // Publish the StreamWriter for other threads. log = sw; } 对运行库的影响 此 MDA 对运行库没有影响。 输出 指示发生此冲突的消息。 配置 复制代码 <mdaConfig> <assistants> <streamWriterBufferedDataLost /> </assistants> </mdaConfig> 请参见 概念 使用托管调试助手诊断错误 参考 StreamWriter 发送反馈意见,就此主题向 Microsoft 发送反馈意见。
赵4老师 2017-11-21
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
StreamWriter是.NET的东东。 请换CFile
哦,没看到你本来就用的是C# 漏掉了应有的sw.Close();
赵4老师 2017-11-21
  • 打赏
  • 举报
回复
StreamWriter是.NET的东东。 请换CFile
小白827 2017-11-20
  • 打赏
  • 举报
回复
大神大神大神大神
小白827 2017-11-20
  • 打赏
  • 举报
回复
大神大神快到碗里来!!!!!!!!!!!!

15,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧