I threw in the conversion as a little extra You can save the bitmap to the same format if you leave out the format-specification, or use the same format as the original file.
When you are directly pointing the panel's backgroundimage to a file, then you are creating a connection between the two so when you then try to overwrite that file, you get an error because the panel is still using the file as backgroundimage. So you need to make sure that no link can occur - in the example, I use a copy of the bitmap-from-file and make sure that the bitmap-from-file is disposed (=destroyed in memory) before using the copy as the backgroundimage.
I'm assuming you want to paint something onto the panel(and its background) and then use the BitBlt operation to copy the result to file, but you can do that with managed code too (see the example). You could even paint on the bitmap itself instead of using the panel inbetween, but I'll leave the example as close to your original code as possible.
but actualy i use this code to save image files and open it again
Declare Auto Function BitBlt Lib "GDI32.DLL" ( _ ByVal hdcDest As IntPtr, _ ByVal nXDest As Integer, _ ByVal nYDest As Integer, _ ByVal nWidth As Integer, _ ByVal nHeight As Integer, _ ByVal hdcSrc As IntPtr, _ ByVal nXSrc As Integer, _ ByVal nYSrc As Integer, _ ByVal dwRop As Int32) As Boolean
' use this to save
Public Sub save_Pic(ByVal zPanel As Panel, ByVal FilePath As String) ' Create graphics objects to work with Dim gfx1 As Graphics = zPanel.CreateGraphics Dim gfx2 As Graphics = Nothing '
Try
Dim curBmp As New Bitmap(zPanel.Width, zPanel.Height, gfx1)
' Make a copy gfx2 = Graphics.FromImage(curBmp)
' Use BitBlt (Bit Block Transfer) Dim hdc1 As IntPtr = gfx1.GetHdc Dim hdc2 As IntPtr = gfx2.GetHdc BitBlt(hdc2, 0, 0, zPanel.Width, zPanel.Height, hdc1, 0, 0, 13369376)
' Save the bitmap to file Dim fileExists As Boolean If fileExists = My.Computer.FileSystem.FileExists(FilePath) = true Then curBmp.Save(FilePath, System.Drawing.Imaging.ImageFormat.Jpeg) else ' i want OverWrite ' i tryed delete file and save it again but "the file used by another process" message apear
End If
Catch ex As Exception MessageBox.Show(ex.Message.ToString)
End Try End Sub
'use this to open file
Public Sub open_Pic(ByVal zPanel As Panel, ByVal FilePath As String)
Dim fileExists As Boolean If fileExists = My.Computer.FileSystem.FileExists(filepath) = False Then zpanel.BackgroundImage = Image.FromFile(filepath) End If end sub
but i have problem when save the file overwrite the another ...
thanks
save file on another have the same name :"overwrite"
save file on another have the same name :"overwrite"
GeneralSQL
Dim zBitmap As Bitmap = Bitmap.FromFile("c:\folder\test.gif")
zBitmap.Save("c:\folder\test2.png", Imaging.ImageFormat.Png)
sheidee21
KAAU
I'm assuming you want to paint something onto the panel(and its background) and then use the BitBlt operation to copy the result to file, but you can do that with managed code too (see the example). You could even paint on the bitmap itself instead of using the panel inbetween, but I'll leave the example as close to your original code as possible.
Jeff3464
thanks nogchoco ...
it's works
thank u very very very much.....
ellen_89
jaramillo
thanks nogchoco ..
but the problem is :
after open file
i try to delete file but i cannot access the file , because the file is used
PIEBALD
thanks nogchoco ..
but i want to save the file with the same name and the same extention...
or delete the original file and recreate it again.
thanks
Marco Minerva
MSDev23
thanks
but actualy i use this code to save image files and open it again
Declare Auto Function BitBlt Lib "GDI32.DLL" ( _
ByVal hdcDest As IntPtr, _
ByVal nXDest As Integer, _
ByVal nYDest As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hdcSrc As IntPtr, _
ByVal nXSrc As Integer, _
ByVal nYSrc As Integer, _
ByVal dwRop As Int32) As Boolean
' use this to save
Public Sub save_Pic(ByVal zPanel As Panel, ByVal FilePath As String)
' Create graphics objects to work with
Dim gfx1 As Graphics = zPanel.CreateGraphics
Dim gfx2 As Graphics = Nothing '
Try
Dim curBmp As New Bitmap(zPanel.Width, zPanel.Height, gfx1)
' Make a copy
gfx2 = Graphics.FromImage(curBmp)
' Use BitBlt (Bit Block Transfer)
Dim hdc1 As IntPtr = gfx1.GetHdc
Dim hdc2 As IntPtr = gfx2.GetHdc
BitBlt(hdc2, 0, 0, zPanel.Width, zPanel.Height, hdc1, 0, 0, 13369376)
' Release handles
gfx1.ReleaseHdc(hdc1)
gfx2.ReleaseHdc(hdc2)
' Save the bitmap to file
Dim fileExists As Boolean
If fileExists = My.Computer.FileSystem.FileExists(FilePath) = true Then
curBmp.Save(FilePath, System.Drawing.Imaging.ImageFormat.Jpeg)
else
' i want OverWrite
' i tryed delete file and save it again but "the file used by another process" message apear
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub
'use this to open file
Public Sub open_Pic(ByVal zPanel As Panel, ByVal FilePath As String)
Dim fileExists As Boolean
If fileExists = My.Computer.FileSystem.FileExists(filepath) = False Then
zpanel.BackgroundImage = Image.FromFile(filepath)
End If
end sub
but i have problem when save the file overwrite the another ...
thanks