110,570
社区成员
发帖
与我相关
我的任务
分享
Graphics.FromImage(image);
....
image.Save(...);
<Runtime.InteropServices.DllImport("gdi32", entrypoint:="BitBlt", ExactSpelling:=True, setlasterror:=True)> _
Private Shared Function BitBlt(ByVal destDC As IntPtr, _
ByVal x As Int32, _
ByVal y As Int32, _
ByVal width As Int32, _
ByVal height As Int32, _
ByVal sourceDC As IntPtr, _
ByVal xsrc As Int32, _
ByVal ysrc As Int32, _
ByVal dwrop As Int32) As Int32
'rop: 复制的模式,vbsrccopy=&HCC0020
End Function
<Runtime.InteropServices.DllImport("gdi32", entrypoint:="CreateCompatibleBitmap", ExactSpelling:=True, setlasterror:=True)> _
Private Shared Function CreateCompatibleBitmap(ByVal hdc As IntPtr, _
ByVal width As Int32, _
ByVal height As Int32) As IntPtr
'以hdc为基础创建一个新的hbitmap对象;
End Function
<Runtime.InteropServices.DllImport("gdi32", entrypoint:="CreateCompatibleDC", ExactSpelling:=True, setlasterror:=True)> _
Private Shared Function CreateCompatibleDC(ByVal hdc As IntPtr) As IntPtr
'以hdc为基础创建一个新的hdc对象;
End Function
<Runtime.InteropServices.DllImport("gdi32", entrypoint:="SelectObject", ExactSpelling:=True, setlasterror:=True)> _
Private Shared Function SelectObject(ByVal hdc As IntPtr, ByVal hbitmap As IntPtr) As IntPtr
'将hbitmap句柄和hdc配对;
End Function
<Runtime.InteropServices.DllImport("gdi32", entrypoint:="DeleteObject", ExactSpelling:=True, setlasterror:=True)> _
Private Shared Function DeleteObject(ByVal hdc As IntPtr) As IntPtr
'将hbitmap句柄和hdc配对;
End Function
''' <summary>
''' 创建 Bitmap 对象;
''' </summary>
''' <param name="g">从 Graphics 创建 Drawing.Bitmap 对象;</param>
''' <returns></returns>
''' <remarks></remarks>
Shared Function CreateBitmap(ByVal g As Drawing.Graphics) As Drawing.Bitmap
Dim _img As Drawing.Bitmap
Dim _imghandle As IntPtr
Dim _imghdc As IntPtr
Dim _width As Int32 = g.VisibleClipBounds.Width
Dim _height As Int32 = g.VisibleClipBounds.Height
'对象g的hdc;
Dim _gdc As IntPtr = g.GetHdc
_imghandle = CreateCompatibleBitmap(_gdc, _width, _height)
_imghdc = CreateCompatibleDC(_gdc)
SelectObject(_imghdc, _imghandle)
Call BitBlt(_imghdc, 0, 0, _width, _height, _gdc, 0, 0, &HCC0020)
g.ReleaseHdc()
_img = Drawing.Bitmap.FromHbitmap(_imghandle)
DeleteObject(_imghdc)
DeleteObject(_imghandle)
Return _img
End Function