c# Graphics中绘制的内容转化为bitmap,并保存bitmap

解决方案 »

  1.   

    倒过来想:)
    从bitmap得到一个Graphics场境,绘往该Graphics的内容就自然成为bitmap的一部分了。
    using(Graphics g = Graphics.FromImage(bitmap))
    {
       g.Draw...
    }
      

  2.   

    Graphics.FromImage(image);
    ....
    image.Save(...);
      

  3.   

    你好,我知道有在bitmap上创建一个graphics,但是我目的是单向的,就是从一个已经存在的graphics上将其绘制的内容用bitmap保存下来,例如我定义一个Graphics g=this.createGraphics()再将g中绘制的内容用bitmap保存下来;而不是Graphics g = Graphics.FromImage(bitmap)。
      

  4.   

    不能反一下 现在bitmap上创建一个graphics 然后再绘制内容  结果不是一样吗
      

  5.   

    我希望是一个实时变化的图像,例如第一次先画了一条直线,第二次再要画一条直线,若是用bitmap去做,那么第一次你画的直线就被重画了,第一次画的直线就不存在了。而我是希望再第一条直线上继续画第二条直线
      

  6.   

    使用Graphics g=this.createGraphics()的好处就是之前的内容不会被重画,就是这样的一个好处,要不然我会直接使用bitmap去创建一个graphics的,求各位大虾帮助
      

  7.   

            <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
      

  8.   

    谢谢楼上的兄弟,貌似好像对我帮助不大,谢谢哈,希望有人回答出,大家都有分,目前为止我已经写一段截屏的代码将绘制的内容截下来再用bitmap保存。不过我个人觉得有点麻烦,希望有更多的人给出好的意见和方法