Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# - Getting error when imageExport.Export(report, memoryStream); with ImageExportFormat.Metafile #721

Open
AlexCol opened this issue Oct 25, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@AlexCol
Copy link

AlexCol commented Oct 25, 2024

I'm trying to print a file of type Metafile (ImageExportFormat.Metafile), but an error occurs when executing imageExport.Export(report, memoryStream);

If I change it to ImageExportFormat.Png or ImageExportFormat.Bmp the error does not occur.

Its a bug or a 'me' error?

The error is (for each page of the document):

System.ArgumentException: Parameter is not valid.
   at System.Drawing.SafeNativeMethods.Gdip.CheckStatus(Int32 status)
   at System.Drawing.Image.get_Height()
   at FastReport.Export.Image.ImageExport.ExportPageBegin(ReportPage page)
   at FastReport.Export.ExportBase.ExportPageNew(Int32 pageNo)

My code:

    private void Print2()
    {
        var reportPath = Path.Combine(_webHostEnvironment.WebRootPath, "reports", "List.frx");
        var report = new Report();

        report.Load(reportPath);
        report.RegisterData(_dataSet, _dataSet.DataSetName);
        report.Prepare();

        var imageExport = new ImageExport
        {
            ImageFormat = ImageExportFormat.Metafile, //changing to Png works
            SeparateFiles = true
        };

        var metaFileStreams = new List<Stream>();
        for (int i = 0; i < report.PreparedPages.Count; i++)
        {
            var memoryStream = new MemoryStream();
            imageExport.PageRange = PageRange.PageNumbers;
            imageExport.PageNumbers = (i + 1).ToString();
            imageExport.Export(report, memoryStream);
            memoryStream.Position = 0;
            metaFileStreams.Add(memoryStream);
        }

        Log.Information($" -- files in memory: {metaFileStreams.Count}");
        PrintMetaFileFromMemory(metaFileStreams);
    }
    private void PrintMetaFileFromMemory(List<Stream> metaFileList)
    {
        var fileIndex = 0;
        PrintDocument pd = new PrintDocument();
        pd.DefaultPageSettings.PaperSize = new PaperSize("A4", 817, 1124);

        pd.DefaultPageSettings.Margins.Bottom = 0;
        pd.DefaultPageSettings.Margins.Top = 0;
        pd.DefaultPageSettings.Margins.Left = 0;
        pd.DefaultPageSettings.Margins.Right = 0;

        pd.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0;
        pd.PrinterSettings.DefaultPageSettings.Margins.Top = 0;
        pd.PrinterSettings.DefaultPageSettings.Margins.Left = 0;
        pd.PrinterSettings.DefaultPageSettings.Margins.Right = 0;

        pd.PrintPage += (sender, args) =>
        {
            if (fileIndex < metaFileList.Count)
            {
                args.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                args.Graphics.CompositingQuality = CompositingQuality.HighQuality;
                args.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

                Image image = Image.FromStream(metaFileList[fileIndex]);
                args.Graphics.DrawImage(image, new Rectangle(0, 0, args.PageBounds.Width, args.PageBounds.Height));
                fileIndex++;
                args.HasMorePages = fileIndex < metaFileList.Count;
            }
            else
            {
                args.HasMorePages = false;
            }
        };

        pd.Print();
    }	
@AlexCol AlexCol added the bug Something isn't working label Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant