Home All Groups Group Topic Archive Search About

Problems with FileAccess and PcitoreBox

Author
6 Feb 2006 8:48 AM
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

--
Nijazi Halimaji
brain solutions
i***@brainsolutions.ch
www.brainsolutions.ch

Author
6 Feb 2006 10:53 AM
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)


--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

Show quoteHide quote
"Halimaji Nijazi" <t***@hotmail.com> schreef in bericht
news:e0Ie9nvKGHA.140@TK2MSFTNGP12.phx.gbl...
> 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
>
> --
> Nijazi Halimaji
> brain solutions
> i***@brainsolutions.ch
> www.brainsolutions.ch
>
>
Author
6 Feb 2006 11:44 AM
Herfried K. Wagner [MVP]
"Halimaji Nijazi" <t***@hotmail.com> schrieb:
> 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...

Check out the two snippets at
<URL:http://dotnet.mvps.org/dotnet/code/graphics/#ImageNoLock>.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>