By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

VB .Net "File in Use" on Loading JPG image into a picture box...

P: n/a
Using VB .Net 2003 Standard Edition.

This simple form has a "File in Use" problem while the executable is running.

Created this simple program to make sure that I hadn't done something in my
code in the "real" project.

In the "real" project, One part of the program saves multiple images and
another part of the program moves the images to the appropriate directories
based on user feedback.

Problem is that once a JPG picture is loaded into a PictureBox I see "File
in Use" Problems with it even after a different JPG picture has been loaded
into that same PictureBox.

Once the program is exited, the files aren't in use!

I can't ask the end-user to frequently exit and restart this program.

This program is a "real time" process that needs to stay running, so
launching a new copy of the program and causing the current version to exit
is not an option either.

ALL the current patches for Windows, Visual Basic .Net 2003, etc have been
applied!

Think that this problem ONLY applies to JPG Pictures, I've tested BMP files
with absolutely NO problems.

For this application, using JPG images is a MUST, so changing to a different
format is out of the question.

Yet if I save the same picture to a new filename that file is not in use.
Used this to save the JPG image from the PictureBox: (Without the quotes!)
"PictureBox.Image.Save(FileName, Imaging.ImageFormat.Jpeg)"

ANY hints on resolving this issue or any valid workarounds?

Bill N.

--- Complete Code for the sample program ---
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents btnExit As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.btnExit = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.PictureBox1.Location = New System.Drawing.Point(4, 4)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(96, 96)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchIma ge
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'btnExit
'
Me.btnExit.Location = New System.Drawing.Point(80, 132)
Me.btnExit.Name = "btnExit"
Me.btnExit.Size = New System.Drawing.Size(108, 56)
Me.btnExit.TabIndex = 1
Me.btnExit.Text = "Exit"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(290, 271)
Me.Controls.Add(Me.btnExit)
Me.Controls.Add(Me.PictureBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click

End

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim Filename As String = _
"C:\MyPictures\MyImage.JPG"

PictureBox1.Image = Image.FromFile(Filename)

End Sub

End Class
---
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 18 Oct 2004 10:59:07 -0700, Bill N. wrote:
Using VB .Net 2003 Standard Edition.

This simple form has a "File in Use" problem while the executable is running.
ANY hints on resolving this issue or any valid workarounds?


Try using a file stream instead:

Dim img as Image
Dim fs as New FileStream(Filename,IO.FileMode.Open)

img = Image.FromStream(fs)

fs.close

PictureBox1.Image = img

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Jul 21 '05 #2

P: n/a
Works much better.

Just needed to add this at the top of the program:
Imports System.IO

Thanks Chris!

:)
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.