Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.

VB
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


VB
#Region "INSTANCE"
    Dim course_id As Integer
    'XPS thumbnail default size
    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

VB
#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

VB
#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

VB
#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
Posted
Comments
Herman<T>.Instance 4-Feb-15 4:44am    
And where the error occurs? After 6 miles of code dump on my screen I am lost. Please be more specific. and show only relevant code. Did you debug your sources?
Luiey Ichigo 5-Feb-15 3:34am    
I'm having error when deleting the directory: IO.Directory.Delete(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) & "\XPS", True)

This error occur IF I load from database and then I select the XPS file. Because both function will goes on same folder. Eg: Assume my debug on C:\Debug. I load the data from database, it will stream the byte to file and I save it in C:\Debug\XPS\x_001.bmp and until last image eg: x_021.bmp..Then let's assume client want to change the display data with new XPS. The select the XPS file and I will convert XPS to Bitmap in the same folder C:\Debug\XPS\. But before I convert the XPS, I will check if the folder XPS exist, I will delete it first. And that's the problem comes. I already dispose the panel,picturebox,image control,arraylist. But still occur.

I already debug the code and hover every variable to see if the value still hold the previous value, and it is nothing.

I hope my explanation can be understand though I'm not good at explaining..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900