Convert byte string to Image Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
DevWithCoffee
Posts: 54
Joined: 13 Oct 2020, 12:16

Convert byte string to Image

04 Jan 2024, 16:45

Is it possible to convert raw bytes to image, preferably PNG? I found it in Python, but I don't think it's convenient to link another language just for this task.
https://www.reddit.com/r/learnpython/comments/ovv5hq/how_to_convert_byte_string_to_image/
User avatar
DevWithCoffee
Posts: 54
Joined: 13 Oct 2020, 12:16

Re: Convert byte string to Image

04 Jan 2024, 19:31

I had already taken a screenshot, but the player can create a blur effect in the video, so I intended to extract it directly from memory:

Code: Select all

StartSnapshot_old:
if(ChkScreen <> 1)
{
	if(!FileExist("Screenshots"))
	{
		FileCreateDir, Screenshots
	}
	WinGet, VidPlayerHWND, ID, % "ahk_pid" VidPlayerPID
	sFile := A_WorkingDir "\ScreenShots\" A_YYYY A_MM A_DD A_Hour A_Min A_Sec A_MSec ".png"
	pBmpHWND := Gdip_BitmapFromHwnd3(VidPlayerHWND, 1)
	pBitmap := Gdip_CreateBitmap(320, 160)
	G := Gdip_GraphicsFromImage(pBitmap)
	Gdip_SetInterpolationMode(G, 5) ; Fix the image before resizing
	Gdip_DrawImage(G, pBmpHWND, 0, 0, 320, 160, 0, 0, 320, 160)
	Gdip_DisposeImage(pBmpHWND)
	Gdip_BitmapSetColorDepth(pBitmap, 24, 0)
	Gdip_SaveBitmapToFile(pBitmap, sFile)
	Gdip_DisposeImage(pBitmap)
	Gdip_DeleteGraphics(G)
	FileGetSize, PicSize , %sFile%
	if(!FileExist(sFile) || PicSize < 221)
	{
		; Old version (WIN7)
		pBitmap := Gdip_BitmapFromHwnd3(VidPlayerHWND, 1)
		pBitmap := Gdip_CloneBitmapArea(pBitmap, 0, 0, 320, 160)
		Gdip_BitmapSetColorDepth(pBitmap, 24, 0)
		Gdip_SaveBitmapToFile(pBitmap, sFile)
		Gdip_DisposeImage(pBitmap)
	}
}
return
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: Convert byte string to Image

05 Jan 2024, 00:38

I do not have the answer to that one. Others may. I suspect that someone posted about this previously.
User avatar
DevWithCoffee
Posts: 54
Joined: 13 Oct 2020, 12:16

Re: Convert byte string to Image  Topic is solved

06 Jan 2024, 21:20

Unfortunately, this was a lack of attention on my part regarding the functions of GDI+...
I was using Gdip_FillRectangle to make each pixel and for that I had to update each "Gdip_BrushCreateSolid" with the color that would be used in the coordinate... making the process take a long time.

The correct function for drawing pixels is Gdip_SetPixel, so as not to need to convert each Byte into ARGB within the Loop, I created a list of 256 colors (0~255) in ARGB format, with each line being the address for each color.

I tested it on a 1.10 GHz processor and the result was practically instantaneous, in this case I used a low resolution video to test (See example).
then I tested it on a 1920x1080 video, which varied between 9 and 11 seconds depending on the other Windows processes.

Code: Select all

#SingleInstance, Force
SetBatchLines, -1
#include Gdip_All.ahk
If !pToken := Gdip_Startup()
{
	MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
	ExitApp
}
_w := 800
_h := 600
pBitmap := Gdip_CreateBitmap(_w, _h)
G := Gdip_GraphicsFromImage(pBitmap)
; Drawn background
pBrush := Gdip_BrushCreateSolid(0xff000000)
Gdip_FillRectangle(G, pBrush, 0, 0, _w, _h)
px := 0
Loop, % _w
{
	_x := A_Index
	Loop, % _h
	{
		px := (A_index*_x)
		Random, AnyRGB, 1, 16777215
		Gdip_SetPixel(pBitmap, (_x-1), (A_Index-1), "0xFF" AnyRGB)
	}
}
MsgBox,, Finished, % pos " drawn pixels in total"
Gdip_DeleteBrush(pBrush)
Gdip_SaveBitmapToFile(pBitmap, "Raw_To_PNG.png")
ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 76 guests