Liberty BASIC Community Forum
« Save an image as PNG/JPEG without extra DLL's »

Welcome Guest. Please Login or Register.
Nov 17th, 2017, 1:26pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Save an image as PNG/JPEG without extra DLL's  (Read 411 times)
Dan Teel
Guru
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1130
xx Save an image as PNG/JPEG without extra DLL's
« Thread started on: Feb 29th, 2016, 02:28am »

Run this in LB's default directory and it will save the toolbar3.bmp as the png, jpeg, and gif formats in the same directory to demonstrate.

Excuse the mess, but its already quite hacky, so I'm not worried about looks.

Code:
loadbmp "bitmap","bmp\Toolbar3.bmp"

if SaveHBITMAPAsType(hbmp("bitmap"),"bmp\Toolbar3.png","image/png") then
    print "Successfully saved Toolbar3.bmp as Toolbar3.png inside the bitmap folder!"
else
    print "Something went wrong, sorry bud."
end if

if SaveHBITMAPAsType(hbmp("bitmap"),"bmp\Toolbar3.jpeg","image/jpeg") then
    print "Successfully saved Toolbar3.bmp as Toolbar3.jpeg inside the bitmap folder!"
else
    print "Something went wrong, sorry bud."
end if

if SaveHBITMAPAsType(hbmp("bitmap"),"bmp\Toolbar3.gif","image/gif") then
    print "Successfully saved Toolbar3.bmp as Toolbar3.gif inside the bitmap folder!"
else
    print "Something went wrong, sorry bud."
end if

unloadbmp "bitmap"

end


function GetEncoderClsid(format$)'fills the CLSID struct with the encoderclsid, returns 1 if successful
    struct numEncoders,a as ulong
    struct encoderBuffSize,a as ulong
    struct mimeTypeAddy,a as ulong
    calldll #gdip,"GdipGetImageEncodersSize",numEncoders as struct,encoderBuffSize as struct,status as ulong
    if encoderBuffSize.a.struct=0 then exit function
    imageCodecInfo=HeapAlloc(encoderBuffSize.a.struct)
    if imageCodecInfo then
        num=numEncoders.a.struct
        size=encoderBuffSize.a.struct
        calldll #gdip,"GdipGetImageEncoders",num as ulong,_
            size as ulong,imageCodecInfo as ulong,_
            status as ulong
        if status=0 then'our buffer was filled with ImageCodecInfo objects
            for i = 1 to num
                addy=imageCodecInfo+(i-1)*76+48
                calldll #kernel32,"RtlMoveMemory",mimeTypeAddy as struct,addy as ulong,4 as ulong,ret as void
                if trim$(lower$(getStrFromWideAddy$(mimeTypeAddy.a.struct,len(format$))))=lower$(trim$(format$)) then
                    'Fill the cls buff
                    addy=imageCodecInfo+(i-1)*76
                    calldll #kernel32,"RtlMoveMemory",CLSID as struct,addy as ulong,16 as ulong,ret as void
                    GetEncoderClsid=1
                end if
            next i

        end if
        call HeapFree imageCodecInfo
    end if
end function

function SaveHBITMAPAsType(hbitmap,file$,type$)
    struct ret,a as ulong
    struct gdiplustoken,token as ulong
    struct CLSID,data1 as ulong,data2 as ulong,data3 as ulong,data4 as ulong
    open "gdiplus.dll" for dll as #gdip
    gdistart$=chr$(1)
    for i = 1 to 15
        gdistart$=gdistart$+chr$(0)
    next i
    calldll #gdip,"GdiplusStartup",gdiplustoken as struct,gdistart$ as ptr,status as ulong
    if status<>0 then
        close #gdip
        exit function
    end if
    if GetEncoderClsid(type$) then
        calldll #gdip,"GdipCreateBitmapFromHBITMAP",hbitmap as ulong,0 as ulong,_
            ret as struct,status as ulong
        gdipBitmap=ret.a.struct
        if status=0 then'looking good so far
            file$=wchar$(file$)
            calldll #gdip,"GdipSaveImageToFile",_
                gdipBitmap as ulong,_
                file$ as ptr,_
                CLSID as struct,_
                0 as ulong,_
                status as ulong
            if status=0 then SaveHBITMAPAsType=1
            calldll #gdip,"GdipDisposeImage",gdipBitmap as ulong,ret as ulong
        end if
    end if
    calldll #gdip,"GdiplusShutdown",gdiplustoken.token.struct as ulong,ret as ulong
    close #gdip
end function

function wchar$(string$)
    for i = 1 to len(string$)
        wchar$=wchar$+mid$(string$,i,1)+chr$(0)
    next i
    wchar$=wchar$+chr$(0)+chr$(0)
end function

function getStrFromWideAddy$(addy,maxLength)
    while strLen<maxLength
        temp$=space$(2)
        calldll #kernel32,"RtlMoveMemory",temp$ as ptr,addy as ulong,2 as ulong,ret as void
        if mid$(temp$,1,1)=chr$(0) then exit while
        getStrFromWideAddy$=getStrFromWideAddy$+mid$(temp$,1,1)
        strLen=strLen+1
        addy=addy+2
    wend
end function

sub HeapFree mem
    if mem=0 then exit sub
    calldll #kernel32,"GetProcessHeap",heap as ulong
    calldll #kernel32,"HeapFree",heap as ulong,0 as ulong,_
        mem as ulong,HeapFree as ulong
end sub

function HeapAlloc(sz)
    if sz<1 then exit function
    calldll #kernel32,"GetProcessHeap",heap as ulong
    calldll #kernel32,"HeapAlloc",heap as ulong,8 as ulong,_
        sz as ulong,HeapAlloc as ulong
end function
 
User IP Logged

ZPtr.net
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls