none
Opening png files from VB

    Question

  • Hi

     

    I have 2 problems that I hope someone can help me with. I’m quite new to VB so I’m sorry if I’m asking a stupid question but I’m really stuck.

     

    1) I am trying to open a png file from VB.

     

    I am using the following code to open the file but it isn’t working, I’m not getting an error but nothing is happening.

     

    Dim FileFolder As String

    Dim fi As FileInfo

     

    FileFolder = txtExportPath.Text

     

    Dim di As New DirectoryInfo(FileFolder)

    Dim FileList As IList = di.GetFiles()

     

     

    For Each fi In FileList

     

    If fi.Name.EndsWith("png") Then

     

              fi.Open(FileMode.Open)

     

     

    2) I then need to copy the picture from the file.

     

    Once the file is open I need to copy the picture but can’t find any help on how to do this.

     

    If anyone can help me that would be GREAT THANKS

     

    Tuesday, December 05, 2006 5:15 PM

All replies

  • Actually, I don't understand what do you want exactly, so there are two cases :
    -If you want to open a PNG file with the default program, use :
    Process.Start(FilePath)
    -If you want to open a PNG file to a picturebox :
    PictureBox1.Image = New Drawing.Bitmap(FilePath)
    For the second question, do you want to copy the picture to the clipboard?, if yes write :
    My.Computer.Clipboard.SetImage(PictureBox1.Image)

    Hope this helps you.
    Tuesday, December 05, 2006 5:43 PM
    Moderator
  • As your description of what you are trying to achieve is rather weak, I'll take a stab at what your trying to achieve and say that you want to select a PNG file and display its contents.  The following will display a dialog allowing you to select a file which you want to display and then displaying it in a picturebox control on the form. 

    This enables you to see all the files, individually select one and then see its contents.

    Public Class Form1
      
            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim x As New OpenFileDialog
            x.Title = "Open Graphic Files"
            x.Filter = "PNG Files|*.png|BMP Files|*.bmp"
            If x.ShowDialog = Windows.Forms.DialogResult.OK Then
                PictureBox1.Load(x.FileName)
            End If
        End Sub
    End Class

     

     

    Tuesday, December 05, 2006 6:51 PM
  • Hi

     

    Sorry about my bad description I’ll try again.

     

    What I have is a dialogue box which allows the users to select a windows folder in which a number of png files reside.

    That file path is then saved as a variable and when the user selects a “create presentation” button the program must do the following.

     

    Loop thought each file in the folder, if the file is a png file then it must open the file.

    Currently the files open in Microsoft Photo Editor when double clicked (but this can be changed if necessary).

    The picture in the file (which in this case is always a chart) must then be selected and pasted into PowerPoint.

     

    The first part I have a problem with is opening the png file. The second is selecting the chart within it.

     

    I’ve done this from Excel to PowerPoint with no problems because it was easy to find the syntax to open and copy a chart from excel. But I can’t seem to get the same thing working with the png files because I can’t open them.

     

    I hope this makes things a little clearer. Thanks for your suggestions I’ll give them a go.

     

    Cheers

    Wednesday, December 06, 2006 11:42 AM
  • Thanks for the help.

    It's sorted now unfortunately I had to add some dodgy cowboy sendkeys but it's the only way I could get the copy and paste to work.

    Cheers

    Private Sub btnPresentation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click, btnPresentation.Click

    'This code creates a powerpoint presentation from the png slides

    Dim slides As Integer

    Dim FileFolder As String

    Dim fi As FileInfo

    Dim ppPresentation As String

    Dim powerPointApp As PowerPoint.Application

    Dim FilePath As String

    'Change form tex

    Me.sslFsrChartConverter.Text = "Creating Presentation ..."

    Me.Refresh()

    'Disable the button

    btnPresentation.Enabled = False

    'Get folder path of folder holding png files

    FileFolder = txtExportPath.Text

    Dim di As New DirectoryInfo(FileFolder)

    Dim FileList As IList = di.GetFiles()

    'open new powerpoint presentation

    powerPointApp = GetPowerPointInstance()

    powerPointApp.Visible = True

    ppPresentation = "NewPresentation.ppt"

    powerPointApp.NewPresentation.Add(ppPresentation)

    powerPointApp.Presentations.Add(WithWindow:=Microsoft.Office.Core.MsoTriState.msoTrue)

    'loop throguh all png files in specified directorey

    If Not FileList Is Nothing Then

    For Each fi In FileList

    If fi.Name.EndsWith("png") Then

    FilePath = FileFolder & "\" & fi.Name

    'open file

    Process.Start(FilePath)

    System.Threading.Thread.Sleep(2000)

    'select slide copy each chart in each pgn file

    SendKeys.Send("^a")

    SendKeys.Send("^c")

    'and paste into powerpoirnt presentation on new slide

    slides = powerPointApp.ActivePresentation.Slides.Add(1, PpSlideLayout.ppLayoutBlank).SlideIndex

    powerPointApp.ActiveWindow.View.GotoSlide(slides)

    powerPointApp.ActiveWindow.View.Paste()

    End If

    Next

    End If

     

    'save the presentation

    powerPointApp.ActivePresentation.SaveAs(FileName:=(FileFolder & "\" & "NewPresentation") & ".ppt", FileFormat:=PpSaveAsFileType.ppSaveAsPresentation, EmbedTrueTypeFonts:=msoFalse)

    ' Close current powerpoint presentation

    powerPointApp.ActivePresentation.Close()

    powerPointApp.Quit()

    Clipboard.Clear()

    're-enable the button

    btnPresentation.Enabled = True

    'Clear up the module variables

    powerPointApp = Nothing

    'Change form heading

    Me.sslFsrChartConverter.Text = "Presentation Created"

    End Sub

    Thursday, December 07, 2006 11:36 AM