Solved

Convert Visio file to JPG in CSharp Automatically

Posted on 2009-07-09
5
876 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Periodically someone asks me whether there’s a way to automatically convert all of the pages in a Visio drawing to PowerPoint slides. There have even been a few times when I’ve wanted to do that myself but I never really had enough incentive to figu…
Meetings to discuss business process can waste time, and often do .  The meeting's dialog can get confusing when participants have different professional perspectives and backgrounds.  A jointly-developed process picture helps wade through the confu…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 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