0

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
Rj Joplin
  • 133
  • 1
  • 2
  • 10
  • The program that generates the report is holding on to the image files. I believe it is something like this, but I am trying to find the applicable VB.NET functions to be able to close the connection. http://stackoverflow.com/questions/13625637/c-sharp-image-from-file-close-connection – Rj Joplin Jul 13 '16 at 16:51

2 Answers2

0

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
Arpit Jain
  • 26
  • 4
  • Was having a problem with this due to the Bitmap("path\filename.jpg") not taking the direct path. This may be because the program that is being used is a streamlined version of ActiveReports and doesn't include all potential uses of the Bitmap(). – Rj Joplin Jul 14 '16 at 14:24
0

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
Rj Joplin
  • 133
  • 1
  • 2
  • 10