倒过来想:) 从bitmap得到一个Graphics场境,绘往该Graphics的内容就自然成为bitmap的一部分了。 using(Graphics g = Graphics.FromImage(bitmap)) { g.Draw... }
Graphics.FromImage(image); .... image.Save(...);
你好,我知道有在bitmap上创建一个graphics,但是我目的是单向的,就是从一个已经存在的graphics上将其绘制的内容用bitmap保存下来,例如我定义一个Graphics g=this.createGraphics()再将g中绘制的内容用bitmap保存下来;而不是Graphics g = Graphics.FromImage(bitmap)。
<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> ''' <res></res> 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
从bitmap得到一个Graphics场境,绘往该Graphics的内容就自然成为bitmap的一部分了。
using(Graphics g = Graphics.FromImage(bitmap))
{
g.Draw...
}
....
image.Save(...);
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>
''' <res></res>
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