Home > Software engineering >  MFC call DLL c # class library in the new StreamWriter () function writes TXT failure
MFC call DLL c # class library in the new StreamWriter () function writes TXT failure

Time:09-26

Wrote an MFC framework need to PDF is decomposed into images, and then address the picture stored in TXT, then using the path for the next steps in MFC, but the PDF is decomposed into image, but to write image path into TXT operations are not solved, don't know where I went wrong, a great god teach

C # core code
 

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. The 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 newest - binding)
//Note using visual basic helper functions provides, have to add the reference to the DLL
PdfDoc=(Acrobat. CAcroPDDoc) Microsoft. VisualBasic. Interaction. The CreateObject (" AcroExch. PDDoc ", "");

//validate parameter
if (! PdfDoc. Open (pdfInputPath)) {throw new FileNotFoundException (); }
if (! Directory. The Exists (imageOutputPath)) {Directory. CreateDirectory (imageOutputPath); }
If (startPageNum & lt;=0) {startPageNum=1; }
If (endPageNum & gt; PdfDoc. GetNumPages () | | endPageNum & lt; .=0) {endPageNum=pdfDoc GetNumPages (); }
If (startPageNum & gt; EndPageNum) {int tempPageNum=startPageNum; StartPageNum=endPageNum; EndPageNum=startPageNum; }
If (imageFormat==null) {imageFormat=imageFormat. Jpeg; }
If (resolution & lt;=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. The CreateObject (" AcroExch. The Rect ", "");

Int imgWidth=(int) ((double) pdfPoint. X * resolution).
Int imgHeight=(int) ((double) pdfPoint. Y * resolution).
sw. WriteLine (" world ");
PdfRect. Left=0;
PdfRect. Right=(short) imgWidth;
PdfRect. Top=0;
PdfRect. Bottom=(short) imgHeight;

//Render to the clipboard, scaled by 100 percent (ie., the original size)
//my we want a smaller image, better for us to scale in the.net
//than Acrobat as it order to Greek out small text
PdfPage. CopyToClipboard (pdfRect, 0, 0, (short) resolution (100 *));

IDataObject clipboardData=(https://bbs.csdn.net/topics/Clipboard.GetDataObject);

If (clipboardData. GetDataPresent (DataFormats. Bitmap))
{
Bitmap pdfBitmap=(Bitmap) clipboardData. GetData (DataFormats. Bitmap);
Bine pdfBitmap. Save (Path.Com (imageOutputPath, imageName) + i.T oString () + "" + imageFormat. The ToString (), imageFormat);
PdfBitmap. The Dispose ();
}
}

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

}

 


MFC calling code
Program: : ConvertPDF2Image (str_pdf, "E: \ \ data1 \ ", "", 0, 0, ImageFormat: : Jpeg, 1);

CodePudding user response:

A great god the great god to bowl!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

CodePudding user response:

Great god the great god the great god the great god

CodePudding user response:

StreamWriter is.net dongdong,
Please change to CFile

CodePudding user response:

reference zhao4zhong1 reply: 3/f
StreamWriter is. NET dongdong,
Please change to CFile

Oh, didn't see you already using c #

Missed due sw. The Close ();

CodePudding user response:

All folding expand all code: all code: multiple code: Visual Basic code: c # code: Visual c + + code: j # code: JScript
Visual Basic
C #
Visual c + +
J #
JScript
The.net Framework developers guide
StreamWriterBufferedDataLost MDA
Please see the send feedback


When the StreamWriter write, but not before the destruction of StreamWriter instance then call Flush or Close method, will activate streamWriterBufferedDataLost managed debugging assistant (MDA), when enabled the MDA, runtime will determine whether there is still any data buffer in the StreamWriter, if there is no buffer data, then activate the MDA, call Collect and WaitForPendingFinalizers method can force a finalizer run, otherwise the finalizer seems to be running at any time, and can be run when the process exits don't, in the case of enable this MDA explicitly run a finalizer will contribute to more reliably reproduce this kind of problem,

Symptoms
StreamWriter will not last 1 to 4 KB of data written to a file,

Why
StreamWriter in internal buffer data, it needs to call the Close method or Flush the buffer data write based data storage area, if not properly call the Close or Flush, StreamWriter instances in the buffer data may not be as expected to write,

Below is the MDA should capture write very bad code example,

Copy the code
//Poorly written code.
Void the Write ()
{
StreamWriter sw=new StreamWriter (" file. TXT ");
Sw. WriteLine (" Data ");
//Problem: forgot to close the StreamWriter.
}


If triggers garbage collection, then hang up a finalizer is completed, the above code will more reliably activate the MDA, if you want to track this kind of problem, can add the code below in debug version by the end of the above methods, this will help to reliably activate the MDA, but it did not solve the root of these problems,

Copy the code
GC. Collect ();
GC. WaitForPendingFinalizers ();


The solution
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related