如何复制内存DC?

Carlven2012 2014-09-14 12:08:20
我的代码如下,定义省略...

Dim memDC As Long, hBmp As Long, oldDib As Long
Dim memDC2 As Long, hBmp2 As Long, oldDib2 As Long
Dim iW As Long, iH As Long

Private Sub Command1_Click()
StretchBlt imgHDC, 0, 0, iW, iH, memDC, 0, 0, iW, iH, vbSrcCopy
End Sub

Private Sub Command2_Click()
StretchBlt imgHDC, 0, 0, iW, iH, memDC2, 0, 0, iW, iH, vbSrcCopy
End Sub

Private Sub Form_Load()
Dim tmPic As StdPicture
Set tmPic = LoadPicture("C:\001.jpg")
imgHDC = Picture1.hdc
Dim bmp As BITMAP
hBmp = tmPic.Handle
Call GetObject(hBmp, Len(bmp), bmp)
iW = bmp.bmWidth
iH = bmp.bmHeight

memDC = CreateCompatibleDC(imgHDC)
oldDib = SelectObject(memDC, hBmp)

'内存图副本
'//////////////////////////////////////////////////////
memDC2 = CreateCompatibleDC(imgHDC)
hBmp2 = CreateCompatibleBitmap(memDC2, iW, iH)
oldDib2 = SelectObject(memDC2, hBmp2)
BitBlt memDC2, 0, 0, iW, iH, memDC, 0, 0, vbSrcCopy
End Sub

结果如下图:


求指教,怎么保存一个DC副本(以备后用)?
...全文
170 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-09-15
  • 打赏
  • 举报
回复
Bitmap Functions The following functions are used with bitmaps. AlphaBlend BitBlt CreateBitmap CreateBitmapIndirect CreateCompatibleBitmap CreateDIBitmap CreateDIBSection ExtFloodFill GetBitmapDimensionEx GetDIBColorTable GetDIBits GetPixel GetStretchBltMode GradientFill MaskBlt PlgBlt SetBitmapDimensionEx SetDIBColorTable SetDIBits SetDIBitsToDevice SetPixel SetPixelV SetStretchBltMode StretchBlt StretchDIBits TransparentBlt CreateCompatibleBitmap The CreateCompatibleBitmap function creates a bitmap compatible with the device that is associated with the specified device context. HBITMAP CreateCompatibleBitmap( HDC hdc, // handle to device context int nWidth, // width of bitmap, in pixels int nHeight // height of bitmap, in pixels ); Parameters hdc Handle to a device context. nWidth Specifies the bitmap width, in pixels. nHeight Specifies the bitmap height, in pixels. Return Values If the function succeeds, the return value is a handle to the bitmap. If the function fails, the return value is NULL. Windows NT: To get extended error information, callGetLastError. Remarks The color format of the bitmap created by the CreateCompatibleBitmap function matches the color format of the device identified by the hdc parameter. This bitmap can be selected into any memory device context that is compatible with the original device. Because memory device contexts allow both color and monochrome bitmaps, the format of the bitmap returned by the CreateCompatibleBitmap function differs when the specified device context is a memory device context. However, a compatible bitmap that was created for a nonmemory device context always possesses the same color format and uses the same color palette as the specified device context. If an application sets the nWidth or nHeight parameters to zero, CreateCompatibleBitmap returns the handle to a 1- by 1-pixel, monochrome bitmap. If a DIB section, which is a bitmap created by the CreateDIBSection function, is selected into the device context identified by the hdc parameter, CreateCompatibleBitmap creates a DIB section. When you no longer need the bitmap, call the DeleteObject function to delete it. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in wingdi.h. Import Library: Use gdi32.lib. See Also Bitmaps Overview, Bitmap Functions, CreateDIBSection, DeleteObject, SelectObject
startbin 2014-09-14
  • 打赏
  • 举报
回复
引用 1 楼 PctGL 的回复:



        hBmp2 = CreateCompatibleBitmap(memDC2, iW, iH)

        '//   先把这句改了, CreateCompatibleBitmap 的第一个参数不能用 CreateCompatibleDC 返回的dc做参数
        '//   其它的再说

不是不能用 而是默认创建出来的dc是单色位图,这个函数是根据dc原来的图像对象格式创建位图的 这里应该改成dc=getdc(0):CreateCompatibleBitmap(dc,iw,ih)
PctGL 2014-09-14
  • 打赏
  • 举报
回复



        hBmp2 = CreateCompatibleBitmap(memDC2, iW, iH)

        '//   先把这句改了, CreateCompatibleBitmap 的第一个参数不能用 CreateCompatibleDC 返回的dc做参数
        '//   其它的再说

Carlven2012 2014-09-14
  • 打赏
  • 举报
回复

        Dim dskDC As Long
        dskDC = GetDC(0)
        memDC2 = CreateCompatibleDC(dskDC)
        hBmp2 = CreateCompatibleBitmap(dskDC, iW, iH)
        ReleaseDC 0, dskDC

        memDC2 = CreateCompatibleDC(imgHDC)
        hBmp2 = CreateCompatibleBitmap(imgHDC, iW, iH)
谢谢楼上两位少侠,呵呵。 看来是我失误。 如上两种方法,我发现都可以。 看来得去补一下这个CreateCompatibleBitmap函数,看看它的参数到底是何说法。再次感谢!

1,486

社区成员

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

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