graphics--> Image

You can create a Graphics object FROM an image. Then use it to draw ON the image. Then you can save the image.
 
You can use the GetHdc method for the Graphics object and then use Graphics.FromImage on the picture then GetHdc for that as well then you can use the BitBlt API to copy Graphics face onto the Bitmap/Image object.
Code:
Private Declare Function BitBlt Lib "gdi32.dll" (ByVal hDestDC As IntPtr, ByVal x As Int32, _
  ByVal y As Int32,  ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal hSrcDC As IntPtr, _
   ByVal xSrc As Int32, ByVal ySrc As Int32, ByVal dwRop As Int32) As Int32
Private Const SRCCOPY As Int32 = &HCC0020

Public Function GetPictureOfGraphics(Byref Draw As Graphics) As Bitmap
You may want to add parameters to make size customizable
Dim Pic As Bitmap = New Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format16bppRgb565)
Dim PicGraphics As Graphics = Graphics.FromImage(Pic) Get a graphics object for the picture
Dim PicDC As IntPtr = PicGraphics.GetHdc() Get a GDI32 Handle for the picture

Dim SrcDC As IntPtr = Draw.GetHdc() Get a GDI32 Handle for the Graphics object

Copy Picture from the provided Graphics Object
BitBlt PicDC, 0, 0, 100, 100, SrcDC, 0, 0, SRCCOPY 

Draw.ReleaseHdc(SrcDC) Release the GDI32 Handles that were used
PicGraphics.ReleaseHdc(PicDC)

Return Pic
End Function
 
Last edited by a moderator:
I did :

Code:
    Private Declare Function BitBlt Lib "gdi32.dll" (ByVal hDestDC As IntPtr, ByVal x As Int32, _
      ByVal y As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal hSrcDC As IntPtr, _
       ByVal xSrc As Int32, ByVal ySrc As Int32, ByVal dwRop As Int32) As Int32
    Private Const SRCCOPY As Int32 = &HCC0020
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim temp As Graphics
        temp = temp.FromImage(Image.FromFile("C:\temp.bmp"))
        CA1.Image = GetPictureOfGraphics(temp)
    End Sub
    Public Function GetPictureOfGraphics(ByRef Draw As Graphics) As Bitmap
        You may want to add parameters to make size customizable
        Dim Pic As Bitmap = New Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format16bppRgb565)
        Dim PicGraphics As Graphics = Graphics.FromImage(Pic) Get a graphics object for the picture
        Dim PicDC As IntPtr = PicGraphics.GetHdc() Get a GDI32 Handle for the picture

        Dim SrcDC As IntPtr = Draw.GetHdc Get a GDI32 Handle for the Graphics object

        Copy Picture from the provided Graphics Object
        BitBlt(PicDC, 0, 0, 100, 100, SrcDC, 0, 0, SRCCOPY)

        Draw.ReleaseHdc(SrcDC) Release the GDI32 Handles that were used
        PicGraphics.ReleaseHdc(PicDC)

        Return Pic
    End Function
 
Back
Top