Image.Save Method

TBJ

Member
Joined
May 9, 2005
Messages
14
Programming Experience
1-3
I am new at VB.Net and have basically exhausted all options regarding this dumb problem i have.

I've been having issues with the Image.save method, I would like to create a Paint-like application(just for fun and to get back on the VB Horse), and it saves files just fine, IF and only IF I use a different filename(and path) from the one the original file had.

If i use a simple Save method and use the same path and filename I get an exception error and my program freezes up..

I checked the Microsoft site and (surprise) this is what they say....

" GDI+ does not allow you to save an image to the same file that you used to construct the image. "

Is there a way around this dumb issue?

TBJ
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
simply check to see if the file is already there and if it is then delete it
VB.NET:
If File.Exists(strFileName) = True then
File.Delete(strFileName) 'I'm not sure if it's File.Delete or not right off hand
End If
 
Last edited by a moderator:

TBJ

Member
Joined
May 9, 2005
Messages
14
Programming Experience
1-3
Thanks man but tried it and it doesn't work. Remember when an image file is going to be saved it is open and windows does not allow delete on open files, I get that error " file is being used by another program", well back to the drawing board, but thanks anyway
 

aleksjay

New member
Joined
May 10, 2005
Messages
2
Programming Experience
10+
Hi all,
Try saving your new file in a TEMP location (anywhere). Then close and delete your original. Finally, replace your deleted file with the TEMP you just saved.

A.J.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
that would work, but why not just:
VB.NET:
Dim sfdSave As New SaveFileDialog
Dim dlgResult As New DialogResult
With sfdSave
  .Title = "Save Image"
  .Filter = "Bitmap (*.bmp)|*.bmp|Gif (*.gif)|*.gif"
  .DefaultExt = "bmp"
  .FileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Untitled.bmp"
  dlgResult = sfdSave.ShowDialog()
End With
If dlgResult <> DialogResult.Cancel Then
  If File.Exists(sfdSave.FileName) = True Then File.Delete(sfdSave.FileName)
  Image.Save.......
End If
plain and simple
 

MarkD

New member
Joined
May 24, 2005
Messages
2
Programming Experience
5-10
Hi,

The problem your having to due to the stupid fact that .Net is keeping a lock on the file you opened ( I think they mention it in the docs somewhere ).

Try doing this

Dim s1 As System.IO.FileStream
Dim ms As System.IO.MemoryStream
Dim br As System.IO.BinaryReader
s1 = New System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)
ms = New System.IO.MemoryStream(CInt(s1.Length))
br = New System.IO.BinaryReader(s1)
Dim bytesRead As Byte() = br.ReadBytes(CInt(s1.Length))
ms.Write(bytesRead, 0, CInt(s1.Length))
PictureBox1.Image = New System.Drawing.Bitmap(ms)
br.Close()
ms.Close()
s1.Close()

Believe me I banged my head against the wall. Why the hell the file isn't closed after you load it is beyond me but this will effectively create a copy in memory and then close the file.
 
Top Bottom