Hi,
I'm doing a project to get XPS file; convert it to images and save each images to bytes to mysql database. My problem is like this. After click button "LOAD", system will retrieve and images byte save earlier and filestream to a location (current running directory). After being a file, system will display the image on panel. No problem here.
BUT when I browse for new XPS file to be view on panel(will replace the load from database), it goes exception which saying the images(first images name on directory) is being used by another process. Filename eg: xps_0.bmp
Before I view the selected XPS, I'm dispose all related variable that hold the images and delete the directory but still exception with message as my Question. How to know which variable or control that hold the file? My code as below:- Sorry if it long.
Imports System.IO
Imports MySql.Data.MySqlClient
Imports Microsoft.VisualBasic
Imports System
Imports System.IO.Packaging
Imports System.Windows.Documents
Imports System.Windows.Xps.Packaging
Imports System.Windows.Media.Imaging
Imports System.Collections.Generic
Imports System.Reflection
Imports System.ComponentModel
#Region "INSTANCE"
Dim course_id As Integer
Private locX As Integer = 15
Private locY As Integer = 10
Private sizeWidth As Integer = 100
Private sizeHeight As Integer = 80
Private Folder As DirectoryInfo
Private ThumbImages() As FileInfo
Private ctrl As New PictureBox()
Private SaveVal As Integer
#End Region
#Region "MISC CONTROL IMAGE VIEW"
Private Sub DisposeControl()
pnControls.Controls.Clear()
If Not (IsNothing(Folder)) Then
Folder = Nothing
End If
If Not (IsNothing(ThumbImages)) Then
ThumbImages = Nothing
End If
If Not (IsNothing(arr_xpsfile)) Then
arr_xpsfile = Nothing
End If
If Not IsNothing(ctrl.Image) Then
Dim tmp As Image = ctrl.Image
ctrl.Image = Nothing
tmp.Dispose()
End If
If Not IsNothing(pictureBox1.Image) Then
Dim tmp As Image = pictureBox1.Image
pictureBox1.Image = Nothing
tmp.Dispose()
End If
End Sub
Private Sub StreamByteToImage(ByVal i As Integer, ByVal path As String)
Dim fileContent As Byte() = Nothing
Dim filename As String
If Not (IsNothing(arr_xpsfile)) Then
fileContent = arr_xpsfile.Item(i).arrData
filename = arr_xpsfile.Item(i).strFileName
If Not (IsNothing(fileContent)) Then
Dim oFileStream As System.IO.FileStream
oFileStream = New System.IO.FileStream(path & "\" & filename, System.IO.FileMode.Create)
oFileStream.Write(fileContent, 0, fileContent.Length)
oFileStream.Close()
End If
End If
End Sub
Private Sub ViewThumbnail()
Try
Folder = New DirectoryInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) & "\XPS")
ThumbImages = Folder.GetFiles()
pnControls.Controls.Clear()
Dim locnewX As Integer = locX
Dim locnewY As Integer = locY
For Each img As FileInfo In ThumbImages
If img.Extension.ToLower() = ".png" OrElse img.Extension.ToLower() = ".jpg" OrElse img.Extension.ToLower() = ".gif" OrElse img.Extension.ToLower() = ".jpeg" OrElse img.Extension.ToLower() = ".bmp" OrElse img.Extension.ToLower() = ".tif" Then
If locnewX >= pnControls.Width - sizeWidth - 10 Then
locnewX = locX
locY = locY + sizeHeight + 30
locnewY = locY
Else
locnewY = locY
End If
loadImagestoPanel(img.Name, img.FullName, locnewX, locnewY)
locnewY = locY + sizeHeight + 10
locnewX = locnewX + sizeWidth + 10
End If
Next img
Catch ex As Exception
strInfoMsg = "Oops! Something is wrong with view thumbnail." & vbCrLf & "Ex:- " & ex.Message
MessageBox.Show(strInfoMsg & vbCrLf & "Err: " & ex.Message)
End Try
End Sub
Private Sub loadImagestoPanel(ByVal imageName As String, ByVal ImageFullName As String, ByVal newLocX As Integer, ByVal newLocY As Integer)
Try
ctrl = New PictureBox()
ctrl.Image = Image.FromFile(ImageFullName)
ctrl.BackColor = Color.Black
ctrl.Location = New Point(newLocX, newLocY)
ctrl.Size = New System.Drawing.Size(sizeWidth, sizeHeight)
ctrl.SizeMode = PictureBoxSizeMode.StretchImage
AddHandler ctrl.MouseClick, AddressOf control_MouseClick
pnControls.Controls.Add(ctrl)
Catch ex As Exception
strInfoMsg = "Oops! Something is wrong with load image to panel." & vbCrLf & "Ex:- " & ex.Message
MessageBox.Show(strInfoMsg & vbCrLf & "Err: " & ex.Message)
End Try
End Sub
Private Sub control_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
Try
Dim control As Control = CType(sender, Control)
Dim pic As PictureBox = CType(control, PictureBox)
pictureBox1.Image = pic.Image
Catch ex As Exception
strInfoMsg = "Oops! Something is wrong with view thumbnail." & vbCrLf & "Ex:- " & ex.Message
MessageBox.Show(strInfoMsg & vbCrLf & "Err: " & ex.Message)
End Try
End Sub
Private Sub view_small_Click(sender As System.Object, e As System.EventArgs) Handles view_small.Click
Try
SaveVal = 0
locX = 20
locY = 10
sizeWidth = 100
sizeHeight = 80
For Each p As Control In pnControls.Controls
SaveVal = SaveVal + 1
Next p
If SaveVal > 0 Then
loadControls()
End If
Catch ex As Exception
strInfoMsg = "Oops! Something is wrong with view small thumbnail." & vbCrLf & "Ex:- " & ex.Message
MessageBox.Show(strInfoMsg & vbCrLf & "Err: " & ex.Message)
End Try
End Sub
Private Sub view_medium_Click(sender As System.Object, e As System.EventArgs) Handles view_medium.Click
Try
SaveVal = 0
locX = 20
locY = 10
sizeWidth = 150
sizeHeight = 130
For Each p As Control In pnControls.Controls
SaveVal = SaveVal + 1
Next p
If SaveVal > 0 Then
loadControls()
End If
Catch ex As Exception
strInfoMsg = "Oops! Something is wrong with view medium thumbnail." & vbCrLf & "Ex:- " & ex.Message
MessageBox.Show(strInfoMsg & vbCrLf & "Err: " & ex.Message)
End Try
End Sub
Private Sub view_large_Click(sender As System.Object, e As System.EventArgs) Handles view_large.Click
Try
SaveVal = 0
locX = 20
locY = 10
sizeWidth = 200
sizeHeight = 180
For Each p As Control In pnControls.Controls
SaveVal = SaveVal + 1
Next p
If SaveVal > 0 Then
loadControls()
End If
Catch ex As Exception
strInfoMsg = "Oops! Something is wrong with view large thumbnail." & vbCrLf & "Ex:- " & ex.Message
MessageBox.Show(strInfoMsg & vbCrLf & "Err: " & ex.Message)
End Try
End Sub
Private Sub loadControls()
Try
Dim locnewX As Integer = locX
Dim locnewY As Integer = locY
For Each p As Control In pnControls.Controls
If locnewX >= pnControls.Width - sizeWidth - 10 Then
locnewX = locX
locY = locY + sizeHeight + 30
locnewY = locY
Else
locnewY = locY
End If
p.Location = New Point(locnewX, locnewY)
p.Size = New System.Drawing.Size(sizeWidth, sizeHeight)
locnewY = locY + sizeHeight + 10
locnewX = locnewX + sizeWidth + 10
Next p
Catch ex As Exception
strInfoMsg = "Oops! Something is wrong with load." & vbCrLf & "Ex:- " & ex.Message
MessageBox.Show(strInfoMsg & vbCrLf & "Err: " & ex.Message)
End Try
End Sub
#End Region
#Region "GET XPS IMAGE FROM DATABASE IF EXIST"
Private Sub btn_load_Click(sender As System.Object, e As System.EventArgs) Handles btn_load.Click
Try
DisposeControl()
If (IO.Directory.Exists(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) & "\XPS")) Then
IO.Directory.Delete(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) & "\XPS", True)
End If
IO.Directory.CreateDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) & "\XPS")
If GetImage(lbl_id.Text) Then
Dim imgpath As String
imgpath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) & "\XPS"
For i = 0 To arr_xpsfile.Count - 1
StreamByteToImage(i, imgpath)
Next
ViewThumbnail()
Else
MessageBox.Show("No data yet on this program")
End If
Catch ex As Exception
strInfoMsg = "Oops! Something is wrong with load file." & vbCrLf & "Ex:- " & ex.Message
MessageBox.Show(strInfoMsg & vbCrLf & "Err: " & ex.Message)
End Try
End Sub
#End Region
#Region "GET NEW XPS FILE FROM DIRECTORY"
Private Sub btn_selectxps_Click(sender As System.Object, e As System.EventArgs) Handles btn_selectxps.Click
Dim strFileName = ""
Dim fileDialogBox As New OpenFileDialog()
fileDialogBox.Title = "Select XPS File "
fileDialogBox.Filter = "XPS Viewer (*.xps)|*.xps"
fileDialogBox.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)
If (fileDialogBox.ShowDialog() = DialogResult.OK) Then
txt_xpspath.Text = fileDialogBox.FileName
Try
DisposeControl
If (IO.Directory.Exists(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) & "\XPS")) Then
IO.Directory.Delete(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) & "\XPS", True)
End If
IO.Directory.CreateDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) & "\XPS")
xpsToBmp(txt_xpspath.Text)
ViewThumbnail()
Catch ex As Exception
strInfoMsg = "Oops! Something is wrong with load xps file." & vbCrLf & "Ex:- " & ex.Message
MessageBox.Show(strInfoMsg & vbCrLf & "Err: " & ex.Message)
End Try
End If
End Sub
Public Shared Sub xpsToBmp(ByVal xpsFile As String)
Dim xps As New XpsDocument(xpsFile, System.IO.FileAccess.Read)
Dim sequence As FixedDocumentSequence = xps.GetFixedDocumentSequence()
Try
For pageCount As Integer = 0 To sequence.DocumentPaginator.PageCount - 1
Dim page As DocumentPage = sequence.DocumentPaginator.GetPage(pageCount)
Dim toBitmap As New RenderTargetBitmap(CInt(Fix(page.Size.Width)), CInt(Fix(page.Size.Height)), 96, 96, System.Windows.Media.PixelFormats.Default)
toBitmap.Render(page.Visual)
Dim bmpEncoder As BitmapEncoder = New BmpBitmapEncoder()
bmpEncoder.Frames.Add(BitmapFrame.Create(toBitmap))
Dim fStream As New FileStream(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) & "\XPS\xps_" & pageCount & ".bmp", FileMode.Create, FileAccess.Write)
bmpEncoder.Save(fStream)
fStream.Close()
Next pageCount
Catch ex As Exception
strInfoMsg = "Oops! Something is wrong with comvert xps to bmp." & vbCrLf & "Ex:- " & ex.Message
MessageBox.Show(strInfoMsg & vbCrLf & "Err: " & ex.Message)
End Try
End Sub
#End Region