매크로

조회 수 1623 추천 0 댓글 3
질문
클립보드에 있는 이미지를 bmp로 저장하는것 까진 검색하여 만들었는데 부분만 저장하는걸 모르겠습니다
예를들어 세븐나이츠 쫄바꾸는 스크립트를 만드는데
같은 영웅이 있을경우 쫄 부분을 이미지를 찍어서 bmp 파일로 저장하고 다시 블루스택에서 서치하여 같은쫄은 패스하려고 하는데,
아래의 스크립트에서 부분적으로 잘라서 저장하는 것을 모르겠습니다. 화면 캡쳐해서 하는것도 알긴 하는데 비활성으로 작업중이라 클립보드 이미지를 잘라서 저장하고 싶습니다. 아시는분 답변해주시면 정말 감사하겠습니다.


;스크립트----------------------------------------------------------
f3::
WinGet,hwnd,ID,BlueStacks App Player ; 캡쳐할 윈도우의 타이틀 
pToken:=Gdip_Startup() 
pBitmap:=Gdip_BitmapFromHWND(hwnd) 
Gdip_SetBitmapToClipboard(pBitmap) 
Gdip_DisposeImage(pBitmap) 
Gdip_Shutdown(pToken) 
SaveClipBoardToBMP() 
return ; 여기까지가 클립보드에 있는 이미지를 bmp로 저장하는 명령어입니다

;----소스---------------
SaveClipBoardToBMP() { 
  Convert("", "clip.bmp") 

Convert(sFileFr = "", sFileTo = "") 
  If  sFileTo  = 
      sFileTo := A_ScriptDir . "\screen.bmp" 
  SplitPath, sFileTo, , sDirTo, sExtTo, sNameTo 

  If Not  hGdiPlus := DllCall("LoadLibrary", "str", "gdiplus.dll") 
      Return  sFileFr+0 ? SaveHBITMAPToFile(sFileFr, sDirTo . "\" . sNameTo . ".bmp") : "" 
  VarSetCapacity(si, 16, 0), si := Chr(1) 
  DllCall("gdiplus\GdiplusStartup", "UintP", pToken, "Uint", &si, "Uint", 0) 

  If  !sFileFr 
  { 
      DllCall("OpenClipboard", "Uint", 0) 
      If    DllCall("IsClipboardFormatAvailable", "Uint", 2) && (hBM:=DllCall("GetClipboardData", "Uint", 2)) {
        DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "Uint", hBM, "Uint", 0, "UintP", pImage) 
      }  
      DllCall("CloseClipboard") 
  } 
  Else If  (sFileFr Is Integer) { 
      DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "Uint", sFileFr, "Uint", 0, "UintP", pImage) 
  }  
  Else { 
      DllCall("gdiplus\GdipLoadImageFromFile", "Uint", Unicode4Ansi(wFileFr,sFileFr), "UintP", pImage) 
  }  

  DllCall("gdiplus\GdipGetImageEncodersSize", "UintP", nCount, "UintP", nSize) 
  VarSetCapacity(ci, nSize) 
  DllCall("gdiplus\GdipGetImageEncoders", "Uint", nCount, "Uint", nSize, "Uint", &ci) 
  Loop,  %nCount% 
  { 
      If  !InStr(Ansi4Unicode(NumGet(ci, 76 * (A_Index - 1) + 44)), "." . sExtTo) 
        Continue 
      pCodec := &ci + 76 * (A_Index - 1) 
        Break 
  } 

  If  pImage 
      pCodec  ? DllCall("gdiplus\GdipSaveImageToFile", "Uint", pImage, "Uint", Unicode4Ansi(wFileTo,sFileTo), "Uint", pCodec, "Uint", 0) : DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "Uint", pImage, "UintP", hBitmap, "Uint", 0) . SetClipboardData(hBitmap), DllCall("gdiplus\GdipDisposeImage", "Uint", pImage) 

  DllCall("gdiplus\GdiplusShutdown" , "Uint", pToken) 
  DllCall("FreeLibrary", "Uint", hGdiPlus) 

CreateRect(ByRef Rect, x, y, w, h)
{
   VarSetCapacity(Rect, 16)
   NumPut(x, Rect, 0, "uint"), NumPut(y, Rect, 4, "uint"), NumPut(w, Rect, 8, "uint"), NumPut(h, Rect, 12, "uint")
}

Gdip_LockBits(pBitmap, x, y, w, h, ByRef Stride, ByRef Scan0, LockMode = 3, PixelFormat = 0x26200a)
{   
   CreateRect(Rect, x, y, w, h)
   VarSetCapacity(BitmapData, 21, 0)
   E := DllCall("Gdiplus\GdipBitmapLockBits", "uint", pBitmap, "uint", &Rect, "uint", LockMode, "int", PixelFormat, "uint", &BitmapData)
   Stride := NumGet(BitmapData, 8)
   Scan0 := NumGet(BitmapData, 16)
   return E
}

SaveHBITMAPToFile(hBitmap, sFile) 
  DllCall("GetObject", "Uint", hBitmap, "int", VarSetCapacity(oi,84,0), "Uint", &oi) 
  hFile:=  DllCall("CreateFile", "Uint", &sFile, "Uint", 0x40000000, "Uint", 0, "Uint", 0, "Uint", 2, "Uint", 0, "Uint", 0) 
  DllCall("WriteFile", "Uint", hFile, "int64P", 0x4D42|14+40+NumGet(oi,44)<<16, "Uint", 6, "UintP", 0, "Uint", 0) 
  DllCall("WriteFile", "Uint", hFile, "int64P", 54<<32, "Uint", 8, "UintP", 0, "Uint", 0) 
  DllCall("WriteFile", "Uint", hFile, "Uint", &oi+24, "Uint", 40, "UintP", 0, "Uint", 0) 
  DllCall("WriteFile", "Uint", hFile, "Uint", NumGet(oi,20), "Uint", NumGet(oi,44), "UintP", 0, "Uint", 0) 
  DllCall("CloseHandle", "Uint", hFile) 

Unicode4Ansi(ByRef wString, sString) 
  nSize := DllCall("MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &sString, "int", -1, "Uint", 0, "int", 0) 
  VarSetCapacity(wString, nSize * 2) 
  DllCall("MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &sString, "int", -1, "Uint", &wString, "int", nSize) 
  Return  &wString 

Ansi4Unicode(pString) 
  nSize := DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "Uint", pString, "int", -1, "Uint", 0, "int",  0, "Uint", 0, "Uint", 0) 
  VarSetCapacity(sString, nSize) 
  DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "Uint", pString, "int", -1, "str", sString, "int", nSize, "Uint", 0, "Uint", 0) 
  Return  sString 

SetClipboardData(hBitmap) 
  DllCall("GetObject", "Uint", hBitmap, "int", VarSetCapacity(oi,84,0), "Uint", &oi) 
  hDIB :=  DllCall("GlobalAlloc", "Uint", 2, "Uint", 40+NumGet(oi,44)) 
  pDIB :=  DllCall("GlobalLock", "Uint", hDIB) 
  DllCall("RtlMoveMemory", "Uint", pDIB, "Uint", &oi+24, "Uint", 40) 
  DllCall("RtlMoveMemory", "Uint", pDIB+40, "Uint", NumGet(oi,20), "Uint", NumGet(oi,44)) 
  DllCall("GlobalUnlock", "Uint", hDIB) 
  DllCall("DeleteObject", "Uint", hBitmap) 
  DllCall("OpenClipboard", "Uint", 0) 
  DllCall("EmptyClipboard") 
  DllCall("SetClipboardData", "Uint", 8, "Uint", hDIB) 
  DllCall("CloseClipboard") 
}


Gdip_UnlockBits(pBitmap)
{
   return DllCall("Gdiplus\GdipBitmapUnlockBits", "uint", pBitmap, "uint", &BitmapData)
}

Gdip_Startup()
{
    if !DllCall("GetModuleHandle", "str", "gdiplus")
        DllCall("LoadLibrary", "str", "gdiplus")
    VarSetCapacity(si, 16, 0), si := Chr(1)
    DllCall("gdiplus\GdiplusStartup", "uint*", pToken, "uint", &si, "uint", 0)
    return pToken
}

Gdip_Shutdown(pToken)
{
    DllCall("gdiplus\GdiplusShutdown", "uint", pToken)
    if hModule := DllCall("GetModuleHandle", "str", "gdiplus")
        DllCall("FreeLibrary", "uint", hModule)
    return 0
}


Gdip_CreateBitmapFromFile(sFile, IconNumber=1, IconSize="")
{
    SplitPath, sFile,,, ext
    if ext in exe,dll
    {
        Sizes := IconSize ? IconSize : 256 "|" 128 "|" 64 "|" 48 "|" 32 "|" 16
        VarSetCapacity(buf, 40)
        Loop, Parse, Sizes, |
        {
            DllCall("PrivateExtractIcons", "str", sFile, "int", IconNumber-1, "int", A_LoopField, "int", A_LoopField, "uint*", hIcon, "uint*", 0, "uint", 1, "uint", 0)
            if !hIcon
                continue

            if !DllCall("GetIconInfo", "uint", hIcon, "uint", &buf)
            {
                DestroyIcon(hIcon)
                continue
            }
            hbmColor := NumGet(buf, 16)
            hbmMask  := NumGet(buf, 12)

            if !(hbmColor && DllCall("GetObject", "uint", hbmColor, "int", 24, "uint", &buf))
            {
                DestroyIcon(hIcon)
                continue
            }
            break
        }
        if !hIcon
            return -1

        Width := NumGet(buf, 4, "int"),  Height := NumGet(buf, 8, "int")
        hbm := CreateDIBSection(Width, -Height), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)

        if !DllCall("DrawIconEx", "uint", hdc, "int", 0, "int", 0, "uint", hIcon, "uint", Width, "uint", Height, "uint", 0, "uint", 0, "uint", 3)
        {
            DestroyIcon(hIcon)
            return -2
        }

        VarSetCapacity(dib, 84)
        DllCall("GetObject", "uint", hbm, "int", 84, "uint", &dib)
        Stride := NumGet(dib, 12), Bits := NumGet(dib, 20)

        DllCall("gdiplus\GdipCreateBitmapFromScan0", "int", Width, "int", Height, "int", Stride, "int", 0x26200A, "uint", Bits, "uint*", pBitmapOld)
        pBitmap := Gdip_CreateBitmap(Width, Height), G := Gdip_GraphicsFromImage(pBitmap)
        Gdip_DrawImage(G, pBitmapOld, 0, 0, Width, Height, 0, 0, Width, Height)
        SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
        Gdip_DeleteGraphics(G), Gdip_DisposeImage(pBitmapOld)
        DestroyIcon(hIcon)
    }
    else
    {
        if !A_IsUnicode
        {
            VarSetCapacity(wFile, 1023)
            DllCall("kernel32\MultiByteToWideChar", "uint", 0, "uint", 0, "uint", &sFile, "int", -1, "uint", &wFile, "int", 512)
            DllCall("gdiplus\GdipCreateBitmapFromFile", "uint", &wFile, "uint*", pBitmap)
        }
        else
            DllCall("gdiplus\GdipCreateBitmapFromFile", "uint", &sFile, "uint*", pBitmap)
    }
    return pBitmap
}

Gdip_GetImageWidth(pBitmap)
{
   DllCall("gdiplus\GdipGetImageWidth", "uint", pBitmap, "uint*", Width)
   return Width
}

Gdip_GetImageHeight(pBitmap)
{
   DllCall("gdiplus\GdipGetImageHeight", "uint", pBitmap, "uint*", Height)
   return Height
}

Gdip_DisposeImage(pBitmap)
{
   return DllCall("gdiplus\GdipDisposeImage", "uint", pBitmap)
}

Gdip_BitmapFromHWND(hwnd)
{
    WinGetPos,,, Width, Height, ahk_id %hwnd%
    hbm := CreateDIBSection(Width, Height), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
    PrintWindow(hwnd, hdc)
    pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
    SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
    return pBitmap
}

DestroyIcon(hIcon)
{
   return DllCall("DestroyIcon", "uint", hIcon)
}

CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0)
{
    hdc2 := hdc ? hdc : GetDC()
    VarSetCapacity(bi, 40, 0)
    NumPut(w, bi, 4), NumPut(h, bi, 8), NumPut(40, bi, 0), NumPut(1, bi, 12, "ushort"), NumPut(0, bi, 16), NumPut(bpp, bi, 14, "ushort")
    hbm := DllCall("CreateDIBSection", "uint" , hdc2, "uint" , &bi, "uint" , 0, "uint*", ppvBits, "uint" , 0, "uint" , 0)

    if !hdc
        ReleaseDC(hdc2)
    return hbm
}

CreateCompatibleDC(hdc=0)
{
   return DllCall("CreateCompatibleDC", "uint", hdc)
}

Gdip_CreateBitmap(Width, Height, Format=0x26200A)
{
    DllCall("gdiplus\GdipCreateBitmapFromScan0", "int", Width, "int", Height, "int", 0, "int", Format, "uint", 0, "uint*", pBitmap)
    Return pBitmap
}

Gdip_GraphicsFromImage(pBitmap)
{
    DllCall("gdiplus\GdipGetImageGraphicsContext", "uint", pBitmap, "uint*", pGraphics)
    return pGraphics
}

Gdip_DrawImage(pGraphics, pBitmap, dx="", dy="", dw="", dh="", sx="", sy="", sw="", sh="", Matrix=1)
{
    if (Matrix&1 = "")
        ImageAttr := Gdip_SetImageAttributesColorMatrix(Matrix)
    else if (Matrix != 1)
        ImageAttr := Gdip_SetImageAttributesColorMatrix("1|0|0|0|0|0|1|0|0|0|0|0|1|0|0|0|0|0|" Matrix "|0|0|0|0|0|1")

    if (sx = "" && sy = "" && sw = "" && sh = "")
    {
        if (dx = "" && dy = "" && dw = "" && dh = "")
        {
            sx := dx := 0, sy := dy := 0
            sw := dw := Gdip_GetImageWidth(pBitmap)
            sh := dh := Gdip_GetImageHeight(pBitmap)
        }
        else
        {
            sx := sy := 0
            sw := Gdip_GetImageWidth(pBitmap)
            sh := Gdip_GetImageHeight(pBitmap)
        }
    }

    E := DllCall("gdiplus\GdipDrawImageRectRect", "uint", pGraphics, "uint", pBitmap
    , "float", dx, "float", dy, "float", dw, "float", dh
    , "float", sx, "float", sy, "float", sw, "float", sh
    , "int", 2, "uint", ImageAttr, "uint", 0, "uint", 0)
    if ImageAttr
        Gdip_DisposeImageAttributes(ImageAttr)
    return E
}

SelectObject(hdc, hgdiobj)
{
   return DllCall("SelectObject", "uint", hdc, "uint", hgdiobj)
}

DeleteObject(hObject)
{
   return DllCall("DeleteObject", "uint", hObject)
}

GetDC(hwnd=0)
{
    return DllCall("GetDC", "uint", hwnd)
}

DeleteDC(hdc)
{
   return DllCall("DeleteDC", "uint", hdc)
}

Gdip_DeleteGraphics(pGraphics)
{
   return DllCall("gdiplus\GdipDeleteGraphics", "uint", pGraphics)
}

Gdip_DisposeImageAttributes(ImageAttr)
{
    return DllCall("gdiplus\GdipDisposeImageAttributes", "uint", ImageAttr)
}

PrintWindow(hwnd, hdc, Flags=0)
{
    return DllCall("PrintWindow", "uint", hwnd, "uint", hdc, "uint", Flags)
}

ReleaseDC(hdc, hwnd=0)
{
   return DllCall("ReleaseDC", "uint", hwnd, "uint", hdc)
}

Gdip_SetImageAttributesColorMatrix(Matrix)
{
    VarSetCapacity(ColourMatrix, 100, 0)
    Matrix := RegExReplace(RegExReplace(Matrix, "^[^\d-\.]+([\d\.])", "$1", "", 1), "[^\d-\.]+", "|")
    StringSplit, Matrix, Matrix, |
    Loop, 25
    {
        Matrix := (Matrix%A_Index% != "") ? Matrix%A_Index% : Mod(A_Index-1, 6) ? 0 : 1
        NumPut(Matrix, ColourMatrix, (A_Index-1)*4, "float")
    }
    DllCall("gdiplus\GdipCreateImageAttributes", "uint*", ImageAttr)
    DllCall("gdiplus\GdipSetImageAttributesColorMatrix", "uint", ImageAttr, "int", 1, "int", 1, "uint", &ColourMatrix, "int", 0, "int", 0)
    return ImageAttr
}

Gdip_CreateBitmapFromHBITMAP(hBitmap, Palette=0)
{
    DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "uint", hBitmap, "uint", Palette, "uint*", pBitmap)
    return pBitmap
}

Gdip_CreateBitmapFromClipboard()
{
    if !DllCall("OpenClipboard", "uint", 0)
        return -1
    if !DllCall("IsClipboardFormatAvailable", "uint", 8)
        return -2
    if !hBitmap := DllCall("GetClipboardData", "uint", 2)
        return -3
    if !pBitmap := Gdip_CreateBitmapFromHBITMAP(hBitmap)
        return -4
    if !DllCall("CloseClipboard")
        return -5
    DeleteObject(hBitmap)
    return pBitmap
}

;#####################################################################################

Gdip_SetBitmapToClipboard(pBitmap)
{
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
DllCall("GetObject", "uint", hBitmap, "int", VarSetCapacity(oi, 84, 0), "uint", &oi)
hdib := DllCall("GlobalAlloc", "uint", 2, "uint", 40+NumGet(oi, 44))
pdib := DllCall("GlobalLock", "uint", hdib)
DllCall("RtlMoveMemory", "uint", pdib, "uint", &oi+24, "uint", 40)
DllCall("RtlMoveMemory", "Uint", pdib+40, "Uint", NumGet(oi, 20), "uint", NumGet(oi, 44))
DllCall("GlobalUnlock", "uint", hdib)
DllCall("DeleteObject", "uint", hBitmap)
DllCall("OpenClipboard", "uint", 0)
DllCall("EmptyClipboard")
DllCall("SetClipboardData", "uint", 8, "uint", hdib)
DllCall("CloseClipboard")
}

Gdip_CreateHBITMAPFromBitmap(pBitmap, Background=0xffffffff)
{
DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "uint", pBitmap, "uint*", hbm, "int", Background)
return hbm
}

  • 모바게 2015.07.17 13:59

    http://www.autohotkey.com/board/topic/29449-gdi-standard-library-145-by-tic/page-58#entry455137

    http://ahkscript.org/boards/viewtopic.php?t=7192


    image crop 관련해서 찾아보세요.

  • 은응 2015.07.17 14:27
    정말 감사합니다 모바게님 공부해볼게요!
  • 은응 2015.07.17 15:40
    해결했습니다 정말 감사합니다

List of Articles
분류 제목 글쓴이 최근변경 추천
자유 요즘들어.. 성운이 님의..매크로관련해서 질문이 많이 ... 5 제발등업좀2 2022.12.14 0/0
질문 스크립트조언부탁드립니다 5 하하이거다 2022.12.14 0/0
질문 밑에 not(!)을 보고 올리는 급 궁금 질문.. 2 마토깽 2022.12.14 0/0
자유 lsearch 적용방법 아시분 계세요? 5 바유무 2022.12.14 0/0
질문 방향키 명령어가 궁금합니다 1 쉬르릴 2022.12.14 0/0
질문 !(느낌표) 가 주는게 무엇을 뜻하나요? 7 디스트로이어 2022.12.14 0/0
질문 클립보드에 저장된 이미지를 부분만 잘라서 저장하고 ... 3 은응 2022.12.14 0/0
질문 pixelsearch 이미지서치 둘다 좌표 검색이요 3 바유무 2022.12.14 0/0
질문 아무것도 모르는 초보인데요 ㅠ.ㅠ 삼성 싱크사이드 ... 1 김프로 2022.12.14 0/0
질문 많이 배우고 있습니다. 질문있습니다. 7 마토깽 2022.12.14 0/0
질문 오토핫키 처음 배울때 질문 3 colt 2022.12.14 0/0
질문 고쳐야 할 부분이 있는지 봐주실 수 있을까요? 6 마토깽 2022.12.14 0/0
자유 [클래시오브클랜] 자작오토핫키로 오토파밍하는 영상 7 ddart 2022.12.14 0/0
질문 오핫으로.. 문자빼기 가능한가요? 13 제발등업좀2 2022.12.14 0/0
질문 이상하게 오토핫키 딜레이가 생겨서 질문드립니다~ 2 페르니코 2022.12.14 0/0
질문 지니모션으로 한게임 여러개 오토핫키로 돌리고싶은데 ... 3 허밍의지 2022.12.14 0/0
질문 이미지써치를 한후에 써칭한 곳에 마우스클릭하는 질문... 2 짱용호짱짱 2022.12.14 0/0
질문 문자 정렬 2 제발등업좀2 2022.12.14 0/0
자유 오토핫키 함수관련 질문이 있을땐 어떤개시판을 이용하... 2 냉면 2015.07.15 0/0
질문 오토핫키 스크립트문제좀 알려주세요. 6 흑콘 2015.07.16 0/0
Board Pagination Prev 1 ... 155156157158159160161162163 ... 209 Next
/ 209

전체 최신 인기글

전체 주간 인기글