TiffBitmapDecoder Release

MissingLink

Member
Joined
Feb 20, 2009
Messages
7
Programming Experience
3-5
Hi there

I have a system that scans in images to multi page tiff files and allows the user to preview before finalising. When the user previews, they click on the page number which should then use the TiffBitmapDecoder to read that particular frame in from the Tiff file and display the BitmapSource in an Image control...this works just fine. The problem comes in when they scan another page which is then inserted into the tiff file. The tiff file is returning that it cannot be accessed as another process is using it. How do I get the TiffBitmapDecoder to release the Tiff file?

Code to generate preview:

VB.NET:
Public ReadOnly Property GetPagePreview(ByVal intPage As Integer) As BitmapSource
        Get

            Dim bitmapSource As BitmapSource

            Try
                Dim p As Integer = (mCurrentDocumentIndex + 1)

                Dim path As New Uri("temp\" & p.ToString & "\" & p.ToString & ".tiff", UriKind.Relative)

                Dim decoder As New TiffBitmapDecoder(path, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)

                bitmapSource = decoder.Frames(intPage)

            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try

            Return bitmapSource

        End Get
    End Property

Code to insert page into tiff:

VB.NET:
Dim strDoc As String = "temp\" & (mCurrentDocumentIndex + 1).ToString & "\" & _
                                                    (mCurrentDocumentIndex + 1).ToString

                aFinishScanParts.ImageBox.InsertTIF(strDoc & ".tiff", strDoc & ".tiff", aFinishScanParts.IntPageIndex + 1)
 
Another Process Accessing File

Hi all

I am urgently in need of some help here...pulling my hair out by the second.
I've been stuck on this for 3 days now.

I have a TiffBitmapDecoder accessing a file and generating a BitmapSource from one of the frames in the file. If I try and modify the file after that e.g. insert new frame or even delete file, I'm informed that the file cannot be accessed as another process is accessing the file.

How do I either a) Get the TiffBitmapDecoder to release the file or B) force all processes that have access to a file to release?

Note: I'm using WPF and the TiffBitmapDecoder does not have standard fileStream methods like close, dispose or flush.

Thanks in advance
 
Public Shared Function ConvertTiff(ByVal Path As String, ByVal HawbNum As String, ByVal DocType As String)
Try
Dim bmpLst As List(Of System.Drawing.Bitmap) = New List(Of System.Drawing.Bitmap)()
Dim st As Stream

Using inFile As New System.IO.FileStream(Path, IO.FileMode.Open, IO.FileAccess.Read) 'Image.FromFile(Path)
Dim decoder As TiffBitmapDecoder = New TiffBitmapDecoder(inFile, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.[Default])
Dim totFrames As Integer = decoder.Frames.Count

Dim i = 0

While i < totFrames
' Create bitmap to hold the single frame
Dim bmpSingleFrame As Drawing.Bitmap = BitmapFromSource(decoder.Frames(i))
' add the frame (as a bitmap) to the bitmap list
bmpLst.Add(bmpSingleFrame)
Interlocked.Increment(i)
End While

End Using
Dim x As Integer = 0
Dim tifPaths As String() = New String(bmpLst.Count - 1) {}
For Each item As Bitmap In bmpLst
Using bmp As Bitmap = New Bitmap(item)

tifPaths(x) = String.Format("{0}\{1}{2}.TIF", "path", HawbNum & "_" & DocType & "_" & x, item).Replace("System.Drawing.Bitmap", "")

If (System.IO.File.Exists(tifPaths(x))) Then
Else
bmp.Save(tifPaths(x), System.Drawing.Imaging.ImageFormat.Tiff)
End If


'EventLog.WriteEntry("Reporting:CCBillingCommon", "After Save In ConvertTiff Method: " + jpegPaths(x), EventLogEntryType.Warning)

Interlocked.Increment(x)
End Using
Next

Return tifPaths

Catch ex As Exception

Finally

End Try
End Function

Public Shared Function BitmapFromSource(ByVal bitmapsource As BitmapSource) As Bitmap
Dim bitmap As Bitmap

Try
Using outStream = New MemoryStream()
Dim enc As BitmapEncoder = New BmpBitmapEncoder()
enc.Frames.Add(BitmapFrame.Create(bitmapsource))
enc.Save(outStream)
bitmap = New Bitmap(outStream)
End Using
Return bitmap
Catch ex As Exception

Finally

End Try

End Function
 
Back
Top