Search code examples
pythonpywin32

pywintypes.error: (0, 'SetPixel', 'No error message is available')


Im trying to recreate the RotBlt function in python, And then i get this error calling the fucntion on the class, and it says:

Traceback (most recent call last):
  File "C:\Users\MY\Desktop\bckup\Folder rafi\New folder\python-recorder\rotblt.py", line 50, in <module>
    RotBlt(hdc, 0, 0, x, y, hdc, 0,0, 10, SRCCOPY)
  File "C:\Users\MY\Desktop\bckup\Folder rafi\New folder\python-recorder\rotblt.py", line 37, in RotBlt
    SetPixel(memDC, nX+nC, nY+nC, c)
pywintypes.error: (0, 'SetPixel', 'No error message is available')

The problem was the code tries to call the SetPixel fucntion but instead giving an unknown message.

Can someone tell what's wrong with these codes?

from win32api import *
from win32gui import *
from win32con import *
from math import *

def RotBlt(destDC, sx1, sy1, sx2, sy2, srcDC, dx1, dy1, deg, mode):
    theta = deg * 0.01745
    # convert to radians

    # define size
    w = sx2-sx1
    h = sy2-sy1


    # define center
    cX = int(float(sx2+sx1)/2)
    cY = int(float(sy2+sy1)/2)

    #make 1:1 ratio.
    if w>h: h=w; 
    else: w=h

    # Allocate stuff
    memDC = CreateCompatibleDC(destDC)
    membmp = CreateCompatibleBitmap(destDC, w, h)
    obmp = SelectObject(memDC, membmp)

    # pivot for memDC
    nC = int(float(sx1)/2)

    #rotate code (kinda slow)
    for x in range(sx1, sx2):
        for y in range(sy1, sy2):
            c = GetPixel(srcDC, x, y)
            nX = int((x-cX)*sin(theta)+(y-cY)*cos(theta))
            nY = int((x-cX)*cos(theta)-(y-cY)*sin(theta))
            SetPixel(memDC, nX+nC, nY+nC, c)

    # splash rotated image
    BitBlt(destDC, dx1, dy1, w, h, memDC, 0,0, mode)

    SelectObject(memDC, obmp)
    DeleteDC(memDC)
    DeleteObject(ombp)

while True:
    x, y = GetSystemMetrics(0), GetSystemMetrics(1)
    hwnd = GetDesktopWindow()
    hdc = GetWindowDC(hwnd)
    RotBlt(hdc, 0, 0, x, y, hdc, 0,0, 10, SRCCOPY)
    ReleaseDC(hwnd, hdc)
    Sleep(10)

Thanks.


Solution

  • Nvm i make the mistake, i had to change this:

    # pivot for memDC
    nC = int(float(sx1)/2)
    

    to this:

    # pivot for memDC
    nC = int(float(w)/2)