Results 1 to 4 of 4

Thread: Convert hBitmap to hImage with GDI, it is possible?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Convert hBitmap to hImage with GDI, it is possible?

    Hello everyone,

    I would like to know if hBitmap can be passed to hImage.

    Let me explain, I am making a program that displays the covers extracted from the ITAG metadata of mp3 music files in a Picturebox.

    In ‘Sub DrawImage (…)’, is where the images are loaded.

    At this point I thought, well ... if I already have the image loaded in the variable 'Cover' type 'hBitmap' as a variable 'Publish' for the whole program, why not take advantage of it and not have to save it to a JPG file and then in a DrawImage () program line in 'GdipLoadImageFromFile (StrPtr (FileName), hImage)' have to load it back into variable 'FileName' and process the image 2 times.

    Here's the problem, once the library 'GdipLoadImageFromFile' loads the temporary jpg file, it returns it as 'hImage'.

    I think that in order not to waste so much time in this process and the images are loaded faster in the control there may be another solution.
    I've been looking at libraries and there's one called 'GdipLoadImageFromStream (…) I don't know if it could work.

    I hope you have explained me well and can help me.

    This is what I am doing so far and it works, but could it be improved with the aforementioned ???

    Code:
    Private Sub DrawImage(hGraphics As Long, Index As Long, FileName As String, DestTR As RECTF)
    
        Dim TR As RECTF
        Dim Conver As Long
        Dim hImage As Long
        Dim ImagPlayMow As Long
        Dim PLeft As Long, PTop As Long
        Dim ReqWidth As Long, ReqHeight As Long
        Dim HScale As Double, VScale As Double
        Dim MyScale As Double
        Dim ImgWidth As Long
        Dim ImgHeight As Long
        Dim SourceHDC As Long
        Dim TempFile As String
         
    If mLoadCover = FromMusicFile Then
        
        TempFile = App.Path & "\tmpCover.jpg"
        Call LoadTag(FileName)                'Calls the object that extracts the album art from the MP3 and saves it in the variable 'Cover' = hBitmap
        Call SavePicture(Cover, TempFile) 'Save the image of the variable 'Cover' in the temporary file' tmpCover.jpg '
        FileName = TempFile
    End If
    
    
    If GdipLoadImageFromFile(StrPtr(FileName), hImage) = 0 Then   'Open the temporary jpg  image.
    
        Call GdipGetImageBounds(hhImage, TR, UnitPixel)
            
            ImgWidth = TR.nWidth
            ImgHeight = TR.nHeight
            
            ArrDimensions(Index) = ImgWidth & " x " & ImgHeight
     
            If ImgWidth > DestTR.nWidth Or ImgHeight > DestTR.nHeight Then
                HScale = DestTR.nWidth / ImgWidth
                VScale = DestTR.nHeight / ImgHeight
            
                MyScale = IIf(VScale >= HScale, HScale, VScale)
                
                ReqWidth = ImgWidth * MyScale
                ReqHeight = ImgHeight * MyScale
        
                PLeft = DestTR.nLeft
                PTop = DestTR.nTop + 1
            Else
                ReqWidth = ImgWidth
                ReqHeight = ImgHeight
                PLeft = DestTR.nLeft
                PTop = DestTR.nTop + 1
            End If
        
            ReqWidth = ItemHeight - 10
            ReqHeight = ItemHeight - 10
          
             GdipDrawImageRectRectI hGraphics, hhImage, PLeft, PTop, ReqWidth, ReqHeight, 0, 0, ImgWidth, ImgHeight, UnitPixel, 0&, 0&, 0&
            Call GdipDisposeImage(hhImage)
    End If
            
    End Sub
    Thanks.

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,152

    Re: Convert hBitmap to hImage with GDI, it is possible?

    Quote Originally Posted by Lompin View Post
    if I already have the image loaded in the variable 'Cover' type 'hBitmap' as a variable 'Publish' for the whole program
    No, your global Cover variable is not of type hBitmap, it must be an StdPicture to be able to call SavePicture the way you do in your snippet.

    You can use GdipCreateBitmapFromHBITMAP with Cover.Handle and Cover.hPal to directly convert your StdPicture to a GDI+ image handle -- no need to save to temporary file at all.

    cheers,
    </wqw>

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Re: Convert hBitmap to hImage with GDI, it is possible?

    Thank you wqweto for answering,

    I have modified the program but there is a problem added:
    The routine 'Sub DrawImage (...)' is inside another loop that calls the same library 'GdiplusStartup ()' that was previously started with other variables .
    When is called the new subroutine 'InitGDIBitMap ()' the vb6 crashes and closes completely.

    Extructure Exemple:

    Sub Draw(...)

    Ini GDI (First Call to 'GdiplusStartup' )

    Sub DrawImage(...)
    Ini GDI (Second Call to 'GdiplusStartup' )
    ....... Code lines.
    End GDI

    End Sub
    End GDI

    End Sub



    Here new code:

    Code:
    Private Declare Function GdipCreateBitmapFromHBITMAP Lib "gdiplus" (ByVal hbm As Long, ByVal hPal As Long, bitmap As Long) As Long
    
    Private GdiToken As Long
    Private lRes        As Long  'for BitMap GDI+ 'Sub DrawImage (...)'
    
    Sub InitGDIBitMap()
    
    Dim Gsp As GDIPlusStartupInput
    Dim lBitmap As Long
     
     ' Initialize GDI+
      Gsp.GdiPlusVersion = 1
      lRes = GdiplusStartup(GdiToken, Gsp)
        
     
     If lRes = 0 Then
       ' Create the GDI+ bitmap
       ' from the image handle
      lRes = GdipCreateBitmapFromHBITMAP(Cover.Handle, Cover.hPal, lBitmap)
    End If
    End Sub
    
    
    
    Private Sub DrawImage(hGraphics As Long, Index As Long, FileName As String, DestTR As RECTF)
    
        Dim TR As RECTF
        Dim Conver As Long
        Dim hImage As Long
        Dim ImagPlayMow As Long
        Dim PLeft As Long, PTop As Long
        Dim ReqWidth As Long, ReqHeight As Long
        Dim HScale As Double, VScale As Double
        Dim MyScale As Double
        Dim ImgWidth As Long
        Dim ImgHeight As Long
        Dim SourceHDC As Long
        Dim TempFile As String
         
        Call LoadTag(FileName)   'Calls the object that extracts the Cover art from the MP3 and saves it in the variable 'Cover' = hBitmap
        InitGDIBitMap                 'Call  InitGDIBitMap from 'Cover'
    
        Call GdipGetImageBounds(lRes, TR, UnitPixel)
            
            ImgWidth = TR.nWidth
            ImgHeight = TR.nHeight
            
            ArrDimensions(Index) = ImgWidth & " x " & ImgHeight
     
            If ImgWidth > DestTR.nWidth Or ImgHeight > DestTR.nHeight Then
                HScale = DestTR.nWidth / ImgWidth
                VScale = DestTR.nHeight / ImgHeight
            
                MyScale = IIf(VScale >= HScale, HScale, VScale)
                
                ReqWidth = ImgWidth * MyScale
                ReqHeight = ImgHeight * MyScale
        
                PLeft = DestTR.nLeft
                PTop = DestTR.nTop + 1
            Else
                ReqWidth = ImgWidth
                ReqHeight = ImgHeight
                PLeft = DestTR.nLeft
                PTop = DestTR.nTop + 1
            End If
        
            ReqWidth = ItemHeight - 10
            ReqHeight = ItemHeight - 10
          
             GdipDrawImageRectRectI hGraphics, lRes, PLeft, PTop, ReqWidth, ReqHeight, 0, 0, ImgWidth, ImgHeight, UnitPixel, 0&, 0&, 0&
            Call GdipDisposeImage(lRes)
    End If
    
       GdiplusShutdown GdiToken
            
    End Sub
    Cheers,
    Last edited by Lompin; Feb 20th, 2021 at 06:17 AM.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Re: Convert hBitmap to hImage with GDI, it is possible?

    Thanks a lot,

    Solved .

    Change 'GdiplusStartup' site and change 'lRes' to 'lBitmap' and ready.

    Greetings.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width