[SOLVED] How to create a 1x1 semi-transparent png?
[SOLVED] How to create a 1x1 semi-transparent png?
How to create a 1x1 semi-transparent png (or hBitmap), like this website?
Last edited by tmplinshi on 01 Apr 2014, 00:22, edited 1 time in total.
- LinearSpoon
- Posts: 156
- Joined: 29 Sep 2013, 22:55
Re: How to create a 1x1 semi-transparent png?
Pretty easy with gdip.ahk
Code: Select all
#Include gdip.ahk
pToken := Gdip_startup()
pBitmap := GetTransparentBitmap(0xFF4D00, 0.8)
Gdip_SaveBitmapToFile(pBitmap, "trans.png")
Gdip_shutdown(pToken)
;Accepts color in the form of 0xRRGGBB and transparency as a float in the range [0,1]
;Gdip must be started with Gdip_startup() for this function to work
GetTransparentBitmap(color, transparency)
{
pBitmap := Gdip_CreateBitmap(1,1)
Gdip_SetPixel(pBitmap, 0, 0, floor(255*transparency) << 24 | color)
return pBitmap
}
Re: How to create a 1x1 semi-transparent png?
Thank you! This transparent png can be used in many places. Examples:
Example 1
Example 2
Re: How to create a 1x1 semi-transparent png?
Here is a simple function without need gdip.ahk . Code takes from LinearSpoon's help, and just me's Image2Include.ahk
I'm confused why the color not same as the web page. I'm using the same color and transparency as the web page.
I also checked out the image generated by http://www.1x1px.me/, Gui, Add, Pic, xp yp+315 w500 h60 BackgroundTrans, 2F93D0-0.506.png produces the same result as SetTransparentImage. Maybe this is the difference between GDI and web browser...
Edit: Tested image created by ImageMagick too, also the same result. (The command line code is: convert -size 500x60 xc:#2F93D081 im.png)
Code: Select all
SetTransparentImage(hwnd, color = 0x000000, transparency = 0.5) {
hGdip := DllCall("Kernel32.dll\LoadLibrary", "Str", "Gdiplus.dll", "UPtr")
VarSetCapacity(SI, 16, 0), NumPut(1, SI, 0, "UChar")
DllCall("Gdiplus.dll\GdiplusStartup", "PtrP", pToken, "Ptr", &SI, "Ptr", 0)
DllCall("Gdiplus.dll\GdipCreateBitmapFromScan0", "int", 1, "int", 1, "int", 0, "int", 0x26200A, A_PtrSize ? "UPtr" : "UInt", 0, A_PtrSize ? "UPtr*" : "uint*", pBitmap)
DllCall("Gdiplus.dll\GdipBitmapSetPixel", A_PtrSize ? "UPtr" : "UInt", pBitmap, "int", 0, "int", 0, "int", floor(255*transparency) << 24 | color)
DllCall("Gdiplus.dll\GdipCreateHBITMAPFromBitmap", "Ptr", pBitmap, "PtrP", hBitmap, "UInt", 0)
DllCall("Gdiplus.dll\GdipDisposeImage", "Ptr", pBitmap)
DllCall("Gdiplus.dll\GdiplusShutdown", "Ptr", pToken)
DllCall("Kernel32.dll\FreeLibrary", "Ptr", hGdip)
GuiControlGet, ctrl, Pos, %hwnd%
hBitmap := DllCall("CopyImage", Ptr,hBitmap, UInt,0, Int,ctrlW, Int,ctrlH, UInt,0x00000008, Ptr)
SendMessage, (STM_SETIMAGE:=0x172), (IMAGE_BITMAP:=0x0), HBITMAP,, ahk_id %hwnd%
Return hBitmap
}
I also checked out the image generated by http://www.1x1px.me/, Gui, Add, Pic, xp yp+315 w500 h60 BackgroundTrans, 2F93D0-0.506.png produces the same result as SetTransparentImage. Maybe this is the difference between GDI and web browser...
Edit: Tested image created by ImageMagick too, also the same result. (The command line code is: convert -size 500x60 xc:#2F93D081 im.png)
Code
Re: How to create a 1x1 semi-transparent png?
Hi tmplinshi, you might want to try this, it's based on tic's gdi+ ahk tutorials.
Didn't look at the code too close, there may be some that can be removed.
I took out all his fine comments, because they would be out of sequence.
Also I think I stole enough. It uses your sharing-ice-cream-cone.jpg
Keep up the good work.
hunter99
Didn't look at the code too close, there may be some that can be removed.
I took out all his fine comments, because they would be out of sequence.
Also I think I stole enough. It uses your sharing-ice-cream-cone.jpg
Keep up the good work.
hunter99
Code: Select all
; Based on code from http://www.autohotkey.com/board/topic/29449-gdi-standard-library-145-by-tic/
; gdi+ ahk tutorials written by tic (Tariq Porter)
; Requires Gdip.ahk either in your Lib folder as standard library or using #Include
; Combo of tic's tutorials #3 and #8 cobbled together by hunter99 8:38 PM 3/31/2014
; Esc key exits. Left click and hold on image, you can drag it. Release beings msgbox.
#SingleInstance, Force
#NoEnv
SetBatchLines, -1
; Uncomment if Gdip.ahk is not in your standard library
;#Include, Gdip.ahk
; Start gdi+
If !pToken := Gdip_Startup()
{
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
ExitApp
}
OnExit, Exit
Gui, 1: -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs
Gui, 1: Show, NA
hwnd1 := WinExist()
pBitmap := Gdip_CreateBitmapFromFile("sharing-ice-cream-cone.jpg")
If !pBitmap
{
MsgBox, 48, File loading error!, Could not load the image specified
ExitApp
}
Width := Gdip_GetImageWidth(pBitmap), Height := Gdip_GetImageHeight(pBitmap)
hbm := CreateDIBSection(Width, Height)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)
Gdip_SetInterpolationMode(G, 7)
Gdip_DrawImage(G, pBitmap, 0, 0, Width, Height, 0, 0, Width, Height)
UpdateLayeredWindow(hwnd1, hdc, (A_ScreenWidth-Width), (A_ScreenHeight-Height), Width//2, Height//2)
Gdip_SetSmoothingMode(G, 4)
Font = Arial
If !Gdip_FontFamilyCreate(Font)
{
MsgBox, 48, Font error!, The font you have specified does not exist on the system
ExitApp
}
Options = x10p y90p w80p Centre cdcffffff r4 s32 Underline Italic Bold
Gdip_TextToGraphics(G, "Sharing ice cream", Options, Font, Width, Height)
UpdateLayeredWindow(hwnd1, hdc, (A_ScreenWidth-Width)//2, (A_ScreenHeight-Height)//2, Width, Height)
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
OnMessage(0x201, "do_your_stuff")
do_your_stuff()
{
PostMessage, 0xA1, 2
MsgBox, 4096, , Put your stuff to do Here!
}
return
;#######################################################################
Esc::
Exit:
Gdip_Shutdown(pToken)
ExitApp
Return
Re: How to create a 1x1 semi-transparent png?
Thanks hunter99, your post reminds me to try display image with GDI, and finally got the same effect as the web page. Unfortunately the image displayed in a standalone window. Seems a long way to research, and beyond my needs, so I'll not go further.
Code: Select all
#include <gdip>
Gui, -Caption +E0x80000 +AlwaysOnTop +Hwndhwnd
Gui, Show, NA
pToken := Gdip_Startup()
sFile := "2F93D081_500x60.png" ; Created by ImageMagick command (convert -size 500x60 xc:#2F93D081 2F93D081_500x60.png)
pBitmap:=Gdip_CreateBitmapFromFile(sFile)
Gdip_GetDimensions(pBitmap, w, h)
hbm := CreateDIBSection(w,h)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
pGraphics := Gdip_GraphicsFromHDC(hdc)
Gdip_DrawImage(pGraphics, pBitmap,0,0,w,h)
UpdateLayeredWindow(hwnd, hdc, (A_ScreenWidth-w)//2, (A_ScreenHeight-h)//2, w,h)
OnMessage(0x201, "WM_LBUTTONDOWN")
return
GuiEscape:
GuiClose:
ExitApp
WM_LBUTTONDOWN() {
PostMessage, 0xA1, 2
}
Who is online
Users browsing this forum: peter_ahk, Spawnova, toddhere, USS_Sandhu and 297 guests