Use of BitBlt Function
If you are drawing a picture with Draw methods in the PictureBox or if you are editing a picture in the PictureBox and you want to save the changes, just use a Button named btnSave, and if you want to save the drawn image or edited image, use a Button named btnsavefile.
Give the name to the pictureBox pic and use the BitBlt function.
Function mypicture() As Bitmap
Then you can convert the Bitmap image to other formats also:
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.IO
Public Declare Function BitBlt Lib "gdi32.dll" Alias "BitBlt" (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 System.Int32) As Long
Public memoryImage As Bitmap
Public pathmemoryImage As Bitmap ' used for bitmap saving
Public myImage As Bitmap
Public Function mypicture() As Bitmap
Dim mygraphics As Graphics = pic.CreateGraphics()
Dim s As Size = pic.Size
myImage = New Bitmap(s.Width, s.Height, mygraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(myImage)
Dim dc1 As IntPtr = mygraphics.GetHdc
Dim dc2 As IntPtr = memoryGraphics.GetHdc
BitBlt(dc2, 0, 0, pic.ClientRectangle.Width, _
pic.ClientRectangle.Height, dc1, 0, 0, 13369376)
mygraphics.ReleaseHdc(dc1)
memoryGraphics.ReleaseHdc(dc2)
Return myImage
End Function
Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
mypicture()
pic.Image = mypicture()
End Sub
Private Sub btnsavefile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsavefile.Click
Dim flname As String
mypicture()
flname = InputBox("enter filename with extension" & vbCr & vbLf & "like .bmp or .jpg or .bmp", "for saving this image ", "mfile.gif", 20, 20)
If Microsoft.VisualBasic.Right(flname, 3) = "bmp" Then
If System.IO.File.Exists("D:/cuttingrangoli/chitra/") = False Then
Try
MkDir("D:/cuttingrangoli")
MkDir("D:/cuttingrangoli/chitra")
Catch ex As Exception
MsgBox("direrectory exists")
End Try
End If
mypicture.Save("D:/cuttingrangoli/chitra" & "/" & flname, System.Drawing.Imaging.ImageFormat.Bmp)
MsgBox("saved in the D:/cuttingrangoli/chitra folder")
End If
If Microsoft.VisualBasic.Right(flname, 3) = "jpg" Then
If System.IO.File.Exists("D:/cuttingrangoli/chitra/") = False Then
Try
MkDir("D:/cuttingrangoli")
MkDir("D:/cuttingrangoli/chitra")
Catch ex As Exception
MsgBox("path exists")
End Try
End If
mypicture.Save("D:/cuttingrangoli/chitra" & "/" & flname, System.Drawing.Imaging.ImageFormat.Jpeg)
MsgBox("saved in the D:/cuttingrangoli/chitra folder")
End If
If Microsoft.VisualBasic.Right(flname, 3) = "gif" Then
If System.IO.File.Exists("D:/cuttingrangoli/chitra/") = False Then
Try
MkDir("D:/cuttingrangoli")
MkDir("D:/cuttingrangoli/chitra")
Catch ex As Exception
MsgBox("path exists")
End Try
End If
mypicture.Save("D:/cuttingrangoli/chitra" & "/" & flname, System.Drawing.Imaging.ImageFormat.Gif)
MsgBox("saved in the D:/cuttingrangoli/chitra folder")
End If
End Sub