none
RenderTargetBitmap.render gives Insufficient memory exception RRS feed

  • Question

  • Hi,

    I am going to convert chart control into image and save that image on local disk, to convert image I used RenderTargetBitmap and DispatcherTimer

    But RenderTargetBitmap.render gives exception as "Insufficient memory to continue the execution of the program."

    The code I am using as follows

    public static void SaveGraphToImage(Visual target, double dpiX, double dpiY, string Imagepath)
            {
                if (target == null)
                {
                    return;
                }

                Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
                bounds = new Rect(0, 0, dpiX, dpiY);
                
                RenderTargetBitmap rtb = new RenderTargetBitmap((int)(bounds.Width * dpiX / 96.0),
                                                                (int)(bounds.Height * dpiY / 96.0),
                                                                dpiX,
                                                                dpiY,
                                                                PixelFormats.Default);//PixelFormats.Pbgra32
                
                DrawingVisual dv = new DrawingVisual();
                using (DrawingContext ctx = dv.RenderOpen())
                {
                    VisualBrush vb = new VisualBrush(target);
                    ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
                }

                DispatcherTimer timer = new DispatcherTimer();

                timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromSeconds(3);
                timer.Tick += (o, args) =>
                {
                    timer.Stop();
                    rtb.Render(dv);

                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(rtb));

                    if (rtb != null)
                    {
                        rtb.Clear();
                        rtb = null;
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                    }

                    using (FileStream pageOutStream = new FileStream(Imagepath, FileMode.Create, FileAccess.Write))
                    {
                        encoder.Save(pageOutStream);
                        pageOutStream.Close();
                        pageOutStream.Dispose();
                    }
                    //GC.Collect();
                    //GC.WaitForPendingFinalizers();
                };
                timer.Start();

    }

    on the below function its giving error on line rtb.Render(dv); marked as bold.

    So please suggest how can I solve this issue.


    mahajan


    • Edited by udayan Wednesday, March 14, 2012 1:52 PM
    Wednesday, March 14, 2012 1:51 PM

Answers

  • You failed to mention how big the image is, and what the dpi X/Y are that you are hoping to achieve.

    If it's a particularly huge image, RenderTargetBitmap may not be the best way to do this. We resorted to 3rd party application in the end.

    I read somewhere it takes 4 bytes for each pixel in your image, so you can calculate the space needed from that.

    You may be able to get round the problem with DecodePixelWidth & DecodePixelHeight?

      

    Good luck,
    Pete


    #PEJL

    Wednesday, March 14, 2012 3:22 PM
    Moderator

All replies

  • You failed to mention how big the image is, and what the dpi X/Y are that you are hoping to achieve.

    If it's a particularly huge image, RenderTargetBitmap may not be the best way to do this. We resorted to 3rd party application in the end.

    I read somewhere it takes 4 bytes for each pixel in your image, so you can calculate the space needed from that.

    You may be able to get round the problem with DecodePixelWidth & DecodePixelHeight?

      

    Good luck,
    Pete


    #PEJL

    Wednesday, March 14, 2012 3:22 PM
    Moderator
  • any alternative way to do this?
    Friday, November 28, 2014 1:27 PM