c# Graphics中绘制的内容转化为bitmap

caixiaoqiting 2011-10-26 12:04:00
c# Graphics中绘制的内容转化为bitmap,并保存bitmap
...全文
1799 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
caixiaoqiting 2011-10-27
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 caijiuxiao 的回复:]
联系我qq806772835@qq.com
[/Quote]
正解,谢谢,现在结贴
江湖我不怕 2011-10-27
  • 打赏
  • 举报
回复
联系我qq806772835@qq.com
黄亮 2011-10-26
  • 打赏
  • 举报
回复
Graphics.FromImage(image);
....
image.Save(...);
gomoku 2011-10-26
  • 打赏
  • 举报
回复
倒过来想:)
从bitmap得到一个Graphics场境,绘往该Graphics的内容就自然成为bitmap的一部分了。
using(Graphics g = Graphics.FromImage(bitmap))
{
g.Draw...
}
xiaoid 2011-10-26
  • 打赏
  • 举报
回复
无语..
我真想删了, 可惜没这个权限.
caixiaoqiting 2011-10-26
  • 打赏
  • 举报
回复
谢谢楼上的兄弟,貌似好像对我帮助不大,谢谢哈,希望有人回答出,大家都有分,目前为止我已经写一段截屏的代码将绘制的内容截下来再用bitmap保存。不过我个人觉得有点麻烦,希望有更多的人给出好的意见和方法
xiaoid 2011-10-26
  • 打赏
  • 举报
回复


<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


caixiaoqiting 2011-10-26
  • 打赏
  • 举报
回复
使用Graphics g=this.createGraphics()的好处就是之前的内容不会被重画,就是这样的一个好处,要不然我会直接使用bitmap去创建一个graphics的,求各位大虾帮助
caixiaoqiting 2011-10-26
  • 打赏
  • 举报
回复
我希望是一个实时变化的图像,例如第一次先画了一条直线,第二次再要画一条直线,若是用bitmap去做,那么第一次你画的直线就被重画了,第一次画的直线就不存在了。而我是希望再第一条直线上继续画第二条直线
hollandjzy 2011-10-26
  • 打赏
  • 举报
回复
不能反一下 现在bitmap上创建一个graphics 然后再绘制内容 结果不是一样吗
caixiaoqiting 2011-10-26
  • 打赏
  • 举报
回复
自己顶顶,,,,
caixiaoqiting 2011-10-26
  • 打赏
  • 举报
回复
你好,我知道有在bitmap上创建一个graphics,但是我目的是单向的,就是从一个已经存在的graphics上将其绘制的内容用bitmap保存下来,例如我定义一个Graphics g=this.createGraphics()再将g中绘制的内容用bitmap保存下来;而不是Graphics g = Graphics.FromImage(bitmap)。

110,570

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧