Need to Export diagram to image

Hello I need to export a diagram to image and i can not do it.

I was probe the next code, but the image is blank.

Thanks.

  Rect b = myDiagram.Panel.DiagramBounds;
      var bmp = myDiagram.Panel.MakeBitmap(new Size(b.Width, b.Height), 96, new Point(b.X, b.Y), s);

How big are the DiagramBounds.Width and Height – maybe they are too big?

When are you looking at the image? Is this for Silverlight or for WPF? You may need to use the Action argument to “look” at the image only after rendering has finished.

There are a lot of posts about MakeBitmap in this forum that might be able to help.

I will search this examples.

Thanks.

I probed:

   Rect b = uxdgm_Diagram.Panel.DiagramBounds;
            double w = b.Width;
            double h = b.Height;
            double s = 1.0;
            if (w > 2000) s = 2000 / w;
            if (h > 2000) s = Math.Min(s, 2000 / h);
            w *= s;
            h *= s;
            var image = uxdgm_Diagram.Panel.MakeBitmap(new Size(w, h), 96, new Point(b.X, b.Y), s, bmp =>
            {
                var pos = uxdgm_Diagram.Panel.Position;
                uxdgm_Diagram.Panel.Position = new Point(pos.X, pos.Y + 1);
                uxdgm_Diagram.Panel.Position = pos;
            });
 
            FileStream stream = new FileStream(@"c:\temporal\new.png", FileMode.Create);
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(image));
            encoder.Save(stream);
            stream.Close();

But this does not work.

I get a white image with the correct size.

Size:
w = 1024
h = 2073

You can only look at the contents of the bitmap image only when the Action is called or later. So you need to move that file-writing code into the Action/lambda expression.

I don’t understand.

makebitmap generates a bipmap, i can’t add it before create??

var image = uxdgm_Diagram.Panel.MakeBitmap(new Size(w, h), 96, new Point(b.X, b.Y), s, bmp =>
            {
                var pos = uxdgm_Diagram.Panel.Position;
                uxdgm_Diagram.Panel.Position = new Point(pos.X, pos.Y + 1);
                uxdgm_Diagram.Panel.Position = pos;
 
                FileStream stream = new FileStream(@"c:\temporal\new.png", FileMode.Create);
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(image));
                encoder.Save(stream);
                stream.Close();
            });

Use the “bmp” argument to the Action.

Thank you. It works.

var image = uxdgm_Diagram.Panel.MakeBitmap(new Size(w, h), 96, new Point(b.X, b.Y), s, bmp =>
            {
                var pos = uxdgm_Diagram.Panel.Position;
                uxdgm_Diagram.Panel.Position = new Point(pos.X, pos.Y + 1);
                uxdgm_Diagram.Panel.Position = pos;
 
                FileStream stream = new FileStream(@"c:\temporal\new.png", FileMode.Create);
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bmp));
                encoder.Save(stream);
                stream.Close();
            });
I paste the code if it helps someone in the future

        

Hello. I was probe the code and work in local, but when i excutes on server (deploy xap) i found a exception, any ideas???

I was modified the code, but exist the error:

private void DoExportToImageAction()
        {
            try
            {
                Rect b = uxdgm_Diagram.Panel.DiagramBounds;
                double w = b.Width;
                double h = b.Height;
                double scale = 1.0;
                if (w > 2000) scale = 2000 / w;
                if (h > 2000) scale = Math.Min(scale, 2000 / h);
                w *= scale;
                h *= scale;
                var image = uxdgm_Diagram.Panel.MakeBitmap(new Size(w, h), 96, new Point(b.X, b.Y), scale, bmp =>
                {
                    var pos = uxdgm_Diagram.Panel.Position;
                    uxdgm_Diagram.Panel.Position = new Point(pos.X, pos.Y + 1);
                    uxdgm_Diagram.Panel.Position = pos;
 
                    // obtenemos dialog
                    SaveFileDialog l_dialog = new SaveFileDialog();
                    l_dialog.DefaultExt = cSimulation.kPngFilster;
                    l_dialog.Filter = cSimulation.kPngFilster;
 
                    bool? dialogResult = l_dialog.ShowDialog();
 
                    if (dialogResult == true)
                    {
                        try
                        {
                            using (System.IO.Stream fileStream = l_dialog.OpenFile())
                            {
                                PngBitmapEncoder encoder = new PngBitmapEncoder();
                                encoder.Frames.Add(BitmapFrame.Create(bmp));
                                encoder.Save(fileStream);
                            }
                        }
                        catch (Exception p_ex)
                        { RtcUtils.SendErrorReportToServer(p_ex, cMessageError.cErrorExportingToImage); }
                    }
                });
            } // try
 
            // Registramos el error en el servidor
            catch (Exception pException)
            { RtcUtils.SendErrorReportToServer(pException, cMessageError.cErrorExportingToImage); }
        }
THE ERROR:

EX_MESSAGE: Los cuadros de diálogos deben ser iniciados por el usuario.
EX_STACKTRACE: en System.Windows.Controls.SaveFileDialog.ShowDialogInternal(Window owner)
en SLRTC1201.Views.Editor.b__ab(BitmapSource bmp)
en Northwoods.GoXam.DiagramPanel.#sb.#FK()
en Northwoods.GoXam.DiagramPanel.#sb.#OM()
EX_INNER_EX_MESSAGE:
ERR_CUSTOM_MSG: 195.235.88.11
EX_INNER_EX_STACKTRACE:

Yes, that is a feature of Silverlight security: the user has to invoke a “Save File” dialog directly. I wonder if you need to have the user choose the file beforehand.

I only need leave to choose the path to store the image

Ok, i write this for someone searching help:

You must vinculate this code with event mouse, by example a click event. You must create open dialog… and after use the code above. You can not do work if refactor in another function, the open dialog must execute directly vinculated to mouse event. (Segurity exception of silverlight)