Solved

Convert Visio file to JPG in CSharp Automatically

Posted on 2009-07-09
5
868 Views
Last Modified: 2013-12-17
Hello Experts!  I have searched and searched and have yet to find a solution for my problem.

The Scenario:  Pulling Visio diagrams out of database via already existing Csharp code that is part of a Windows service.  I can get the diagram out and written to a temp file.  Now I need to convert it to a jpg programatically.

Using: VS 2008, Visio 2003

I think I should use the Microsoft Interop Drawing Control assembly for Visio to do this; however, in every attempt, I have run into errors from "Class not registered" to must be executed in a singled threaded apartment.  It's installed and registered.  Can run another app on the same machine that uses the Visio ocx on a windows form (that I created) with no problems.  Even tried uninstalling and reinstalling Visio and VS.

I've tried running it in a different thread, creating a console app just to see if I can get it to work, and a host of other things (3 days worth).  I suspect that I am having an issue because I am trying to do this "in the background" with no UI.

Does this make any sense to anyone?  Suggestions for how to achieve my end result?

Thanks in advance!
0
Comment
Question by:mykena
  • 3
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Scott Helmers
ID: 24822443
Let me start by saying that I don't know anything about C#, so I can't specifically help with language constructs, but the root of your problem may lie in the way that you need to create jpg images from Visio. The key is that you can't create a jpg from the Visio drawing -- instead you need to create an image from each page. To say that a different way, it's the export method of the page object that produces images. Consequently, try opening the Visio document then iterate through the pages collection to create your images.

The code below shows how this works in VB.

Let me know whether this does the trick or whether you still need additional help.

Scott

Sub SaveAsJPG()

' save all pages in current document as jpg images using

' page name and page number as file name

 

    Dim PathName As String, jpgName As String

    Dim pg As Page

   

    PathName = ActiveDocument.Path      ' Set pathname to that of current document

            

    For Each pg In ActiveDocument.Pages

        jpgName = PathName & Format(pg.Index, "0#") & " " & pg.Name & ".jpg"

        pg.Export jpgName

    Next

 

End Sub

Open in new window

0
 

Author Comment

by:mykena
ID: 24823028
Scott, thanks again!  I think you answered that portion of my problem a few weeks ago and that worked great.  In fact, the app that I mentioned that does work, is the one I was working on when you answered.  The problem I am now having is in another app which runs as a windows service and not as a GUI.  I think it may be specifically related to .NET and/or the language structure.
0
 
LVL 30

Accepted Solution

by:
Scott Helmers earned 500 total points
ID: 24826732
Sorry -- I didn't notice that you were the same person who asked the "save as jpg" question a few weeks agoI

I don't know any reason why the save as jpg function won't work without the GUI. Could the problem be that you haven't instantiated Visio?

For example, the code below will run in Excel (or any other app that supports VBA). It creates an invisible instance of Visio, opens a document, then saves the pages as images. There is no GUI, per se, because Visio is invisible. (NOTE: you will need to set a reference to the Visio Type Library in the VBA project.)

Let me know whether this does the trick...

Sub SaveAsJPG()

' creates an invisible Visio instance, opens a document, then

' saves all pages in the document as jpg images using

' page name and page number as file name

 

    Dim vsoApp As Visio.Application

    Dim vsoDoc As Visio.Document

    Dim PathName As String, jpgName As String

    Dim pg As Visio.Page

   

    Set vsoApp = CreateObject("Visio.InvisibleApp")

    ' SET PATH/FILENAME BELOW TO VSD ON YOUR SYSTEM

    Set vsoDoc = vsoApp.Documents.Open("c:\Temp\flowchart technique.vsd")

   

    PathName = vsoApp.Documents(1).Path      ' Set pathname to that of first document

            

    For Each pg In vsoApp.ActiveDocument.Pages

        jpgName = PathName & Format(pg.Index, "0#") & " " & pg.Name & ".jpg"

        pg.Export jpgName

    Next

    

    vsoDoc.Close

    vsoApp.Quit

    Set vsoDoc = Nothing

    Set vsoApp = Nothing

 

End Sub

Open in new window

0
 
LVL 30

Expert Comment

by:Scott Helmers
ID: 24868583
mykena,

Did the idea of an invisible Visio app translate into C#? Will this technique work for you?

Scott
0
 

Author Comment

by:mykena
ID: 24880601
Sorry, my project deadline is coming fast so I've been too busy to post.  I was not able to get the invisible  Visio to work as posted, but I did find a way to run a console app that uses the Visio control and never shows it to the user.  Even when I tried that in VS 2008, it did not work, so I built the app in VS 2003 and then converted to VS 2008.  Why this works, I don't know but it did.  I suspect there is some underlying .NET dependency that I may not be aware of.  I'm still pretty new to C#/.NET.
Note:  The errors that I encountered building the app in Vs 2008 always seemed to have something to do with the app thinking the control was not registered.  If you want more details on those errors, let me know.  Visio was always installed on the machine.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The ability to add structure to Visio diagrams using containers, lists and callouts is one of my favorite features in Visio 2010. In this article we’ll examine lists. We’ll explore containers and callouts in separate articles. Prior to reading th…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now