【GDI】在内存中绘图,保存为BMP,或者JPG

是是非非 2008-05-02 11:18:55
大概思路是知道的
CreateDCByNum创建一个DC
然后 CreateDIBitmap

但是有一些细节还是不清楚……

比如,如何设置颜色数量,宽度,高度,是否需要调色板(我只用16位色,应该不用调色板吧?)

保存为JPG格式有没有简便一点的方法

有做过的能否给我看一点源码……
...全文
453 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
silencenet 2008-05-09
  • 打赏
  • 举报
回复

Dim lDc as Long,lBmp as Long
lDc = CreateCompatibleDC(UserControl.hdc) '创建兼容内存DC。。这里填0创建与屏幕Dc相兼容的内存Dc
lBmp = CreateCompatibleBitmap(lDc, Width,Height) '创建一幅自定义大小的位图。。
SelectObject lDc, lBmp '将刚刚创建的位图选中内存Dc。。然后你就可以在这个lDc中绘图了。。

'前面代码已经说明了。。。。可以自己创建自定义大小位图..不需要现有图片.这也也可以叫“无中生有”吧。。
'而这。。。。绘图好像还是要到内存Dc中绘制。。
'我不知道除了内存Dc可以绘图外。。还可以怎么用内存绘图。。
'另一个是好像是创建与设备无关的位图。。那个可能更符合你的要求。。我没试过。。楼下的回答吧。。
'不过。。。如果没有窗口Dc。。那保存位图好像有难点。。 我找了很久没见过有API保存位图的。。 :(。。我都想要呢。。
yinweihong 2008-05-09
  • 打赏
  • 举报
回复
这个代码看看,不知道是否符合你的要求
Option Explicit

Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function MoveToEx Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, lpPoint As Long) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Const SRCCOPY = &HCC0020
Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Private Const BLACK_PEN = 7
Private Const WHITE_BRUSH = 0
Private Const NULL_BRUSH = 5
Private Declare Function Rectangle Lib "gdi32" (ByVal hDC As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
' **********
Private Type MemoryBitmap
hDC As Long
hBM As Long
oldhDC As Long
wid As Long
hgt As Long
End Type
' Delete the bitmap and free its resources.
Private Sub DeleteMemoryBitmap(memory_bitmap As MemoryBitmap)
SelectObject memory_bitmap.hDC, memory_bitmap.oldhDC
DeleteObject memory_bitmap.hBM
DeleteDC memory_bitmap.hDC
End Sub

' Draw on the memory bitmap.
Private Sub DrawOnMemoryBitmap(memory_bitmap As MemoryBitmap)
Dim wid As Long
Dim hgt As Long

wid = memory_bitmap.wid
hgt = memory_bitmap.hgt

' Give the device context a white background.
SelectObject memory_bitmap.hDC, GetStockObject(WHITE_BRUSH)
Rectangle memory_bitmap.hDC, 0, 0, wid, hgt
SelectObject memory_bitmap.hDC, GetStockObject(NULL_BRUSH)

' Draw the on the device context.
SelectObject memory_bitmap.hDC, GetStockObject(BLACK_PEN)
MoveToEx memory_bitmap.hDC, 0, 0, ByVal 0&
LineTo memory_bitmap.hDC, wid, hgt
MoveToEx memory_bitmap.hDC, 0, hgt, ByVal 0&
LineTo memory_bitmap.hDC, wid, 0
End Sub
' Make a memory bitmap of the given size.
' Return the bitmap's DC.
Private Function MakeMemoryBitmap(ByVal wid As Long, ByVal hgt As Long) As MemoryBitmap
Dim result As MemoryBitmap

' Create the device context.
result.hDC = CreateCompatibleDC(0)

' Create the bitmap.
result.hBM = CreateCompatibleBitmap( _
result.hDC, wid, hgt)

' Make the device context use the bitmap.
result.oldhDC = SelectObject(result.hDC, result.hBM)

' Return the MemoryBitmap structure.
result.wid = wid
result.hgt = hgt
MakeMemoryBitmap = result
End Function
Private Sub Form_Load()
Dim memory_bitmap As MemoryBitmap

' Create the memory bitmap.
Picture1.ScaleMode = vbPixels
memory_bitmap = MakeMemoryBitmap( _
Picture1.ScaleWidth, _
Picture1.ScaleHeight)

' Draw on the bitmap.
DrawOnMemoryBitmap memory_bitmap

' Copy the device context into the PictureBox.
Picture1.AutoRedraw = True
BitBlt Picture1.hDC, 0, 0, _
Picture1.ScaleWidth, _
Picture1.ScaleHeight, _
memory_bitmap.hDC, 0, 0, SRCCOPY
Picture1.Picture = Picture1.Image

' Delete the memory bitmap.
DeleteMemoryBitmap memory_bitmap
End Sub
  • 打赏
  • 举报
回复
CreateDC
创建一个DC
yinweihong 2008-05-09
  • 打赏
  • 举报
回复
我来帮顶先~
是是非非 2008-05-09
  • 打赏
  • 举报
回复
up
是是非非 2008-05-06
  • 打赏
  • 举报
回复
up
东方之珠 2008-05-06
  • 打赏
  • 举报
回复
up
嗷嗷叫的老马 2008-05-06
  • 打赏
  • 举报
回复
帮UP
是是非非 2008-05-05
  • 打赏
  • 举报
回复
up
是是非非 2008-05-04
  • 打赏
  • 举报
回复
Debug.Print SelectObject(lDc, P.Handle)

这个是现有图片的绘图

我想要的是无中生有

用CreateBitmap创建一个图片

可能与新在COM里面的,不会有桌面DC
silencenet 2008-05-04
  • 打赏
  • 举报
回复
保存用的话反内存Dc中的图拷到窗口Dc后。。用VB中的SavePicture语句保存吧。。使用API保存图像好像没有资料可查。。:( LZ有的话分享下。。
silencenet 2008-05-04
  • 打赏
  • 举报
回复
这几天正写这个。。

Dim lDc as Long
lDc = CreateCompatibleDC(UserControl.hdc) '创建兼容内存DC
Debug.Print SelectObject(lDc, P.Handle) '将位图选入内存DC
'我这里的P是VB中的Picture对象。。我是先使用Loadpicture函数加载的图像。。
'当然你也可以用API LoadImage加载一幅图像。。P.Handle的位置写上Loadimage返回的位图句柄
'你画图的话使用API的绘图函数在这个lDc中绘。。 不过好像这个时候lDc的大小是P对象中图像的大小
'如果要自定义一个大小在lDc中的话要这样
'Dim lBmp
' lBmp = CreateCompatibleBitmap(lDc, Width,Height) '创建一幅自定义大小的位图。。
'SelectObject lDc, lBmp '将刚刚创建的位图选中内存Dc。。然后你就可以在这个lDc中绘图了。。就像在Me.Hdc中绘图一样的
Dim Bc As Long
Bc = GetPixel(lDc, 0, 0) '获得图像坐标0,0的颜色作为透明颜色

'下面是把内存lDc中的图拷到窗口Dc中的。。常用的好像是BitBlt吧。。不过我觉得这个更好用。。
Call GdiTransparentBlt(UserControl.hdc, 5, 5, priButSize - 10, priButSize - 10, lDc, 0, 0, PicDef.Width, PicDef.Height, Bc)


'API声明。。现在用的都贴出来了。。不选了。。

Private Type POINTAPI
X As Long
Y As Long
End Type

Private Type TypePic
Pic As Picture '位图
Width As Long '位图宽度(像素)
Height As Long '位图高度(像素)
BackColor As Long '背景颜色
ChuDi As Boolean '是否以作处理

End Type

Private Const WINDING = 2

Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreatePatternBrush Lib "gdi32" (ByVal hBitmap As Long) As Long
Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long
Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function GdiTransparentBlt Lib "gdi32" (ByVal hdc1 As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal W1 As Long, ByVal H1 As Long, ByVal Hdc2 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal W2 As Long, ByVal H2 As Long, ByVal Color As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long


'获取DC某一点的颜色
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long

'删除句柄
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

'创建位图
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long

'画圆角形
Private Declare Function RoundRect Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long

'画三角形
Private Declare Function PolyPolygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, lpPolyCounts As Long, ByVal nCount As Long) As Long

Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
是是非非 2008-05-03
  • 打赏
  • 举报
回复
顶起
舉杯邀明月 2008-05-02
  • 打赏
  • 举报
回复
Mark.....

Up.....
kwer 2008-05-02
  • 打赏
  • 举报
回复
在我的空间有源码
etomahawk 2008-05-02
  • 打赏
  • 举报
回复
Mark
是是非非 2008-05-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kwer 的回复:]
在我的空间有源码
[/Quote]

哥们,没找到啊,在哪儿呢?

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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