How do I open .jpg file in external application - vb.net

So what I'm trying to do is open some images in Photoshop on the press of a button. I can get PS to launch without an issue but it just sits there waiting and does not open the file I am requesting (regardless of format (.png .jpg. bmp etc)).
I have added the Photoshop Object Library through the Add Reference Manager, but I keep getting the following error...
System.InvalidCastException: 'Unable to cast COM object of type 'Photoshop.ApplicationClass' to interface type 'Photoshop._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{5DE90358-4D0B-4FA1-BA3E-C91BBA863F32}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'
All I'm wanting to do is open a series of image files from a listBox but I cannot even open an external image file. Any help would be much appreciated.
This is what I am trying...
Dim PSD_App As Photoshop.Application
Dim PSD_Doc As Photoshop.Document
Private Sub OpenInPS_btn_Click(sender As Object, e As EventArgs) Handles OpenInPS_btn.Click
PSD_App = New ApplicationClass
PSD_Doc = PSD_App.Open("E:\TestImageConverter\TestImage.jpg")
End Sub
Thanks for taking the time to look.

Simplest way to open a file in VB with photoshop:
Dim appRef, sampleDoc
Set appRef = CreateObject( "Photoshop.Application" )
' Switch off any dialog boxes
appRef.displayDialogs = 3
' Bring to front
appRef.BringToFront
sampleDoc = "E:/TestImageConverter/TestImage.jpg"
appRef.Open sampleDoc

Related

How to allow access to a folder?

Trying to write text from a text box to a file, keeping any text that is already in there, but this message comes up:
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Access to the path 'C:\Tickets.txt' is denied.
This is for a school assignment to create an IT ticketing software for a school.
What I have done so far:
Private Sub BtnComSubmit_Click(sender As Object, e As EventArgs) Handles BtnComSubmit.Click
My.Computer.FileSystem.WriteAllText("C:\Tickets.txt", TxtComList.Text, True)
End Sub
How could I give the necessary permission(s) to perform this action?
Change this:
My.Computer.FileSystem.WriteAllText("C:\Tickets.txt", TxtComList.Text, True)
To this:
Dim desktopPath As String = Environment.GetFolderPath( Environment.SpecialFolder.Desktop )
Dim filePath As String = System.IO.Path.Combine( desktopPath , "Tickets.txt" )
My.Computer.FileSystem.WriteAllText( filePath , TxtComList.Text, True )
BTW, I recommend avoiding the VB-specific My.Computer... stuff and using the System.IO API so you can graduate over to C# eventually with greater familiarity with .NET's APIs. Very few people stick with VB.NET for long.

ActiveReports not allowing overwrite of jpg after report generation. Windows. VB.NET

Currently, I am utilizing ActiveReports to implement a dynamic image via pathname into a report that is generating.
The Images are being automatically generated as .jpg to a server folder. The Active Reports module imports the files using this code.
Sub ActiveReport_ReportStart
Picture1.Image = System.Drawing.Image.FromFile("path\filename.jpg")
End Sub
The problem I am running into is that this report locks out the jpgs from being overwritten.
I am unsure what could be going wrong, but it seems that the report is still using the image files after the report has been generated.
Am I missing a "disconnect" code piece to ensure that an import doesn't allow for continued contact to the file?
I apologize if this is simple, but I can't find anything for this specific instance.
Thank you.
EDIT:
I attempted to get around the lockout by copying them into their own variable. But this didn't work either.
Sub ActiveReport_ReportStart
dim TempImage as Image = Image.FromFile("path\filename")
Picture1.Image = TempImage
End Sub
You can use "Using" block to make sure that the object of the image is disposed as soon as its usage is completed.
Using statement basically marks a boundary for the objects specified in the statement. So when code block using Using – End Using is exited either after normal execution or some exception cause, the framework invokes the Dispose method of these objects automatically.
Here is the suggested code which can be helpful for you in resolving the issue:
Private Sub SectionReport1_ReportStart(sender As Object, e As EventArgs) Handles MyBase.ReportStart
Dim img As Image
Using bmpTemp = New Bitmap("path\filename.jpg")
img = New Bitmap(bmpTemp)
End Using
Picture1.Image = img
End Sub
I was able to get this to work by creating a function that used the Graphics.FromImage method and disposed the original file.
Public Function GetImageFile(ByVal pathfn As String) As Image
Dim tempImg As Image = Image.FromFile(pathfn)
Dim tempBtm As New Bitmap(Width:=img.Width*CorrectFactor, Height:=img.Height*CorrectFactor, Format:=tempImg.PixelFormat)
Using g As Graphics = Graphics.FromImage(bm)
g.DrawImage(tempImg, Point.Empty)
End Using
tempImg.Dispose()
Return tempBtm
End Function
The item that would be placed in the report would be as follows.
Sub ActiveReport_ReportStart
Picture1.Image = GetImageFile("Path\Filename")
End Sub

An unhandled exception of type 'System.AccessViolationException' occurred vb.net 2013

Imports Microsoft.Office.Interop
Imports System.IO
Imports Access
Public Class Form1
Dim AccessApplication As Microsoft.Office.Interop.Access.Application
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AccessApplication = New Access.Application()
If Not AccessApplication.Visible = True Then
AccessApplication.Visible = True
End If
AccessApplication.OpenCurrentDatabase("C:\POS\POS.lib", False)
End Sub
End Class
I'm trying to open "C:\POS\POS.lib" from vb.net 2013, but the error 'System.AccessViolationException' appear, I tried a lot of methods to fix it but didn't works :
the error appear on the line
AccessApplication.OpenCurrentDatabase("C:\POS\POS.lib", False)
This is the full error :
An unhandled exception of type 'System.AccessViolationException' occurred in PosStartup.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Here is the file logo:
Find the original file extension, and rename the file to have this extension before you open it, and rename it to the have "lib" extension, after you close it. Use System.Diagnostics.Process.Start to start Access in a new process. System knows to open Access, according to file extension:
If System.IO.File.Exists ("C:\POS\POS.lib") And Not System.IO.File.Exists Then ("C:\POS\POS.mdb") ' replace 'mdb' with the correct file extension
System.IO.File.Move("C:\POS\POS.lib", "C:\POS\POS.mdb")' replace 'mdb' with the correct file extension
System.Diagnostics.Process.Start("C:\POS\POS.mdb")' replace 'mdb' with the correct file extension
End If
If you have no need to manege Access from your VB.net app, remove all Office.Interop, and Access Namespaces objects, and use System.Diagnostics.Process.Start to start Access in a new process, and send the file to it as a parameter:
System.Diagnostics.Process.Start ("Access.exe", "C:\POS\POS.lib")
Rename Interop.DLL_Name.dll to DLL_Name.dll and reference it to project. Set Interop true.

How to use RichEditBox in Visual Basic to save the file?

I want to save whatever is being typed in RichEditBox. Below is my example. I was able to successfully with TextBox by using "mytext.Text" but there is no such option that I could see of RichEditBox.
Private Async Function Button_Click(sender As Object, e As RoutedEventArgs) As Task
Dim savePicker As New FileSavePicker
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary
' Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add(".txt", New List(Of String) From {".txt"})
' Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = "New Document"
Dim file As StorageFile = Await savePicker.PickSaveFileAsync
If file IsNot Nothing Then
' Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync
CachedFileManager.DeferUpdates(file)
' Write to file
Await FileIO.WriteTextAsync(file, txtfile.)
' Let Windows know that we are finished changing the file so the other app can update the remote version of the file.
' Completing updates may require windows to ask for user input
Dim status As FileUpdateStatus = Await CachedFileManager.CompleteUpdatesAsync(file)
End If
End Function
Assuming a Windows.UI.Xaml.Controls.RichEditBox, you can retrieve the text with the GetText method of the control's Document property. You can also write the document directly to a stream with the SaveToStream method of the Document property.

Typecast exception from ShellBrowserWindow object to ShellFolderView object

Looking for help trying to figure out why this typecast is not working on my machine.
This code was provided as an answer to another question I had and it's not working for me. It works for the answer poster on their machine, but I'm get a an exception on the line trying to typecast from ShellBrowserWindow to ShellFolderView.
I am using Visual Studio Express 2013, running on Windows 7 Pro X64 Sp1. The target framework for the project is .Net Framework 4. I've added references to Microsoft Internet Controls and Microsoft Shell Controls and Automation and I've added the Imports statements for Shell32 and SHDocVw. DLL versions are as follows: shell32.dll = 6.1.7601.18517 and shdocvw.dll = 6.1.7601.1822 I'm not sure what I could be missing.
The code looks like this. (This code is in a form object)
Imports EdmLib
Imports Shell32
Imports SHDocVw
Public Class BlankForm
Private Sub BlankForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim bar As String() = GetExplorerSelectedFiles()
Exit Sub
'The rest of my program is below this line - I'm just trying to test this one function right now...
End Sub
'Uses the windows shell to get the files selected in explorer
Public Function GetExplorerSelectedFiles() As String()
Dim ExplorerFiles As New List(Of String)
Dim exShell As New Shell32.Shell
Dim SFV As Shell32.ShellFolderView
For Each window As SHDocVw.ShellBrowserWindow In DirectCast(exShell.Windows, SHDocVw.IShellWindows)
If (window.Document).GetType.Name <> "HTMLDocumentClass" Then
SFV = CType(window.Document, ShellFolderView) '<--This is where it fails!!
For Each fi As FolderItem In SFV.SelectedItems
ExplorerFiles.Add(fi.Path)
Next
End If
Next
Return ExplorerFiles.ToArray
End Function
End Class
The line SFV = CType(window.Document, ShellFolderView) results in the following message:
An unhandled exception of type 'System.InvalidCastException' occurred
in LaunchTemplateEPDM.exe
Additional information: Unable to cast COM object of type
'System.__ComObject' to interface type 'Shell32.ShellFolderView'. This
operation failed because the QueryInterface call on the COM component
for the interface with IID '{29EC8E6C-46D3-411F-BAAA-611A6C9CAC66}'
failed due to the following error: No such interface supported
(Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
I've taken a screenshot of a quickwatch on the window object. A quickwatch on the window.document object shows an error saying it's either undefined or inaccessible.
I ran the query Microsoft.VisualBasic.Information.TypeName(window.document) and it returns "IShellFolderViewDual3".
I fixed it.
Not sure why this happens on my system and not yours.
What I found was that GetType.Name always just returns "System.__ComObject", regardless of whether the object is of type ShellFolderView, HTMLDocumentClass or something else. So what was happening was no matter what the actual type of the object was, it was passing the <>"HTMLDocumentClass" test because it was always evaluating to "System.__ComObject". Then when we tried to run the CType function on an object that didn't implement the ShellFolderView interface, it would throw that exception.
I eventually stumbled upon this article which led me to experiment with the TypeName Function which seems to return the actual type, and so I ended up with the working code below:
Public Function GetExplorerSelectedFiles() As String()
Dim ExplorerFiles As New List(Of String)
Dim exShell As New Shell32.Shell
For Each window As SHDocVw.ShellBrowserWindow In DirectCast(exShell.Windows, SHDocVw.IShellWindows)
If TypeName(window.Document) Like "IShellFolderViewDual*" Then
For Each fi As FolderItem In DirectCast(window.Document, ShellFolderView).SelectedItems
ExplorerFiles.Add(fi.Path)
Next
End If
Next
Return ExplorerFiles.ToArray
End Function