0

I have a fallowing method which raises exceptions depending on stream size. I have a feeling that the problem is memory limits. Can someone explain me why i get those errors and how to fix this?

    private List<MemoryStream> streams = new List<MemoryStream>();

    private byte[] GetReportImage(IReportDocument report, bool portrait)
    {
        var processor = new ReportProcessor();
        var reportSource = new InstanceReportSource();
        reportSource.ReportDocument = report;
        var deviceInfo = new System.Collections.Hashtable();
        deviceInfo["OutputFormat"] = "PNG";
        deviceInfo["DpiX"] = portrait ? imagePortraitDpi : imageLandscapeDpi;
        deviceInfo["DpiY"] = portrait ? imagePortraitDpi : imageLandscapeDpi;
        string documentName = "";
        this.streams.Clear();

        MemoryStream resultStream;
        try
        {
            var result = processor.RenderReport("IMAGE", reportSource, deviceInfo, new CreateStream(CreateStream), out documentName);
            var width = portrait ? widthPortait : widthLandscape;
            var height = portrait ? heightPortrait : heightLandscape;
            using (resultStream = new MemoryStream())
            {
                using (var resultBitmap = new Bitmap(width, height * streams.Count)) <- here parameter is not valid c#
                {
                    using (var graphics = Graphics.FromImage(resultBitmap))
                    {
                        int offsetY = 0;
                        foreach (var stream in this.streams)
                        {
                            graphics.DrawImage(Image.FromStream(stream), 0, offsetY, width, height);
                            offsetY += height;
                            graphics.Save();
                        }
                    }
                    resultBitmap.Save(resultStream, System.Drawing.Imaging.ImageFormat.Png); <-- here A generic error occurred in GDI+.
                }
            }
        }
        finally
        {
            foreach (var stream in this.streams)
                stream.Close();
        }
        return resultStream.ToArray();
    }

width = 994; height = 703;

when streams.Count = 90 and streams.Sum(p=>p.Length) = 1261524 - no exception thrown and everything works. when streams.Count = 96 and streams.Sum(p=>p.Length) = 1342111 - A generic error occurred in GDI+. when streams.Count = 119 and streams.Sum(p=>p.Length) = 1556359 - parameter is not valid

  • How big do you think the resulting image would be? See https://stackoverflow.com/questions/29175585/what-is-the-maximum-resolution-of-c-sharp-net-bitmap?noredirect=1 – C.Evenhuis Jun 28 '17 at 08:45
  • Sure it is a memory limit, the bitmap requires 332 megabytes. The kind of memory allocation you can only get when your program first starts up, but can't get when it has been running for a while and the address space got fragmented. Project > Properties > Build tab > untick the "Prefer 32-bit" checkbox. You don't prefer it. And do consider whether such a huge bitmap is *really* useful, it gets to be quite hard to look at and hard to print on a piece of paper. – Hans Passant Jun 28 '17 at 08:56

0 Answers0