注册 登录
编程论坛 VFP论坛

请教用MyFll.Fll截取图片问题?

wxzd123 发布于 2024-10-28 15:50, 284 次点击
只有本站会员才能查看附件,请 登录

 截取表单上的图片
Set Library To myFll AddI
l=ThisForm.Image1.Left
t=ThisForm.Image1.top
w=ThisForm.Image1.Left+ThisForm.Image1.Width
h=ThisForm.Image1.top+ThisForm.Image1.Height
RectToBmp(l,t,w,h,"tp.bmp")
只有本站会员才能查看附件,请 登录

当100%时可以,125%时乘以这个系数也可以,但150%是再乘这个系数截取的就不对了?哪位老师知道是什么问题?谢谢
7 回复
#2
sych2024-10-29 10:38
原生的windows窗口,菜单有自己的句柄
vfp的窗口不是纯粹的表单,还包括菜单栏,所以用窗口句柄获取的图像包括菜单和表单

#3
wxzd1232024-10-29 14:13
sych老师您好,可是当100%截取没有问题,当125%时用125%当系数也没有问题,当150%时用150%当系数就不对了
#4
吹水佬2024-10-29 14:34
可能是VFP对系统显示的缩放设置兼容问题
#5
wxzd1232024-10-29 21:39
谢谢
#6
sych2024-10-31 10:52
是的,设置为150%时,我们在设计时拖放image控件就能发现,显示的虚框是不准确的

#7
sych2024-10-31 11:07
自己写一个,可以避免屏幕放大造成的困扰,而且屏幕被遮挡也可以正确截屏
PUBLIC oform1

oform1=NEWOBJECT("form1")
oform1.Show
RETURN

DEFINE CLASS form1 AS form


    ShowWindow = 2
    DoCreate = .T.
    Caption = "Form1"
    BackColor = RGB(240,240,0)
    Name = "Form1"


    ADD OBJECT image1 AS image WITH ;
        Picture = GETPICT(),;
        Stretch = 2, ;
        Height = 200, ;
        Left = 84, ;
        Top = 24, ;
        Width = 200, ;
        Name = "Image1"


    ADD OBJECT command1 AS commandbutton WITH ;
        Top = 108, ;
        Left = 324, ;
        Height = 25, ;
        Width = 60, ;
        Caption = "截图", ;
        Name = "Command1"


    PROCEDURE command1.Click
        thisformTitleBar=thisform.TitleBar
        thisformBorderStyle= thisform.BorderStyle
        thisform.TitleBar=0
        thisform.BorderStyle= 0

        hwnd=thisform.hwnd
        tcFile="c:\tp.bmp"
        lleft=thisform.image1.Left
        ltop=thisform.image1.Top
        lwidth=thisform.image1.Width
        lheight=thisform.image1.height
        DECLARE INTEGER GetDesktopWindow IN win32api
        DECLARE INTEGER GetDC IN win32api INTEGER hwnd
        Declare SHORT GetWindowRect IN user32 INTEGER hwnd, STRING @ lpRect
        DECLARE Long GetWindowDC IN WIN32API  Long hWnd
        DECLARE Long ReleaseDC IN WIN32API  Long hWnd, Long hDc
        DECLARE Long CreateCompatibleDC IN WIN32API  Long hDc
        DECLARE Long DeleteDC IN WIN32API  Long hDc
        DECLARE Long CreateCompatibleBitmap IN WIN32API  Long hDc, Long nWidth, Long nHeight
        DECLARE Long SelectObject IN WIN32API  Long hDc, Long hObject
        DECLARE Long DeleteObject IN WIN32API  Long hObject
        DECLARE Long GdiplusStartup IN gdiplus Long @ token, String @ inputbuf, Long @ outputbuf
        DECLARE Long GdiplusShutdown IN gdiplus  Long token
        DECLARE Long GdipCreateBitmapFromHBITMAP IN gdiplus  Long hbitmap, Long hpalette, Long @ hGpBitmap
        Declare Long GdipCreateBitmapFromScan0 in GdiPlus.dll ;
            Integer width, Integer height, Integer stride, Long format, Long scan0, Long @ bitmap
        Declare Long GdipGetImageGraphicsContext in GdiPlus.dll    Long image, Long @ graphics
        DECLARE LONG GdipDeleteGraphics IN GDIPLUS LONG graphics
        Declare Long GdipDrawImageRectI in GdiPlus.dll ;
            Long graphics, Long image, Integer x, Integer y, Integer width, Integer height

        DECLARE Long GdipDisposeImage IN gdiplus  Long image
        DECLARE Long GdipSaveImageToFile IN gdiplus Long nImage, String FileName,String @ clsIdEncoder, Long encoderParams
        DECLARE INTEGER CLSIDFromString IN ole32 STRING lpsz, STRING @pclsid
        DECLARE INTEGER PrintWindow IN win32api INTEGER,INTEGER ,INTEGER
        DECLARE INTEGER BitBlt IN gdi32;
        INTEGER hDestDC, INTEGER x, INTEGER y,;
        INTEGER nWidth, INTEGER nHeight, INTEGER hSrcDC,;
        INTEGER xSrc, INTEGER ySrc, INTEGER dwRop
        #define SRCCOPY 0x00CC0020
        LOCAL cFileExtName, cEncoder, iInputBuf, iResult, hDesktopDc, hVDc, hBitmap, hToken, hGdipBitmap,encoderClsid,lpRect,lnWidth,lnheight,abc
        m.cFileExtName =LOWER( JUSTEXT( m.tcFile ))
        m.hDesktopDc = GetDC( m.hWnd )
        m.hVdc = CreateCompatibleDC( m.hDesktopDc )
        lpRect = REPLI (Chr(0), 16)
        =GetWindowRect (m.hWnd, @lpRect)
        lnWidth=ctobin(SUBS(lpRect,9,4),'4rs')-ctobin(SUBS(lpRect,1,4),'4rs')
        lnHeight=ctobin(SUBS(lpRect,13,4),'4rs')-ctobin(SUBS(lpRect,5,4),'4rs')
        m.hBitmap = CreateCompatibleBitmap( m.hDesktopDc,lnWidth,lnHeight )
        ABC=SelectObject( m.hVdc, m.hBitmap )
        IF hwnd=GetDesktopWindow()
            =BitBlt(hvDC,0,0,lnwidth,lnHeight,hDesktopDc,0,0,SRCCOPY)
        else
            =PrintWindow(m.hWnd,m.hVdc,0)
        endif
        m.hToken = 0
        m.iInputBuf = PADR(CHR(1), 16, CHR(0))
        m.iResult = -1
        IF ( 0 == GdiplusStartup( @ m.hToken, @ m.iInputBuf, 0 ))
            m.hGdipBitmap = 0
            IF ( 0 == GdipCreateBitmapFromHBITMAP(m.hBitmap, 0, @ m.hGdipBitmap ))
                lnFormat=0x00021808
                graphics = 0
                resizedImage = 0
                =GdipCreateBitmapFromScan0(lwidth,lheight, 0, m.lnFormat, 0, @resizedImage)
                =GdipGetImageGraphicsContext(m.resizedImage, @graphics)
                =GdipDrawImageRectI(m.graphics, m.hGdipBitmap,-lleft,-ltop,lnwidth,lnheight)
                m.encoderClsid  = "{557CF40"+chr(47+(at(m.cFileExtName,"bmpjpggif      tifpng")+2)/3)+"-1A04-11D3-9A73-0000F81EF32E}"
                cEncoder= REPLICATE(CHR(0),16)
                CLSIDFromString(STRCONV(m.encoderClsid + CHR(0), 5), @cEncoder)
                m.iResult = GdipSaveImageToFile(m.resizedImage,STRCONV( m.tcFile+CHR(0), 5 ), @ m.cEncoder, 0 )
                GdipDeleteGraphics(graphics)
                GdipDisposeImage(m.resizedImage)
                GdipDisposeImage( m.hGdipBitmap )
            ENDIF
            GdiplusShutdown( m.hToken )
        ENDIF
        =DeleteObject( m.hBitmap )
        =DeleteDC( m.hVdc )
        =ReleaseDC( 0, m.hDesktopDc )
        thisform.TitleBar=thisformTitleBar
        thisform.BorderStyle=thisformBorderStyle
        RETURN
    ENDPROC


ENDDEFINE
#8
wxzd1232024-10-31 20:11
sych老师太强了,能否编译成文件方便在程序中任何地方方便调用,截图是界面跳动,谢谢
1