Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

GDI+ standard library 1.45 by tic


  • Please log in to reply
1385 replies to this topic
GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009
Any way to make this not interfere with anything, so it can go over the current window (FPS) without causing control issues (Ever played a first person with an always on top window near the middle? The mouse constantly gets stuck on it, at least for minecraft.)

#SingleInstance, Force
#NoEnv
SetBatchLines, -1
;#Include, Gdip.ahk

IfNotExist, hair.png
UrlDownloadToFile, http://floridapundit.com/wp-content/uploads/2011/01/crosshairs-300x300.png, hair.png

If !pToken := Gdip_Startup()
{
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
ExitApp
}
OnExit, Exit

Width := A_ScreenWidth
Height := A_ScreenHeight
Gui, 1: Default
Gui, -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs +HwndHwnd1
Gui, Show, NA

hbm := CreateDIBSection(Width, Height)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)

pBitmap := Gdip_CreateBitmapFromFile("hair.png")

x := Width/2 - Gdip_GetImageWidth(pBitmap)/2
y := Height/2 - Gdip_GetImageHeight(pBitmap)/2
Gdip_DrawImage(G, pBitmap, x, y, 300, 300)

UpdateLayeredWindow(hwnd1, hdc, 0, 0, Width, Height)
Return

Exit:
Gdip_DisposeImage(pBitmap)
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
ExitApp
Return


tic
  • Members
  • 1934 posts
  • Last active: Dec 21 2015 01:05 PM
  • Joined: 22 Apr 2007
Wicked:

This would be the c++ for the same code with variation

int Gdip_FilterColor(unsigned char * Bitmap, int w, int h, int Stride, unsigned int Color, unsigned int ReplaceColor, int v)
{
unsigned int p, A1, R1, G1, B1, A2, R2, G2, B2, tA, tR, tG, tB;
int offset = Stride/4;

A1 = (Color | 0xff000000) >> 24;
R1 = (Color | 0x00ff0000) >> 16;
G1 = (Color | 0x0000ff00) >> 8;
B1 = Color | 0x000000ff;

A2 = (ReplaceColor | 0xff000000) >> 24;
R2 = (ReplaceColor | 0x00ff0000) >> 16;
G2 = (ReplaceColor | 0x0000ff00) >> 8;
B2 = ReplaceColor | 0x000000ff;

for (int y = 0; y < h; ++y)
{
  for (int x = 0; x < w; ++x)
  {
   p = (4*x)+(y*Stride);
  
   tA = Bitmap[3+p];
   tR = Bitmap[2+p];
   tG = Bitmap[1+p];
   tB = Bitmap[p];
   if ((tA < A1+v || tA > A1-v) && (tR < R1+v || tR > R1-v) && (tG < G1+v || tG > G1-v) && (tB < B1+v || tB > B1-v))
   {
    Bitmap[3+p] = A2;
    Bitmap[2+p] = R2;
    Bitmap[1+p] = G2;
    Bitmap[p] = B2;
   }
  }
}
return 0;
}

This doesn't compile to machine code though as it uses temporary pointers. It could probably be re-jigged to compile to machine code, or the above could just be compiled straight to a dll and called.

note1234:

This needs to be compiled to machine code and called

int Gdip_PixelSearch(unsigned int * HayStack, int w, int h, int Stride, int ARGB, int * x, int * y)
{
int offset = Stride/4;
for (int y1 = 0; y1 < h; ++y1)
{
for (int x1 = 0; x1 < w; ++x1)
{
if (HayStack[x1+y1*offset] == ARGB)
{
x[0] = x1; y[0] = y1;
return 0;
}
}
}
x[0] = -1; y[0] = -1;
return -1;
}

Geekdude:

Try adding window style WS_EX_TRANSPARENT (0x20)
Haven't tested this yet though with layered windows

Edit:
Apologies if the code is unreadable. This new forum clearly hasn't been tested. I would gladly help with improving the forum, but it clearly needs a DEV and UAT server.
I'm not sure why this cowboy approach to development is being used.

Edit2:
Wicked. I updated the code. That code compiles to machine code

RHCP
  • Members
  • 1228 posts
  • Last active: Apr 08 2017 06:17 PM
  • Joined: 29 May 2006
The other day i was trying to work out how to change the colour of an image using gdi drawimage and matrices. For other noobs like myself, here's a very basic function from a program of mine which generates appropriate colour translation matrices and stores them in an array for use with drawimage.
The first parameter of this function is for something else.

Cheers.

P.S. A big thanks to tic and others for creating such an amazing library.


SetupColourArrays(ByRef HexColour, Byref MatrixColour)
{
If IsByRef(HexColour)
HexColour := []
If IsByRef(MatrixColour)
MatrixColour := []
HexCoulourList := "White=FFFFFF|Red=B4141E|Blue=0042FF|Teal=1CA7EA|Purple=540081|Yellow=EBE129|Orange=FE8A0E|Green=168000|Light Pink=CCA6FC|Violet=1F01C9|Light Grey=525494|Dark Green=106246|Brown=4E2A04|Light Green=96FF91|Dark Grey=232323|Pink=E55BB0|Black=000000"
loop, parse, HexCoulourList, |
{
StringSplit, Item , A_LoopField, = ;Format "White = FFFFFF"
If IsByRef(HexColour)
HexColour[Item1] := Item2 ; White, FFFFFF - hextriplet R G B
If IsByRef(MatrixColour)
{
colour := Item2
colourRed := "0x" substr(colour, 1, 2)
colourGreen := "0x" substr(colour, 3, 2)
colourBlue := "0x" substr(colour, 5, 2)
colourRed := Round(colourRed/0xFF,2)
colourGreen := Round(colourGreen/0xFF,2)
colourBlue := Round(colourBlue/0xFF,2)
Matrix =
(
0 |0 |0 |0 |0
0 |0 |0 |0 |0
0 |0 |0 |0 |0
0 |0 |0 |1 |0
%colourRed% |%colourGreen% |%colourBlue% |0 |1
)
MatrixColour[Item1] := Matrix
}
}
Return
}


Wicked
  • Members
  • 504 posts
  • Last active: Nov 02 2017 11:07 PM
  • Joined: 07 Jun 2008
Apologies for not seeing your reply soon, tic. This new forum layout is painful. I used to find the thread by using the simple Google search that the old one had... But the new search is just awful, and I used to use it daily, so I've not been using the AHK forums much. :(.

3nL8f.png


Wicked
  • Members
  • 504 posts
  • Last active: Nov 02 2017 11:07 PM
  • Joined: 07 Jun 2008
Hello again, tic.

It's the first time I've compiled C++ to MCode using MCodeGen, so I've most likely done something wrong, but...

Take the following image:
Posted Image
http://img706.imageshack.us/img706/8632/beepmk.png

What needs to be done first, is all non-white pixels need to be removed or changed to fully transparent. As you saw, previously I did this with:
Gdip_FilterColor(pBitmap,Color)
{
   Gdip_GetDimensions(pBitmap, w, h)
   pBitmap2 := Gdip_CreateBitmap(w, h)
   Gdip_LockBits(pBitmap, 0, 0, w, h, Stride1, Scan01, BitmapData1)
   Gdip_LockBits(pBitmap2, 0, 0, w, h, Stride2, Scan02, BitmapData2)
   Loop, %h%
   {
      y := A_Index-1
      Loop, %w%
      {
         x := A_Index-1
         ARGB := Gdip_GetLockBitPixel(Scan01, x, y, Stride1)
         if (ARGB = Color)
            Gdip_SetLockBitPixel(ARGB,Scan02,x, y,Stride2)
      }
   }
   Gdip_UnlockBits(pBitmap, BitmapData1), Gdip_UnlockBits(pBitmap2, BitmapData2)
   Gdip_DisposeImage(pBitmap)
   return pBitmap2
}

This worked great, albeit very slow. Also, some of my users showed me the sometimes the pixels are not exactly 0xffffffff on their machines; they were sometimes 0xfffefefe. This was my need for the variation.

Anyways, the function I came up with to filter the colors using your C++ was:
Gdip_FilterColor(bitmap,color,replacecolor,variation=0){
    static filtercolor
    if !filtercolor{
        mcode_filtercolor:="83EC248B5424388B4C243C8BC20D0000FF00C1E810894424148BC10D0000FF00C1E810578BFA8944242081CAFF0000008BC181CF00FF00000D00FF00008954243C8B542434C1EF08C1E80881C9FF000000897C241C89442424894C244085D20F8E090100008B442444535533C9568B74243C894C242085F60F8EDA0000008B5C24388BFB8D53022BFA8D34118BEB2BEA83C703458BCB2BCA8B54243C897C2410896C2414894C241C895424188D6424000FB63C370FB61C2E0FB62C310FB6168D88FF0000003BF9720BB9FF0000002BC83BF9765F8B7C24248D0C073BD172088BCF2BC83BD1764C8B7C24288D0C073BD972088BCF2BC83BD976398B5424488D0C023BE972088BCA2BC83BE976268B4C242C8B6C24148B7C24108B54241C880E8B4C2430880C2E8B4C244CC60437FF880C32EB088B6C24148B7C24108B4C241C83C604FF4C24180F8564FFFFFF8B4C24208B5424408B74243C034C24444A894C2420895424400F850BFFFFFF5E5D5B33C05F83C424C3"
        varsetcapacity(filtercolor,strLen(mcode_filtercolor)//2)
        loop,% strlen(mcode_filtercolor)//2
            numput("0x" substr(mcode_filtercolor,(2*a_index)-1,2),filtercolor,a_index-1,"char")
    }
    gdip_getimagedimensions(bitmap,w,h)
    gdip_lockbits(bitmap,0,0,w,h,stride,scan,bitmapdata)
    e:=dllcall(&filtercolor,"uint",scan,"int",w,"int",h,"int",stride,"uint",color,"uint",replacecolor,"int",variation)
    gdip_unlockbits(bitmap,bitmapdata)
    return (e="") ? -3:e
}
However, this doesn't work... I'm sure it's a simple mistake, but could you possibly show me the fastest method of achieving what I need?

3nL8f.png


CodeEater
  • Members
  • 83 posts
  • Last active: Jun 06 2013 03:42 PM
  • Joined: 17 Jun 2012
Hello ,RHCP
Your function is interesting.Could you show how it work?

Wicked
  • Members
  • 504 posts
  • Last active: Nov 02 2017 11:07 PM
  • Joined: 07 Jun 2008
^+m::
SetupColourArrays(hex,matrix)
return

gdip_draw_image(pGraphics,pBitmap,dx,dy,dw,dh,"","","","",matrix["teal"]) would draw the image at dx,dy,dw,dh, but draw it teal.

3nL8f.png


tic
  • Members
  • 1934 posts
  • Last active: Dec 21 2015 01:05 PM
  • Joined: 22 Apr 2007
Wicked:

Made a couple of mistakes as I wrote it directly into the browser. Here it is with testing:

http://pastebin.com/8eqGitCp

and C++

http://pastebin.com/vFgELSEi

Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
Edit: Nevermind... Don't know what was it or what I did but it's working now!

Hi! I use the function below to make screenshots from parts of the screen but it doesn't seem to be working on Windows 8. I haven't yet found any posts about issues with Win8 so I'm asking for some help.
The same script works fine on Windows 7.
file := A_ScriptDir "\" A_Now ".png"
screen := "20|50|500|100"

Screenshot(file,screen)

Screenshot(outfile, screen)
{
pToken := Gdip_Startup()

pBitmap := Gdip_BitmapFromScreen(screen)

Gdip_SaveBitmapToFile(pBitmap, outfile, 100)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
}

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012
Hi, trying to get the currently displayed screen and save to png, this is what I have..
#Persistent
;#NoTrayIcon

SetWorkingDir, %A_ScriptDir%
#Include Gdip.ahk

if not exist ScreenShots
FileCreateDir, ScreenShots
name=%A_MM%-%A_DD%,%A_Hour%,%A_Min%
loop
{
Clip=%ClipBoardAll%

pToken := Gdip_Startup()
pBitmap := Gdip_BitmapFromScreen(Screen=0)
Gdip_SaveBitmapToFile(pBitmap, "ScreenShots\temp.PNG")
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
FileMove, ScreenShots\temp.png, Screenshots\%name%.png
; FileDelete, ScreenShots\temp.png
ClipBoard = %Clip%
Sleep 180000
}
I'm not sure what it's doing now, only that the file is not being made. Can someone help with that?

Edit: Sorry, I'm stupid, figured it out. Slower than I would have liked, but this will do just fine.

OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012
Hmm, is there a way to add the cursor? Or, not exclude it? Not sure what to change for that.

OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


Wicked
  • Members
  • 504 posts
  • Last active: Nov 02 2017 11:07 PM
  • Joined: 07 Jun 2008
Thank you for your continued help, tic! It doesn't go without appreciation!

3nL8f.png


Wicked
  • Members
  • 504 posts
  • Last active: Nov 02 2017 11:07 PM
  • Joined: 07 Jun 2008
I do think you misunderstood me, tic. While I still find use for your function, and can still make it work, I needed it to change all of the pixels that weren't a given color to another color (transparent). The one you posted above, which I'm already using elsewhere, changes the color of the given color. So using the image above, it'd change all the white text to another color, when I needed it to change everything but the white text. :P.
Posted Image

3nL8f.png


CodeEater
  • Members
  • 83 posts
  • Last active: Jun 06 2013 03:42 PM
  • Joined: 17 Jun 2012
Thank you ,Wicked and tic
Gdip_FilterColor() is very useful with me.

CodeEater
  • Members
  • 83 posts
  • Last active: Jun 06 2013 03:42 PM
  • Joined: 17 Jun 2012
I think you can change line 26: if ((tA < A1+v || tA > A1-v) && (tR < R1+v || tR > R1-v) && (tG < G1+v || tG > G1-v) && (tB < B1+v || tB > B1-v))
to if (!((tA < A1+v || tA > A1-v) && (tR < R1+v || tR > R1-v) && (tG < G1+v || tG > G1-v) && (tB < B1+v || tB > B1-v)))
It may change every color but the one you need to skip.