【问题标题】:Applying both a rotation and color adjustment to a GDI DeviceContext causes the colours to be reset对 GDI DeviceContext 应用旋转和颜色调整会导致颜色被重置
【发布时间】:2011-11-08 18:57:38
【问题描述】:

我正在使用 API 调用来转换位图对象。我有各种物理变换(旋转、翻转、缩放、位移)以及一些颜色调整(反转、亮度和对比度)

除了我将旋转或剪切变换与颜色调整结合使用时,一切正常。在这种情况下,颜色会恢复为原始图像。

这个问题的有趣之处在于它似乎依赖于 XForm 矩阵中的非对角线因子(下面的 eM12 和 eM21)。例如,这里是旋转矩阵:

Public Sub RotateClockwise(Radians As Single)
    With XFormMatrix
        .eM11 = Cos(Radians)
        .eM12 = Sin(Radians)
        .eM21 = -Sin(Radians)
        .eM22 = Cos(Radians)
        .eDx = 0
        .eDy = 0
    end with
end sub

只要 eM12 或 eM21 不为零,我的颜色就会消失!例如,剪切变换也会出现同样的问题:

Public Sub Shear(ShearFactorX As Single, ShearFactorY As Single)
    With XFormMatrix
        .eM11 = 1
        .eM12 = ShearFactorX
        .eM21 = ShearFactorY
        .eM22 = 1
        .eDx = 0
        .eDy = 0
    End With
End Sub

这是我应用变换并设置颜色调整的子(缩写)

Private Declare Function CreateCompatibleDC Lib "GDI32.dll" (ByVal hDC As Long) As Long
Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hDC As Long, pBitmapInfo As BITMAPINFO, ByVal un As Long, ByVal lplpVoid As Long, ByVal handle As Long, ByVal dw As Long) As Long
Private Declare Function SelectObject Lib "GDI32.dll" (ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function SetWorldTransform Lib "gdi32" (ByVal hDC As Long, ByRef lpXform As xForm) As Long
Private Declare Function SetColorAdjustment Lib "GDI32.dll" (ByVal hDC As Long, ByRef lpCA As ColorAdjustment) As Long
Private Declare Function StretchBlt Lib "gdi32" (ByVal hDC 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 nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

Private Sub TransformAndDraw()
'Initialise objects
BufferDC = CreateCompatibleDC(0)
BgBuffer = CreateDIBSection(hDC:=BufferDC, pBitmapInfo:=bmi, un:=DIB_RGB_COLORS, lplpVoid:=0&, handle:=0&, dw:=0&)
SelectObject BufferDC, BgBuffer

'Define the transformation matrices
SetWorldTransform hDC:=BufferDC, lpXform:=XFormMatrix

' Apply this colour adustment to the Buffer DC
SetColorAdjustment hDC:=BufferDC, lpCA:=NewAdjust

'load picSource into the Buffer and apply scaling factor
    StretchBlt hDC:=BufferDC, _
               x:=0, _
               y:=0, _
               nWidth:=srcBmp.bmWidth * xsize, _
               nHeight:=srcBmp.bmHeight * xsize, _
               hSrcDC:=srcHDC, _
               xSrc:=0, _
               ySrc:=0, _
               nSrcWidth:=srcBmp.bmWidth, _
               nSrcHeight:=srcBmp.bmHeight, _
               dwRop:=vbSrcCopy

'Paint the UserControl surface with the Buffer

'reset and delete objects
End Sub

总结一下我的问题:在我的非对角因子使用非零值时,有什么方法可以同时使用 SetWorldTransformSetColorAdjustment XForm 转换?

【问题讨论】:

    标签: vb.net gdi


    【解决方案1】:

    好的,我找到了解决方案。我将物理变换和颜色调整分成两个单独的 StretchBlt 调用。在这期间,我重置了 Image-Buffer 中的 Xform 矩阵,使其在颜色调整期间不存在。

    在第一次调用中,外部图片源被加载到缓冲区中并根据 Xform 矩阵进行拉伸。 在第二次调用中,缓冲区被绘制到自身上以应用颜色调整。

    Private Sub TransformAndDraw()
        'Initialise objects
        BufferDC = CreateCompatibleDC(0)
        BgBuffer = CreateDIBSection(hDC:=BufferDC, pBitmapInfo:=bmi, un:=DIB_RGB_COLORS, lplpVoid:=0&, handle:=0&, dw:=0&)
        SelectObject BufferDC, BgBuffer
    
        'extract the original transform-settings
        OldGraphicsMode = SetGraphicsMode(hDC:=BufferDC, iMode:=GM_ADVANCED)
        GetWorldTransform hDC:=BufferDC, lpXform:=OldMatrix
        ' Get the current (default) colour adjustment
        OldStretchMode = SetStretchBltMode(hDC:=BufferDC, nStretchMode:=HALFTONE)
        OrigAdjust.caSize = Len(OrigAdjust)
        GetColorAdjustment hDC:=BufferDC, lpCA:=OrigAdjust
    
        'Define the transformation matrices
        SetWorldTransform hDC:=BufferDC, lpXform:=XFormMatrix
    
        'load picSource into the Buffer and apply scaling factor
        'hSrcDC:=srcHDC
        StretchBlt hDC:=BufferDC, _
                   x:=0, _
                   y:=0, _
                   nWidth:=srcBmp.bmWidth * xsize, _
                   nHeight:=srcBmp.bmHeight * xsize, _
                   hSrcDC:=srcHDC, _
                   xSrc:=0, _
                   ySrc:=0, _
                   nSrcWidth:=srcBmp.bmWidth, _
                   nSrcHeight:=srcBmp.bmHeight, _
                   dwRop:=vbSrcCopy
    
        'revert the transform settings, these can conflict with the colour adjustment
        SetWorldTransform hDC:=BufferDC, lpXform:=OldMatrix
        SetGraphicsMode hDC:=BufferDC, iMode:=OldGraphicsMode
    
       ' Apply the colour adustment to the Buffer DC
        SetColorAdjustment hDC:=BufferDC, lpCA:=NewAdjust
    
        'now apply a second call to StretchBlt, this time painting the buffer with itself!
        'hSrcDC:=BufferDC!
        StretchBlt hDC:=BufferDC, _
                   X:=0, _
                   Y:=0, _
                   nWidth:=UserControl.ScaleWidth, _
                   nHeight:=UserControl.ScaleHeight, _
                   hSrcDC:=BufferDC, _
                   xSrc:=0, _
                   ySrc:=0, _
                   nSrcWidth:=UserControl.ScaleWidth, _
                   nSrcHeight:=UserControl.ScaleHeight, _
                   dwRop:=vbSrcCopy
    
        'now revert the colour adjustment settings
        SetStretchBltMode hDC:=BufferDC, nStretchMode:=OldStretchMode
        SetColorAdjustment hDC:=BufferDC, lpCA:=OrigAdjust
    
        'Paint the UserControl surface with the Buffer
        BitBlt hDestDC:=UserControl.hDC, _
               X:=0, _
               Y:=0, _
               nWidth:=UserControl.ScaleWidth, _
               nHeight:=UserControl.ScaleHeight, _
               hSrcDC:=BufferDC, _
               xSrc:=0, _
               ySrc:=0, _
               dwRop:=vbSrcCopy
    
        'Delete objects!
        DeleteDC BufferDC
        DeleteObject BgBuffer
        UserControl.Refresh
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多