【问题标题】:Saving an image of what a device context drew, wxPython保存设备上下文绘制的图像,wxPython
【发布时间】:2011-01-12 20:09:07
【问题描述】:

我需要能够保存设备上下文画布状态的图像(格式无关紧要)。我试过dc.GetAsBitmap,但它返回无效的位图。我该怎么做?

【问题讨论】:

标签: python wxpython wxwidgets


【解决方案1】:

你可能会从 this 中找出一些东西(使用图像和 wxPython)

【讨论】:

    【解决方案2】:

    我相信这应该可以解决问题:

    def saveSnapshot(dcSource):
        # based largely on code posted to wxpython-users by Andrea Gavana 2006-11-08
        size = dcSource.Size
    
        # Create a Bitmap that will later on hold the screenshot image
        # Note that the Bitmap must have a size big enough to hold the screenshot
        # -1 means using the current default colour depth
        bmp = wx.EmptyBitmap(size.width, size.height)
    
        # Create a memory DC that will be used for actually taking the screenshot
        memDC = wx.MemoryDC()
    
        # Tell the memory DC to use our Bitmap
        # all drawing action on the memory DC will go to the Bitmap now
        memDC.SelectObject(bmp)
    
        # Blit (in this case copy) the actual screen on the memory DC
        # and thus the Bitmap
        memDC.Blit( 0, # Copy to this X coordinate
            0, # Copy to this Y coordinate
            size.width, # Copy this width
            size.height, # Copy this height
            dcSource, # From where do we copy?
            0, # What's the X offset in the original DC?
            0  # What's the Y offset in the original DC?
            )
    
        # Select the Bitmap out of the memory DC by selecting a new
        # uninitialized Bitmap
        memDC.SelectObject(wx.NullBitmap)
    
        img = bmp.ConvertToImage()
        img.SaveFile('saved.png', wx.BITMAP_TYPE_PNG)
    

    (我只是包含一个指向原始链接的链接,但无法快速找到它。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多