Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert Visio file to JPG in CSharp Automatically

Posted on 2009-07-09
5
Medium Priority
?
888 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 31

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 31

Accepted Solution

by:
Scott Helmers earned 1500 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 31

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Book Review: Using Microsoft Visio 2010 by Chris Roth   Disclaimer: The author of Using Visio 2010 is a friend and fellow Visio MVP. Whether you’re new to Visio or are upgrading to Visio 2010 from a previous version, you will find a lot to like…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

721 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