 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
tic
Joined: 22 Apr 2007 Posts: 1786
|
Posted: Wed Aug 04, 2010 2:16 am Post subject: |
|
|
Carcophan:
If you will only need to alter the image a small number of times then Tutorial 9 is still applicable. It would be done in exactly the same way. Draw your graph into a pBitmap and SetImage a converted hBitmap to the hwnd of your control/window. If it will be updated many times then create a gdi bitmap and continually bitblt its hdc into the hwnd of the control/window.
You can see a similar use of it in my post here:
http://www.autohotkey.com/forum/viewtopic.php?t=59265
(I stole Zaelia's elasticity formula)
Lucid_Method:
Glad you like the examples I really wish I could devote more time and write more....
Well....It can be done in ahk as seen below, however, it is quite slow. I tried spaghetti coding it a little bit to speed it up, but it didnt make too much difference. Its not super slow though, but I would need to rewrite it in c++ and compile to machine code. I'll write the machine code equivalent as I think it would be a nice addition. Im still thinking how best to go about adding lockbits to the library.....anyone have any comments on how it currently looks?
| Code: |
#SingleInstance, Force
#NoEnv
SetBatchLines, -1
; Uncomment if Gdip.ahk is not in your standard library
;#Include, Gdip.ahk
If !pToken := Gdip_Startup()
{
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
ExitApp
}
OnExit, Exit
pBitmapHayStack := Gdip_CreateBitmapFromFile("ImageHayStack.png")
pBitmapNeedle := Gdip_CreateBitmapFromFile("ImageNeedle.png")
Time1 := A_TickCount
Coords := SlowImageSearch(pBitmapHayStack, pBitmapNeedle)
MsgBox, % A_TIckCount-Time1 "`n" Coords ;%
Gdip_DisposeImage(pBitmapHayStack), Gdip_DisposeImage(pBitmapNeedle)
return
;#######################################################################
SlowImageSearch(pBitmapHayStack, pBitmapNeedle)
{
Width1 := Gdip_GetImageWidth(pBitmapHayStack), Height1 := Gdip_GetImageHeight(pBitmapHayStack)
Width2 := Gdip_GetImageWidth(pBitmapNeedle), Height2 := Gdip_GetImageHeight(pBitmapNeedle)
E1 := Gdip_LockBits(pBitmapHayStack, 0, 0, Width1, Height1, Stride1, Scan01)
E2 := Gdip_LockBits(pBitmapNeedle, 0, 0, Width2, Height2, Stride2, Scan02)
p := Gdip_GetLockBitPixel(Scan02, 0, 0, Stride2)
y1 := 0
Loop, %Height1%
{
Loop, %Width1%
{
x1 := A_Index-1, y2 := 0, yt := y1
Match := 1
Loop, %Height2%
{
xt := x1
if (A_Index = 1)
{
if (Gdip_GetLockBitPixel(Scan01, ++xt, yt, Stride1) != p)
{
Match := 0
GoTo, NoMatch
}
}
else
{
Loop, %Width2%
{
if (Gdip_GetLockBitPixel(Scan01, ++xt, yt, Stride1) != Gdip_GetLockBitPixel(Scan02, A_Index-1, y2, Stride2))
{
Match := 0
GoTo, NoMatch
}
}
}
y2++, yt++
}
if Match
GoTo, Match
NoMatch:
continue
}
y1++
}
Match:
Gdip_UnlockBits(pBitmapHayStack), Gdip_UnlockBits(pBitmapNeedle)
return Match ? xt-Width2 "|" yt-Height2 : 0
}
;#######################################################################
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
}
;#######################################################################
Gdip_UnlockBits(pBitmap)
{
return DllCall("Gdiplus\GdipBitmapUnlockBits", "uint", pBitmap, "uint", &BitmapData)
}
;#######################################################################
Gdip_ToARGB(A, R, G, B)
{
return (A << 24) | (R << 16) | (G << 8) | B
}
;#######################################################################
Gdip_FromARGB(ARGB, ByRef A, ByRef R, ByRef G, ByRef B)
{
A := (0xff000000 & ARGB) >> 24
R := (0x00ff0000 & ARGB) >> 16
G := (0x0000ff00 & ARGB) >> 8
B := 0x000000ff & ARGB
}
;#######################################################################
Gdip_AFromARGB(ARGB)
{
return (0xff000000 & ARGB) >> 24
}
;#######################################################################
Gdip_RFromARGB(ARGB)
{
return (0x00ff0000 & ARGB) >> 16
}
;#######################################################################
Gdip_GFromARGB(ARGB)
{
return (0x0000ff00 & ARGB) >> 8
}
;#######################################################################
Gdip_BFromARGB(ARGB)
{
return 0x000000ff & ARGB
}
;#######################################################################
Gdip_SetLockBitPixel(ARGB, Scan0, x, y, Stride)
{
Numput(ARGB, Scan0+0, (x*4)+(y*Stride))
}
;#######################################################################
Gdip_GetLockBitPixel(Scan0, x, y, Stride)
{
return NumGet(Scan0+0, (x*4)+(y*Stride))
}
;#######################################################################
Exit:
Gdip_Shutdown(pToken)
ExitApp
return |
edit:
added
| Code: | | p := Gdip_GetLockBitPixel(Scan02, 0, 0, Stride2) |
for a bit more speed
Last edited by tic on Wed Aug 04, 2010 2:42 am; edited 2 times in total |
|
| Back to top |
|
 |
Lucid_Method
Joined: 19 Apr 2010 Posts: 145 Location: Mobile, AL
|
Posted: Wed Aug 04, 2010 2:30 am Post subject: |
|
|
You rock Tic I'll give it a try tomorrow. Speed isn't a big concern in my case |
|
| Back to top |
|
 |
Carcophan
Joined: 24 Dec 2008 Posts: 1308 Location: :noitacoL
|
Posted: Wed Aug 04, 2010 9:45 pm Post subject: |
|
|
Huzzah! We have made some progress, thank you Tic.
It isn't prefect yet, but I am 48x farther along now than I was at the begining of the week.
| Code: |
#SingleInstance Force
#NoEnv
#Include GDIP.ahk
SetBatchLines, -1
CoordMode, Mouse, Relative
SetTitleMatchMode, 2
SetFormat, Float, 0.1
hotkey, ESC, kill
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//1.5, Height := A_ScreenHeight//1.5 ;scales to screen size
Width := 500, Height := 400
Gui, 1: +LastFound
Gui, 1: Show, w%Width% h%Height%, TestGraph using gdi+
hwnd1 := WinExist()
hwnddc := GetDC(hwnd1)
hbm := CreateDIBSection(Width, Height), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc), Gdip_SetSmoothingMode(G, 4)
pBitmapBackground := CreateBackground(Width, Height)
SetTimer, Update, 30
return
;#######################################################################
Update:
Gdip_DrawImage(G, pBitmapBackground, 0, 0, Width, Height)
Gdip_DrawImage(G, pBitmap1)
BitBlt(hwnddc, 0+25, 0+25, Width-50, Height-50, hdc, 0, 0)
return
;#######################################################################
CreateBackground(Width, Height)
{
pBitmap := Gdip_CreateBitmap(Width, Height)
G := Gdip_GraphicsFromImage(pBitmap), Gdip_SetSmoothingMode(G, 4)
pBrushBG := Gdip_BrushCreateSolid(0xffeee9e9) ;eee9e9 = snowwhite, 'ff' is non-transparent, aa = fully tranp
gdip_FillRectangle(G, pBrushBG, 0, 0, width, height)
pPenBG := Gdip_CreatePen(0xffaa0330, 3) ;works with line below \/
gdip_DrawRectangle(G, pPenBG, 0, 0, width, height) ;works with line above /\
pPen1 := Gdip_CreatePen(0xff000000, 3) ;black bar /segment
pPen2 := Gdip_CreatePen(0xff000000, 1) ;black line
Random, LoopVar, 50, 150 ;number of Horz linegments to display
L1 := 0 ;lngth of line
x2 := 0 ;end of line segment
loop, %LoopVar%
{
;Random, y1, 100, %rr%
Random, y1, 5, %Height% ;-55 ;random hght for vert black lines for testing, replace with xls MRC data
Random, L1, 1, 15 ;random segment length, change to xls input for ticdur
x1 := x2 ;set x1 and x2 to 0 for first loop,,, sets x1 to match x2 bc x2 is about to have distance added
x2 += L1 ;distance/duration added to x2, new end point for line segment, in next loop x1 will inheret this l1 value
Gdip_DrawLine(G, pPen1, x1, y1, x2, y1)
}
drawvarY = 0
drawvarX = 0
Loop 34
{
Gdip_DrawLine(G, pPen2, drawvarY, 0, drawvarY, Width)
Gdip_DrawLine(G, pPen2, 0, drawvarX, Width, drawvarX) ;lines
drawvarY += 36 ;line vert spacing, goal=15min segments ;;;;;;;900/36=25
drawvarX += 50 ;line horz spacing, goal=$range, $25 being 'center horx' or bottom 1/3 ot 1/4 of draw area
}
Gdip_DeletePen(pBrushRec)
Gdip_DeletePen(pBrushBG) ;snow-white bg color
Gdip_DeletePen(pPenBG) ;red border around bg color
Gdip_DeletePen(pPen1) ;black vert lines
Gdip_DeletePen(pPen2) ;black grid
;==========================================================================
SelectObject(hdc, obm) ; Select the object back into the hdc
DeleteObject(hbm) ; Now the bitmap may be deleted
DeleteDC(hdc) ; Also the device context related to the bitmap may be deleted
Gdip_DeleteGraphics(G) ; The graphics may now be deleted
Gdip_DeleteGraphics(G)
return pBitmap
}
;#######################################################################
;#######################################################################
kill:
GuiClose:
GuiEscape:
Exit:
Gdip_DisposeImage(pBitmapBackground)
SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
ExitApp
return
|
There is still obvisouly a lot of work to get done, but I was just so happy that it got to this point and had to say thank you. |
|
| Back to top |
|
 |
DeWild1
Joined: 30 Apr 2006 Posts: 358 Location: Shigle Springs
|
Posted: Wed Aug 04, 2010 9:57 pm Post subject: |
|
|
Please give me a little hope to see if I should still keep trying..
You see, I need to resize the canvas size, (After giving up on Gdip, i went the simple way.. ) | Code: |
Random, 1st, 1, 130
Random, 2nd, 1, 100
grr = image\i_view32.exe template.png /crop=(%1st%,%2nd%,1014,700) /convert=c:\temp\image%a_index%.gif
;Its in a loop
runwait, %grr%
| and save it as a gif to make the size a little smaller.
So, I am not sure if Gidp can do that. I think there was some talk about resizing but the canvas size is different..
Even if it can not, the reason I am asking for help today , is I want to grab the above image%a_index%.gif then write some text on each image.
I can pull the data like this, | Code: | FileReadLine, phones, phones.txt, %a_index%
FileReadLine, city, citys.txt, %a_index% |
I see it can write it on a GUI "Example 8: Gdip.Tutorial.8-Write.text.onto.a.gui.ahk" but I need it to load an image, write the text at xyz location.
Can it do that?
I am sorry I am so stu stu stupid, but this code gives me a headake!  _________________ CPULOCK.com
virusSWAT.com
Computer Repair Computer Service.com
911PCFIX.com |
|
| Back to top |
|
 |
DeWild1
Joined: 30 Apr 2006 Posts: 358 Location: Shigle Springs
|
|
| Back to top |
|
 |
Wicked
Joined: 07 Jun 2008 Posts: 369
|
Posted: Tue Aug 17, 2010 12:43 am Post subject: |
|
|
| DeWild1 wrote: | Please give me a little hope to see if I should still keep
I see it can write it on a GUI "Example 8: Gdip.Tutorial.8-Write.text.onto.a.gui.ahk" but I need it to load an image, write the text at xyz location.
Can it do that?
|
Do you mean:
| Code: | | Gdip_TextToGraphics(pGraphics, Text, Options, Font="Arial", Width="", Height="", Measure=0) |
|
|
| Back to top |
|
 |
DeWild1
Joined: 30 Apr 2006 Posts: 358 Location: Shigle Springs
|
Posted: Tue Aug 17, 2010 6:09 pm Post subject: |
|
|
| Wicked wrote: | | DeWild1 wrote: | Please give me a little hope to see if I should still keep
I see it can write it on a GUI "Example 8: Gdip.Tutorial.8-Write.text.onto.a.gui.ahk" but I need it to load an image, write the text at xyz location.
Can it do that?
|
Do you mean:
| Code: | | Gdip_TextToGraphics(pGraphics, Text, Options, Font="Arial", Width="", Height="", Measure=0) |
|
Someone PLEASE SHOOT ME!!!!!!!
I am sorry I am so staauuupid!
I have been at this for hours, (don't laugh)
OK, I have looked through Gdip.Tutorial.6-Image.Editing.ahk and I have taken what you said to try (thank you Wicked very much for trying to help me) and I went through the Gdip.Tutorial.8-Write.text.onto.a.gui.ahk and I am ashamed to even post, and I am sure I must have missed it when I read through all 20 or so pages here, but I think I just need to know how to load the image now..
I will re- read and IF I find the answer, I will edit this so as not to shame my self even MORE!
Sean & Tanks stuff is cake walk comaired to this stuff.
Well, for me at least.
| Code: | #SingleInstance, Force
#NoEnv
SetBatchLines, -1
; Uncomment if Gdip.ahk is not in your standard library
#Include, Gdip.ahk
file1 = test.png
pGraphics := Gdip_CreateBitmapFromFile(File1),
text = superman
Width = 100, Height = 100
Font = Arial
Options = x10p y30p w80p Centre cbbffffff r4 s20 Underline Italic
Gdip_TextToGraphics(pGraphics, Text, Options, Font, Width, Height)
Gdip_SaveBitmapToFile(pGraphics, "test1.png")
|
_________________ CPULOCK.com
virusSWAT.com
Computer Repair Computer Service.com
911PCFIX.com |
|
| Back to top |
|
 |
DeWild1
Joined: 30 Apr 2006 Posts: 358 Location: Shigle Springs
|
Posted: Tue Aug 17, 2010 6:54 pm Post subject: |
|
|
Well, i am getting it to save a file now..
But still no text.
| Code: | #SingleInstance, Force
#NoEnv
SetBatchLines, -1
; Uncomment if Gdip.ahk is not in your standard library
#Include, Gdip.ahk
pToken := Gdip_Startup()
file1 = test.png
pBitmap := Gdip_CreateBitmapFromFile(File1),
text = superman xcvdkvn vmsfkd lvsvlsvv sflv slf vlsf vsf vf sl lws vd sdvl s v s vs vlsd vlsdv dsv sdv sdv sdv sdv sdv
Width := Gdip_GetImageWidth(pBitmap), Height := Gdip_GetImageHeight(pBitmap)
Font = Arial
Options = x10p y30p w80p Center cbbffffff r4 s20 Underline Italic
Gdip_TextToGraphics(pBitmap, Text, Options, Font, Width, Height, Measure=0)
Gdip_SaveBitmapToFile(pBitmap, "test1.png")
|
_________________ CPULOCK.com
virusSWAT.com
Computer Repair Computer Service.com
911PCFIX.com |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Aug 17, 2010 7:41 pm Post subject: |
|
|
| Code: | #SingleInstance, Force
#NoEnv
SetBatchLines, -1
#Include, Gdip.ahk
File1 = test.png
If !pToken := Gdip_Startup()
{
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
ExitApp
}
pBitmapFile1 := Gdip_CreateBitmapFromFile(File1)
Width := Gdip_GetImageWidth(pBitmapFile1), Height := Gdip_GetImageHeight(pBitmapFile1)
msgbox ,,,%width% %height%,1
pBitmap := Gdip_CreateBitmap(width, height)
G := Gdip_GraphicsFromImage(pBitmap)
Gdip_DrawImage(G, pBitmapFile1, 0, 0, Width, Height, 0, 0, Width, Height)
text = superman xcvdkvn vmsfkd
Font = Arial
Options = x10p y30p w80p Center cbbffffff r4 s20 Underline Italic
Gdip_TextToGraphics(G, text, Options, Font, Width, Height,Measure=0)
Gdip_SaveBitmapToFile(pBitmap, "FinalImage.png")
Gdip_DisposeImage(pBitmapFile1)
Gdip_DisposeImage(pBitmap)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
ExitApp
Return |
|
|
| Back to top |
|
 |
DeWild1
Joined: 30 Apr 2006 Posts: 358 Location: Shigle Springs
|
Posted: Tue Aug 17, 2010 7:47 pm Post subject: |
|
|
| Anonymous wrote: | | Code: | #SingleInstance, Force
#NoEnv
SetBatchLines, -1
#Include, Gdip.ahk
File1 = test.png
If !pToken := Gdip_Startup()
{
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
ExitApp
}
pBitmapFile1 := Gdip_CreateBitmapFromFile(File1)
Width := Gdip_GetImageWidth(pBitmapFile1), Height := Gdip_GetImageHeight(pBitmapFile1)
msgbox ,,,%width% %height%,1
pBitmap := Gdip_CreateBitmap(width, height)
G := Gdip_GraphicsFromImage(pBitmap)
Gdip_DrawImage(G, pBitmapFile1, 0, 0, Width, Height, 0, 0, Width, Height)
text = superman xcvdkvn vmsfkd
Font = Arial
Options = x10p y30p w80p Center cbbffffff r4 s20 Underline Italic
Gdip_TextToGraphics(G, text, Options, Font, Width, Height,Measure=0)
Gdip_SaveBitmapToFile(pBitmap, "FinalImage.png")
Gdip_DisposeImage(pBitmapFile1)
Gdip_DisposeImage(pBitmap)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
ExitApp
Return |
|
THANK YOU!!
I think I was seconds from doing it myself, from another example, http://www.autohotkey.com/forum/viewtopic.php?p=218521#218521 but you have really helped me learn this stuff with your code!
THANK YOU!! _________________ CPULOCK.com
virusSWAT.com
Computer Repair Computer Service.com
911PCFIX.com |
|
| Back to top |
|
 |
SifJar
Joined: 13 Feb 2010 Posts: 170
|
Posted: Thu Aug 19, 2010 12:34 pm Post subject: |
|
|
I'm not sure if here is definitely the right place to put this, but here goes anyway:
I have been trying to write some text on an image and save it, and it is working fine, except I can't get it to work with the font I want to use. Its a font called "Liberation Sans" and is downloadable from here: https://fedorahosted.org/liberation-fonts/
This is the code I have:
| Code: | ; We can specify the font to use. Here we use Arial as most systems should have this installed
Font := "Liberation Sans"
;Write the text
Gdip_TextToGraphics(G, "Lorem Ipsum", "x160 y70 s20", Font) |
If I make it Arial it works fine, but I want to use that font. Anyone have any ideas how to make if work?
EDIT: Turns out it wasn't installed right. My bad. |
|
| Back to top |
|
 |
SifJar
Joined: 13 Feb 2010 Posts: 170
|
Posted: Sat Aug 21, 2010 9:27 pm Post subject: |
|
|
Sorry for double post, but I have another problem and wanted to make sure it got seen.
Anyway, I'm trying to display an image generated by my script in a window, but I am struggling to make it work. Here's my code, which I have adapted from the 9th tutorial:
| Code: | Preview:
GoSub Generate
;Display
Gui, 2: Add, Picture, x0 y0 w640 h480 0xE vPreviewImg
Gui, 2: Add, Button, x170 y490 w300 h40 , Save
; Generated using SmartGUI Creator 4.0
GuiControlGet, Pos, Pos, PreviewImg
GuiControlGet, hwnd, hwnd, PreviewImg
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
SetImage(hwnd, hBitmap)
Gui, 2: Show, w640 h540, Preview
Return |
The subroutine "Generate" definitely works and produces a bitmap called "pBitmap", which I can save, but I can't get it to preview. Anyone tell me what I'm doing wrong? |
|
| Back to top |
|
 |
Guest
|
Posted: Sun Aug 22, 2010 11:11 am Post subject: |
|
|
Had to add some code to get a test bitmap.
| Code: | #SingleInstance, Force
#NoEnv
SetBatchLines, -1
onexit close_me
#Include, Gdip.ahk
File1 = test.png
If !pToken := Gdip_Startup()
{
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
ExitApp
}
#Include, Gdip.ahk
pBitmap := Gdip_CreateBitmapFromFile(File1)
Width := Gdip_GetImageWidth(pBitmap), Height := Gdip_GetImageHeight(pBitmap)
msgbox,,,%width% %height%,1
Gui, 2: Add, Picture, x0 y0 w640 h480 0xE Hwndimage_handle
Gui, 2: Add, Button, x170 y490 w300 h40 , Save
Gui, 2: Show, w640 h540, Preview
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
SetImage(image_handle, hBitmap)
Return
close_me:
guiclose:
Gdip_DisposeImage(pBitmap)
DeleteObject(hBitmap)
Gdip_Shutdown(pToken)
ExitApp
Return |
|
|
| Back to top |
|
 |
SifJar
Joined: 13 Feb 2010 Posts: 170
|
Posted: Sun Aug 22, 2010 10:00 pm Post subject: |
|
|
| Thank you, got it working now. |
|
| Back to top |
|
 |
Big Digger
Joined: 07 Feb 2009 Posts: 43
|
Posted: Wed Aug 25, 2010 4:20 am Post subject: |
|
|
tic
it is possible to make this library compatible with AHK_L x64? |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|