我一直致力于一个应用程序,其中一部分代码采用EPS文件,使用Ghostscript将.eps文件转换为.jpg,然后将.jpg链接到PictureBox控件。所有这一切看起来都运行良好,但是,我试图让应用程序遍历" temp"存储JPG的文件夹,当用户点击"退出"表单上的按钮。删除文件的代码是:
Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
frmFigureViewer.picMyJPEG.Image = Nothing
'Clear folder contents if folder exists, ON Form Load
If Directory.Exists("C:\Temp\StandaloneRPSTLViewer") Then
For Each FilesToDelete In System.IO.Directory.GetFiles("C:\Temp\StandaloneRPSTLViewer")
System.IO.File.Delete(FilesToDelete)
Next FilesToDelete
End If
Me.Close()
End Sub
然而,在调试时,我得到一个IOException错误:"进程无法访问文件' C:\ Temp \ StandaloneRPSTLViewer \ MXP56962.jpg'因为它正在被另一个进程使用。"
下面我提供了可能存在问题的第二段代码。如果有必要,有人可以指出我正确的方向,我可以自己上传项目文件,如果它会使它更容易。
Private Sub btnDisplay_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplay.Click
frmMain.CurrentFigure = txtFigure.Text
frmMain.MyTMCode = txtMyTMCode.Text
frmFigureViewer.WindowState = FormWindowState.Maximized
frmFigureViewer.Show()
Dim ArrayUpperBound = FGCFigNo.Length
FigureResult = Enumerable.Range(0, FGCFigNo.Length).Where(Function(f) FGCFigNo(f) = frmMain.CurrentFigure).ToArray
frmFigureViewer.lblFGCfromFGCList.Text = FGCCode(FigureResult(0))
frmFigureViewer.lblTOPCurrentFigureTitle.Text = FGCTitle(FigureResult(0))
frmFigureViewer.lblTOPDisplayFigure.Text = FGCFigNo(FigureResult(0))
frmFigureViewer.lblCurrentSheet.Text = FGCSheetNo(FigureResult(0))
frmFigureViewer.lblFinalSheet.Text = FGCSheetNo(FigureResult(FigureResult.Length - 1))
ArtResult = FGCArtID(FigureResult(frmFigureViewer.lblCurrentSheet.Text - 1))
Dim JpgFilePath As String = "C:\Temp\StandaloneRPSTLViewer" + "\" + Mid(ArtResult, 1, ArtResult.Length - 4) + ".jpg"
If File.Exists(JpgFilePath) Then
GoTo OnlyDisplay
End If
Dim SourceFile As String = ""
Dim DestinationFile As String = ""
Dim EpsFilePath As String = ARTFOLDER + "\" + ArtResult
If Not Directory.Exists("C:\Temp\StandaloneRPSTLViewer") Then 'If ArtFile staging folder does not exist, create it
MkDir("C:\Temp\StandaloneRPSTLViewer")
End If
SourceFile = EpsFilePath
DestinationFile = Mid(EpsFilePath, 1, Len(EpsFilePath) - 4) + " view.eps"
FileCopy(SourceFile, DestinationFile)
EpsFilePath = DestinationFile
RunGS("-q", "-dNOPAUSE", "-dBATCH", "-dSAFER", "-sDEVICE=jpeg", _
"-r600", "-dJPEGQ=100", "-dEPSCrop", "-dDownScaleFactor=0", "-dTextAlphaBits=4", "-dGraphicsAlphaBits=4", _
"-sPAPERSIZE=letter", "-sOutputFile=" & JpgFilePath, EpsFilePath)
Kill(EpsFilePath) 'KILLS THE "...view.eps" temporary file in the working folder.
OnlyDisplay:
frmFigureViewer.picMyJPEG.Image = Image.FromFile(JpgFilePath)
Fig036Result = Enumerable.Range(0, FIGURENO.Length).Where(Function(f) FIGURENO(f) = frmMain.CurrentFigure).ToArray 'Build subset array where FigureNo = Contents of Figure text box
'BUILD DICTIONARY HERE'
Dim dict As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim DictionaryFigureNumber As Integer = 1
Dim DictionarySortPosition As Integer = 1
Do Until DictionaryFigureNumber = 1000
Dim DictionaryFigureLetterASCII As Integer = 65
dict.Add(DictionaryFigureNumber, DictionarySortPosition)
DictionarySortPosition = DictionarySortPosition + 1
Do Until DictionaryFigureLetterASCII = 91
dict.Add(DictionaryFigureNumber & Chr(DictionaryFigureLetterASCII), DictionarySortPosition)
DictionarySortPosition = DictionarySortPosition + 1
DictionaryFigureLetterASCII = DictionaryFigureLetterASCII + 1
Loop
DictionaryFigureNumber = DictionaryFigureNumber + 1
Loop
'END OF DICTIONARY BUILDING
Dim TempLength = Fig036Result.GetLength(0)
TempLength = TempLength - 1
Dim CurrentPlace As Integer
CurrentPlace = 0
Dim TempItemNo As String
TempItemNo = ""
Dim ArrayString As String
ArrayString = ""
Array.Clear(FinalDataArray, 0, FinalDataArray.Length)
Array.Clear(FinalSortArray, 0, FinalSortArray.Length)
'Build smaller subset array that contains concatonated strings ONLY for figure listed in textbox.
Do Until CurrentPlace > TempLength
Dim ArrayLocation As Integer = Fig036Result(CurrentPlace)
If txtMyTMCode.Text = TMCODE(ArrayLocation) Then 'If statement to restrict text data in viewer to ONLY TM Code provided in the textbox
TempItemNo = ITEMNO(ArrayLocation)
TempSMR = SMnR(ArrayLocation)
TempNSN = NSN(ArrayLocation)
TempCAGE = CAGE(ArrayLocation)
TempPartNum = PARTNUM(ArrayLocation)
TempDescription = ITEMName(ArrayLocation)
TempUOC = UOC(ArrayLocation)
TempQTY = QTYFIG(ArrayLocation)
TempIndvFGC = FGC(ArrayLocation)
TempPLISN = PLISN(ArrayLocation)
TempNHAPLISN = NHAPLISN(ArrayLocation)
If TMIND(ArrayLocation) = "" Then
TempTMIND = ""
ElseIf TMIND(ArrayLocation) = "1" Then
TempTMIND = "*"
ElseIf TMIND(ArrayLocation) = "2" Then
TempTMIND = "**"
ElseIf TMIND(ArrayLocation) = "3" Then
TempTMIND = "***"
ElseIf TMIND(ArrayLocation) = "4" Then
TempTMIND = "****"
ElseIf TMIND(ArrayLocation) = "5" Then
TempTMIND = "*****"
ElseIf TMIND(ArrayLocation) = "6" Then
TempTMIND = "******"
ElseIf TMIND(ArrayLocation) = "7" Then
TempTMIND = "*******"
ElseIf TMIND(ArrayLocation) = "8" Then
TempTMIND = "********"
ElseIf TMIND(ArrayLocation) = "9" Then
TempTMIND = "*********"
ElseIf TMIND(ArrayLocation) = "10" Then
TempTMIND = "**********"
End If
Dim TempValue As String = ""
dict.TryGetValue(TempItemNo, TempValue)
ArrayString = TempItemNo + "|" + TempSMR + "|" + TempNSN + "|" + TempCAGE + "|" + TempPartNum + "|" + TempDescription + "|" + TempUOC + "|" + TempQTY + "|" + TempIndvFGC + "|" + TempPLISN + "|" + TempNHAPLISN + "|" + TempTMIND + "|"
FinalDataArray(CurrentPlace) = ArrayString
FinalSortArray(CurrentPlace) = TempValue
End If
CurrentPlace = CurrentPlace + 1
Loop
Dim DisplayArray(CurrentPlace - 1) As String
Array.Clear(DisplayArray, 0, DisplayArray.Length)
Dim DisplayArrayCounter As Integer
DisplayArrayCounter = 0
Array.Sort(FinalSortArray, FinalDataArray)
For Each item In FinalDataArray
If Not item Is Nothing Then
DisplayArray(DisplayArrayCounter) = item
DisplayArrayCounter = DisplayArrayCounter + 1
End If
Next
Dim i As Integer
Dim VertLocation As Integer = 5
Dim Mypos As Integer
Dim FillerItemNo As String = ""
Dim FillerSMR As String = ""
Dim FillerNSN As String = ""
Dim FillerCage As String = ""
Dim FillerPartNum As String = ""
Dim FillerDesc As String = ""
Dim FillerUOC As String = ""
Dim FillerQTY As String = ""
Dim FillerFGC As String = ""
Dim FillerPLISN As String = ""
Dim FillerNHAPLISN As String = ""
Dim FillerTMIND As String = ""
Dim FGCConflict As Boolean
'Loop to split up Concatonated Strings, Create Textboxes, and Fill Them
For i = 0 To (DisplayArray.Length - 1)
If DisplayArray(i) = "" Then Exit For
Mypos = InStr(1, DisplayArray(i), "|", CompareMethod.Binary)
If Mypos > 0 Then
FillerItemNo = Mid(DisplayArray(i), 1, Mypos - 1)
DisplayArray(i) = Mid(DisplayArray(i), Mypos + 1, Len(DisplayArray(i)))
Mypos = InStr(1, DisplayArray(i), "|", CompareMethod.Binary)
FillerSMR = Mid(DisplayArray(i), 1, Mypos - 1)
DisplayArray(i) = Mid(DisplayArray(i), Mypos + 1, Len(DisplayArray(i)))
Mypos = InStr(1, DisplayArray(i), "|", CompareMethod.Binary)
FillerNSN = Mid(DisplayArray(i), 1, Mypos - 1)
DisplayArray(i) = Mid(DisplayArray(i), Mypos + 1, Len(DisplayArray(i)))
Mypos = InStr(1, DisplayArray(i), "|", CompareMethod.Binary)
FillerCage = Mid(DisplayArray(i), 1, Mypos - 1)
DisplayArray(i) = Mid(DisplayArray(i), Mypos + 1, Len(DisplayArray(i)))
Mypos = InStr(1, DisplayArray(i), "|", CompareMethod.Binary)
FillerPartNum = Mid(DisplayArray(i), 1, Mypos - 1)
DisplayArray(i) = Mid(DisplayArray(i), Mypos + 1, Len(DisplayArray(i)))
Mypos = InStr(1, DisplayArray(i), "|", CompareMethod.Binary)
FillerDesc = Mid(DisplayArray(i), 1, Mypos - 1)
DisplayArray(i) = Mid(DisplayArray(i), Mypos + 1, Len(DisplayArray(i)))
Mypos = InStr(1, DisplayArray(i), "|", CompareMethod.Binary)
FillerUOC = Mid(DisplayArray(i), 1, Mypos - 1)
DisplayArray(i) = Mid(DisplayArray(i), Mypos + 1, Len(DisplayArray(i)))
Mypos = InStr(1, DisplayArray(i), "|", CompareMethod.Binary)
FillerQTY = Mid(DisplayArray(i), 1, Mypos - 1)
DisplayArray(i) = Mid(DisplayArray(i), Mypos + 1, Len(DisplayArray(i)))
Mypos = InStr(1, DisplayArray(i), "|", CompareMethod.Binary)
FillerFGC = Mid(DisplayArray(i), 1, Mypos - 1)
DisplayArray(i) = Mid(DisplayArray(i), Mypos + 1, Len(DisplayArray(i)))
Mypos = InStr(1, DisplayArray(i), "|", CompareMethod.Binary)
FillerPLISN = Mid(DisplayArray(i), 1, Mypos - 1)
DisplayArray(i) = Mid(DisplayArray(i), Mypos + 1, Len(DisplayArray(i)))
Mypos = InStr(1, DisplayArray(i), "|", CompareMethod.Binary)
FillerNHAPLISN = Mid(DisplayArray(i), 1, Mypos - 1)
DisplayArray(i) = Mid(DisplayArray(i), Mypos + 1, Len(DisplayArray(i)))
Mypos = InStr(1, DisplayArray(i), "|", CompareMethod.Binary)
FillerTMIND = Mid(DisplayArray(i), 1, Mypos - 1)
DisplayArray(i) = Mid(DisplayArray(i), Mypos + 1, Len(DisplayArray(i)))
End If
Dim VertPT As Point = New Point(20, VertLocation)
'ItemNo
Dim MytxtBoxItemNo As TextBox = New TextBox
MytxtBoxItemNo.Name = "txtItemNo" & i + 1
MytxtBoxItemNo.Location = VertPT
MytxtBoxItemNo.Text = FillerItemNo
MytxtBoxItemNo.Visible = True
MytxtBoxItemNo.Height = 20
MytxtBoxItemNo.Width = 30
MytxtBoxItemNo.ScrollBars = ScrollBars.Horizontal
'SMRCode
VertPT = New Point(80, VertLocation)
Dim MytxtboxSMR As TextBox = New TextBox
MytxtboxSMR.Name = "txtSMR" & i + 1
MytxtboxSMR.Location = VertPT
MytxtboxSMR.Text = FillerSMR
MytxtboxSMR.Visible = True
MytxtboxSMR.Height = 20
MytxtboxSMR.Width = 50
MytxtboxSMR.ScrollBars = ScrollBars.Horizontal
'NSN
VertPT = New Point(170, VertLocation)
Dim MytxtboxNSN As TextBox = New TextBox
MytxtboxNSN.Name = "txtNSN" & i + 1
MytxtboxNSN.Location = VertPT
If FillerNSN = "" Then
FillerNSN = "NO NSN"
MytxtboxNSN.ForeColor = System.Drawing.Color.Red
End If
MytxtboxNSN.Text = FillerNSN
MytxtboxNSN.Visible = True
MytxtboxNSN.Height = 20
MytxtboxNSN.Width = 90
MytxtboxNSN.ScrollBars = ScrollBars.Horizontal
'CAGE
VertPT = New Point(290, VertLocation)
Dim MytxtboxCAGE As TextBox = New TextBox
MytxtboxCAGE.Name = "txtCage" & i + 1
MytxtboxCAGE.Location = VertPT
MytxtboxCAGE.Text = FillerCage
MytxtboxCAGE.Visible = True
MytxtboxCAGE.Height = 20
MytxtboxCAGE.Width = 50
MytxtboxCAGE.ScrollBars = ScrollBars.Horizontal
'PART NUMBER
VertPT = New Point(360, VertLocation)
Dim MytxtboxPartNum As TextBox = New TextBox
MytxtboxPartNum.Name = "txtPartNum" & i + 1
MytxtboxPartNum.Location = VertPT
MytxtboxPartNum.Text = FillerPartNum
MytxtboxPartNum.Visible = True
MytxtboxPartNum.Height = 20
MytxtboxPartNum.Width = 70
MytxtboxPartNum.ScrollBars = ScrollBars.Horizontal
'TM IND
VertPT = New Point(455, VertLocation)
Dim MytxtboxTMIND As TextBox = New TextBox
MytxtboxTMIND.Name = "txtTMIND" & i + 1
MytxtboxTMIND.Location = VertPT
MytxtboxTMIND.Text = FillerTMIND
MytxtboxTMIND.AutoSize = False
MytxtboxTMIND.Font = New Font(Me.Font.FontFamily, 20, FontStyle.Bold, GraphicsUnit.Pixel)
MytxtboxTMIND.Visible = True
MytxtboxTMIND.Height = 20
MytxtboxTMIND.Width = 80
MytxtboxTMIND.ScrollBars = ScrollBars.Horizontal
'Description
VertPT = New Point(554, VertLocation)
Dim MytxtboxDesc As TextBox = New TextBox
MytxtboxDesc.Name = "txtDesc" & i + 1
MytxtboxDesc.Location = VertPT
MytxtboxDesc.Text = FillerDesc
MytxtboxDesc.Visible = True
MytxtboxDesc.Height = 20
MytxtboxDesc.Width = 160
MytxtboxDesc.ScrollBars = ScrollBars.Horizontal
'UOC
VertPT = New Point(740, VertLocation)
Dim MytxtboxUOC As TextBox = New TextBox
MytxtboxUOC.Name = "txtUOC" & i + 1
MytxtboxUOC.Location = VertPT
MytxtboxUOC.Text = FillerUOC
MytxtboxUOC.Visible = True
MytxtboxUOC.Height = 20
MytxtboxUOC.Width = 100
MytxtboxUOC.ScrollBars = ScrollBars.Horizontal
'QTY
VertPT = New Point(870, VertLocation)
Dim MytxtboxQTY As TextBox = New TextBox
MytxtboxQTY.Name = "txtQTY" & i + 1
MytxtboxQTY.Location = VertPT
MytxtboxQTY.Text = FillerQTY
MytxtboxQTY.Visible = True
MytxtboxQTY.Height = 20
MytxtboxQTY.Width = 40
MytxtboxQTY.ScrollBars = ScrollBars.Horizontal
'Item's individual FGC
VertPT = New Point(940, VertLocation)
Dim MytxtboxFGC As TextBox = New TextBox
MytxtboxFGC.Name = "txtFGC" & i + 1
MytxtboxFGC.Location = VertPT
Dim FirstFourIndvFGC As String = Mid(FillerFGC, 1, 4)
Dim FirstFourFGCList As String = frmFigureViewer.lblFGCfromFGCList.Text
If Not FirstFourIndvFGC = FirstFourFGCList Then
MytxtboxFGC.ForeColor = System.Drawing.Color.Red
FGCConflict = True
Else
MytxtboxFGC.ForeColor = System.Drawing.Color.Green
End If
MytxtboxFGC.Text = FillerFGC
MytxtboxFGC.Visible = True
MytxtboxFGC.Height = 20
MytxtboxFGC.Width = 50
MytxtboxFGC.ScrollBars = ScrollBars.Horizontal
'PLISN
VertPT = New Point(1050, VertLocation)
Dim MytxtboxPLISN As TextBox = New TextBox
MytxtboxPLISN.Name = "txtPLISN" & i + 1
MytxtboxPLISN.Location = VertPT
MytxtboxPLISN.Text = FillerPLISN
MytxtboxPLISN.Visible = True
MytxtboxPLISN.Height = 20
MytxtboxPLISN.Width = 50
MytxtboxPLISN.ScrollBars = ScrollBars.Horizontal
'NHA PLISN
VertPT = New Point(1150, VertLocation)
Dim MytxtboxNHAPLISN As TextBox = New TextBox
MytxtboxNHAPLISN.Name = "txtNHAPLISN" & i + 1
MytxtboxNHAPLISN.Location = VertPT
MytxtboxNHAPLISN.Text = FillerNHAPLISN
MytxtboxNHAPLISN.Visible = True
MytxtboxNHAPLISN.Height = 20
MytxtboxNHAPLISN.Width = 50
MytxtboxNHAPLISN.ScrollBars = ScrollBars.Horizontal
'Add the controls and iterate VertLocation prior to starting the next RPSTL item
frmFigureViewer.p036text.Controls.Add(MytxtBoxItemNo)
frmFigureViewer.p036text.Controls.Add(MytxtboxNSN)
frmFigureViewer.p036text.Controls.Add(MytxtboxCAGE)
frmFigureViewer.p036text.Controls.Add(MytxtboxTMIND)
frmFigureViewer.p036text.Controls.Add(MytxtboxDesc)
frmFigureViewer.p036text.Controls.Add(MytxtboxFGC)
frmFigureViewer.p036text.Controls.Add(MytxtboxQTY)
frmFigureViewer.p036text.Controls.Add(MytxtboxPartNum)
frmFigureViewer.p036text.Controls.Add(MytxtboxUOC)
frmFigureViewer.p036text.Controls.Add(MytxtboxSMR)
frmFigureViewer.p036text.Controls.Add(MytxtboxPLISN)
frmFigureViewer.p036text.Controls.Add(MytxtboxNHAPLISN)
VertLocation = VertLocation + 30
Next
If FGCConflict = True Then
If chkFGCConflict.Checked = True Then
MsgBox("There is an FGC conflict within this figure. The ITEM FGC has been colored red for your convenience")
End If
End If
JpgFilePath = ""
End Sub
更新:RunGS子例程的代码是:
Option Explicit On
Imports System.Runtime.InteropServices
'--- Simple VB.Net wrapper for Ghostscript gsdll32.dll
' (Tested using Visual Studio 2010 and Ghostscript 9.06)
Module GhostscriptDllLib
Private Declare Function gsapi_new_instance Lib "gsdll32.dll" (ByRef instance As IntPtr, ByVal caller_handle As IntPtr) As Integer
Private Declare Function gsapi_set_stdio Lib "gsdll32.dll" (ByVal instance As IntPtr, ByVal gsdll_stdin As StdIOCallBack, ByVal gsdll_stdout As StdIOCallBack, ByVal gsdll_stderr As StdIOCallBack) As Integer
Private Declare Function gsapi_init_with_args Lib "gsdll32.dll" (ByVal instance As IntPtr, ByVal argc As Integer, <MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.LPStr)> ByVal argv() As String) As Integer
Private Declare Function gsapi_exit Lib "gsdll32.dll" (ByVal instance As IntPtr) As Integer
Private Declare Sub gsapi_delete_instance Lib "gsdll32.dll" (ByVal instance As IntPtr)
'--- Run Ghostscript with specified arguments
Public Function RunGS(ByVal ParamArray Args() As String) As Boolean
Dim InstanceHndl As IntPtr
Dim NumArgs As Integer
Dim StdErrCallback As StdIOCallBack
Dim StdInCallback As StdIOCallBack
Dim StdOutCallback As StdIOCallBack
NumArgs = Args.Count
StdInCallback = AddressOf InOutErrCallBack
StdOutCallback = AddressOf InOutErrCallBack
StdErrCallback = AddressOf InOutErrCallBack
'--- Shift arguments to begin at index 1 (Ghostscript requirement)
ReDim Preserve Args(NumArgs)
System.Array.Copy(Args, 0, Args, 1, NumArgs)
'--- Start a new Ghostscript instance
If gsapi_new_instance(InstanceHndl, 0) <> 0 Then
Return False
Exit Function
End If
'--- Set up dummy callbacks
gsapi_set_stdio(InstanceHndl, StdInCallback, StdOutCallback, StdErrCallback)
'--- Run Ghostscript using specified arguments
gsapi_init_with_args(InstanceHndl, NumArgs + 1, Args)
'--- Exit Ghostscript
gsapi_exit(InstanceHndl)
'--- Delete instance
gsapi_delete_instance(InstanceHndl)
Return True
End Function
'--- Delegate function for callbacks
Private Delegate Function StdIOCallBack(ByVal handle As IntPtr, _
ByVal Strz As IntPtr, ByVal Bytes As Integer) As Integer
'--- Dummy callback for standard input, standard output, and errors
Private Function InOutErrCallBack(ByVal handle As IntPtr, _
ByVal Strz As IntPtr, ByVal Bytes As Integer) As Integer
Return 0
End Function
End Module
答案 0 :(得分:0)
所以我做了一些进一步的挖掘,并且能够得出结论,问题不在于Ghostscript。我的应用程序中的PictureBox仍然将.jpg文件保存在内存中。奇怪的是,我假设将PictureBox.src设置为“Nothing”会释放文件,但这不起作用。在进行了各种谷歌搜索之后,我能够在这里找到解决方法:https://support.microsoft.com/en-us/kb/309482
使用图片文件加载PictureBox控件时,Microsoft Visual Studio .NET或Microsoft Visual Studio 2005集成开发环境(IDE)会保留对文件的锁定。当您在设计时手动将PictureBox控件的Image属性设置为文件,或者在运行时使用FromFile方法时,会发生这种情况。
RESOLUTION(无论如何我的申请):
Dim fs As System.IO.FileStream
' Specify a valid picture file path on your computer.
fs = New System.IO.FileStream(JpgFilePath,
IO.FileMode.Open, IO.FileAccess.Read)
frmFigureViewer.picMyJPEG.Image = System.Drawing.Image.FromStream(fs)
fs.Close()