Jump to content

Read Binary Image Trasparent Format .bmp


bub
 Share

Recommended Posts

SCRIPT

#include <StaticConstants.au3>
#include <GUIConstants.au3>
#include "ImgBin.au3"

GUICreate('Image from Stream smile.gif')
GUICtrlCreatePic(@ScriptDir&"\my.bmp", 0,100,330,90)

$Pic = GUICtrlCreatePic("", 0,0,330,90)
$Image = _File(@ScriptDir&"\my.bmp")
_SetImageBinaryToCtrl($Pic,$Image)


GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
WEnd
Func _File($File_R)
Return FileRead(FileOpen($File_R,0))
EndFunc

UDF

Func _SetImageBinaryToCtrl($CtrlId, ByRef $Binary)
    Local $picdata = Binary($Binary) ; Fetch the Data
    Local $piclength = BinaryLen($picdata) ; Get Length

    Local $picstruct = DllStructCreate("byte[" & $piclength & "]")
        DllStructSetData($picstruct,1,$picdata)
        Local $picmemory = DllStructGetPtr($picstruct)
        _SetMemoryImageToCtrl($CtrlId, $picmemory, $piclength)
        DllStructSetData($picstruct,1,0)
    $picstruct = ""
EndFunc

; Authors: Zedna, based on code by Prog@ndy
Func _SetMemoryImageToCtrl($CtrlId, $Pointer, $nSize)
    Local $hData, $pData, $pStream, $pBitmap, $hBitmap

        ; use GDI+ for converting to bitmap first
    $hData = _MemGlobalAlloc($nSize,2)
    $pData = _MemGlobalLock($hData)
    _MemMoveMemory($Pointer,$pData,$nSize)
    _MemGlobalUnlock($hData)
    $pStream = DllCall( "ole32.dll","int","CreateStreamOnHGlobal", "int",$hData, "long",1, "Int*",0)
    $pStream = $pStream[3]

    _GDIPlus_Startup()
    $pBitmap = DllCall($ghGDIPDll,"int","GdipCreateBitmapFromStream", "ptr",$pStream, "int*",0)
    $pBitmap = $pBitmap[2]
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($pBitmap)
    _SetBitmapToCtrl($CtrlId, $hBitmap)
    If @error Then SetError(3, 0, 0)
    _GDIPlus_BitmapDispose($pBitmap)
    _GDIPlus_Shutdown()
    _WinAPI_DeleteObject($pStream)
    _MemGlobalFree($hData)
EndFunc

; internal helper function
; Out of resources.au3 :)
Func _SetBitmapToCtrl($CtrlId, $hBitmap)
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $IMAGE_BITMAP = 0
    Local Const $SS_BITMAP = 0xE
    Local Const $GWL_STYLE = -16

    Local $hWnd = GUICtrlGetHandle($CtrlId)
    If $hWnd = 0 Then Return SetError(1, 0, 0)
        ; set SS_BITMAP style to control
    Local $oldStyle = DllCall("user32.dll", "long", "GetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE)
    If @error Then Return SetError(2, 0, 0)
    DllCall("user32.dll", "long", "SetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE, "long", BitOR($oldStyle[0], $SS_BITMAP))
    If @error Then Return SetError(3, 0, 0)
        Local $oldBmp = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hWnd, "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hBitmap)
    If @error Then Return SetError(4, 0, 0)
    If $oldBmp[0] <> 0 Then _WinAPI_DeleteObject($oldBmp[0])
    Return 1
endfunc

Posted Image

look at the image, because the transparency of the BMP is canceled?

is possible with _SetImageBinaryToCtrl have the same result?

It may be the conversion GDIplus?

Excuse my English, I'm Italian

Link to comment
Share on other sites

Try this to display a transparent image in a GUI:

#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
Opt("GuiOnEventMode", 1)

Local Const $STM_SETIMAGE = 0x0172
Local $hGUI, $Pic, $hImage, $hBmp, $iW, $iH

_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile("x.png")
$iW = _GDIPlus_ImageGetWidth($hImage)
$iH = _GDIPlus_ImageGetHeight($hImage)
$hBitmap = _GDIPlus_BitmapCloneArea($hImage, 0, 0, $iW, $iH, $GDIP_PXF32ARGB)
$hBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)

_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()

$hGUI = GUICreate("Test", $iW * 2, $iH * 2)
GUISetBkColor(0xF0F0F0)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")


$Pic = GUICtrlCreatePic("", $iW / 2, $iH / 2, $iW, $iH)
GUICtrlSendMsg($Pic, $STM_SETIMAGE, 0, $hBmp)
GUICtrlSetOnEvent($Pic, "_Pic_Clicked")

_WinAPI_DeleteObject($hBmp)

GUISetState()

Do
Until Not Sleep(1000)

Func _Pic_Clicked()
    MsgBox(0, "Test", "You clicked on the pic!")
EndFunc

Func _Quit()
    GUIDelete($hGUI)
    Exit
EndFunc

Use only transparent PNGs or GIFs!

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

I need to use _SetImageBinaryToCtrl

Do not create the handle of image from a file, but from his "text".

If it were possible to use transparent PNGs image format, that's okay, but I only ever use _SetImageBinaryToCtrl!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...