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
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
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 :
-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 :
Hope this helps you.
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
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.
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.
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 slidesDim 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.TextDim di As New DirectoryInfo(FileFolder) Dim FileList As IList = di.GetFiles() 'open new powerpoint presentation
powerPointApp = GetPowerPointInstance()
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
System.Threading.Thread.Sleep(2000)'select slide copy each chart in each pgn file
SendKeys.Send("^c") 'and paste into powerpoirnt presentation on new slide
slides = powerPointApp.ActivePresentation.Slides.Add(1, PpSlideLayout.ppLayoutBlank).SlideIndex
powerPointApp.ActiveWindow.View.Paste()End If Next End If
powerPointApp.ActivePresentation.SaveAs(FileName:=(FileFolder &"\" & "NewPresentation") & ".ppt", FileFormat:=PpSaveAsFileType.ppSaveAsPresentation, EmbedTrueTypeFonts:=msoFalse) ' Close current powerpoint presentation
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