Problems with FileAccess and PcitoreBox

  • Thread starter Halimaji Nijazi
  • Start date
H

Halimaji Nijazi

Hi

For viewing pictures I am using a pictures box... For making proper saving
of the pic, I must delete an existing pic-file and overwrite it with the
picture in the picturebox... Trying this I always get an exeption, that the
file is in use... This is my code...

'remarks
Imports System.IO
Imports System.Drawing
'

Dim mytempic as String= "X:\Photos\MyTempPic1.jpg"
If System.Io.File.Exists(mytempic) = False Then
Me.pboPicture.Image.Save(mytempPic)
Else
Dim tempPic As String = "X:\Photos\tmp.jpg"
Me.pboPicture.Image.Save(tempPic)
Me.pboPicture.Image = Image.FromFile(tempPic)
'The error occurs here, but I cleared the picturebox
File.Delete(mytemppic)
Me.pboPicture.Image.Save(mytempic)
'other way to load the picture
Me.pboPicture.ImageLocation = mytempic
Me.pboPicture.Load()
End If

end if


If anyone has an idea, please write... I tried everything and didn't get it
work...

Thanks alot

Nijazi Halimaji
 
P

Peter Proost

Hi I always load a temp copy of the original image in a picturebox if I'm
going to do some editing to a picture.
So I don't use Me.pboPicture.Image = Image.FromFile(tempPic), I hope my
sample below is clear enough to do the rest of the explanation

Dim myLoc As String = "c:\2.jpg"
Dim myBmp As Bitmap
Dim myNewBmp As Bitmap
Dim g As Graphics

myBmp = DirectCast(Bitmap.FromFile(myLoc), Bitmap)
myNewBmp = New Bitmap(myBmp.Width, myBmp.Height)
myNewBmp SetResolution(myBmp.HorizontalResolution, myBmp.VerticalResolution)

g = Graphics.FromImage(myNewBmp)
g.DrawImage(myBmp, New Rectangle(0, 0, myBmp.Width, myBmp.Height), 0, 0,
myBmp.Width, myBmp.Height, GraphicsUnit.Pixel)
g.DrawString("Peter Proost vb.net", Me.Font, Brushes.Black, 2, 2)

myBmp.Dispose()
g.Dispose()
PictureBox1.Image = myNewBmp

If System.IO.File.Exists(myLoc) Then
System.IO.File.Delete(myLoc)
End If

myNewBmp.Save(myLoc, Imaging.ImageFormat.Jpeg)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top