Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic CreateImage methid is creating an empty image ! (Read 4617 times)
Homam
Junior Member
**
Offline


I Love MindFusion!

Posts: 67
Joined: Feb 7th, 2012
CreateImage methid is creating an empty image !
Jan 26th, 2013 at 8:16am
Print Post  
diagram.CreateImage(); creating an empty image.
This is my code:

using (MemoryStream ms2 = new MemoryStream(Process.ReadProcessDiagram(ProcessID)))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(ms2);
                diagram.LoadFromXml(doc);
                var diagramImage = diagram.CreateImage();

                CreateImageFile(diagramImage,  "D:\\diagram1.jpg");
            }


private static void CreateImageFile(BitmapSource source, string imageFileName)
        {
            // add the RenderTargetBitmap to a Bitmapencoder
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            var bitmapFrame = BitmapFrame.Create(source);

            encoder.Frames.Add(bitmapFrame);

            using (MemoryStream ms = new MemoryStream())
            {
                encoder.Save(ms);

                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(ms);

                if (WorkflowHelper.IsRightToLeft)
                    bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipY);
                bitmap.Save(imageFileName);
                ms.Close();
            }
        }

Any help please!!
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: CreateImage methid is creating an empty image !
Reply #1 - Jan 27th, 2013 at 8:15am
Print Post  
If the diagram is not shown on screen before exporting, insert a diagram.UpdateLayout() call between the LoadFromXml and CreateImage methods.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Homam
Junior Member
**
Offline


I Love MindFusion!

Posts: 67
Joined: Feb 7th, 2012
Re: CreateImage methid is creating an empty image !
Reply #2 - Jan 27th, 2013 at 1:49pm
Print Post  
I added this line of code and nothing changed. The attached image is the result.
  

a.png ( 127 KB | 275 Downloads )
a.png
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: CreateImage methid is creating an empty image !
Reply #3 - Jan 28th, 2013 at 9:00am
Print Post  
Try resetting the memory stream's current position before creating a bitmap from stream in your CreateImageFile method:

Code
Select All
encoder.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Homam
Junior Member
**
Offline


I Love MindFusion!

Posts: 67
Joined: Feb 7th, 2012
Re: CreateImage methid is creating an empty image !
Reply #4 - Jan 28th, 2013 at 10:14pm
Print Post  
Same result. I tried to save another image in the same way and it worked fine. So, I think the problem is in the CreateImage method. Please try my code or send me a small tested code that creates an image from a diagram (in WPF) and save it as an image file.
Note: I used the same method in the Windows Forms version and it is working there.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: CreateImage methid is creating an empty image !
Reply #5 - Jan 29th, 2013 at 7:48am
Print Post  
If your diagram instance is not part of the WPF visual tree (you are not adding it to application's UI), you will have to run its measure/arrange passes manually before exporting. Otherwise VisualBrushes used when drawing exported images don't paint anything. This works in my test:

Code
Select All
var d = new Diagram();
var n1 = d.Factory.CreateShapeNode(10, 10, 50, 50);
var n2 = d.Factory.CreateShapeNode(110, 110, 50, 50);
d.Factory.CreateDiagramLink(n1, n2);
d.Measure(d.Bounds.Size);
d.Arrange(d.Bounds);
Utilities.SaveImage(d.CreateImage(), "test.png"); 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Homam
Junior Member
**
Offline


I Love MindFusion!

Posts: 67
Joined: Feb 7th, 2012
Re: CreateImage methid is creating an empty image !
Reply #6 - Jan 30th, 2013 at 6:41am
Print Post  
It worked. Thank you Smiley
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint