dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Cannot load a bitmap from CLongBinary object

  1. #1
    Join Date
    Nov 1999
    Posts
    270

    Cannot load a bitmap from CLongBinary object

    I am trying to load a CBitmap object from the CLongBinary data member that is created by the AppWizard in the CDaoRecordView class to accommodate a bitmap object embedded in a MS Access .mdb file.

    I have tried CBitmap::SetBitmapBits() and CBitmap::LoadBitmap() in every way I could think of with no luck. The documentation says the data from this type of object will be loaded into the CLongBinary object, in the form of a HGLOBAL member, m_hData.

    How do I load this thing???

    Thanks a lot for any ideas!


  2. #2
    Join Date
    Nov 1999
    Location
    Dresden / Germoney
    Posts
    1,402

    Re: Cannot load a bitmap from CLongBinary object

    How did you "embed" it?

    ---------- Access

    If you did it embed want to do it from in MS Access, you have probably inserted an OLE Object ("Insert Object/.."). In this case, the BLOB data contains the servers CLSID, and the servers "state", which includes the bitmap.
    You can only run the OLE Object Server again and use the server's interface (e.g. MS Paint, Imaging) to work with the bitmap.
    Alternatively, you could write your own little server that lets you insert the bitmap in a known format.

    To awaken the server, you use OleLoad(IStorage *,), or OleLoadFromStream(IStream *)

    You might ask DAO to give you a Stream or Storage to the blob. If DAO doesn't do this, copy the BLOB to a HGLOBAL (allocated with GlobalAlloc, yes I know this is an anachronism, but it's right), then use CreateStreamOnHGlobal, or use CreateILockBytesOnHGlobal, StgOpenDocFileOnILockBytes.

    (which you have to do, unhappily, depends on how MS Access persisted the object. If both OLE Server & Access are nice guys, both methods may work)


    ----------- DAO

    If you can / want to add the image from your Program (i.e. using DAO), you need a way to get a native windows bitmap from/to a long binary.
    Well, in this case, I cannot be of much help, but there are enough fols who did it (or, at least, something similar). Search this Forum, and www.codeguru.com, and The Net on how to load/save a bitmap. I remember of some code to load & save .bmp's in the DirectX samples. YOu "only" need to convert the file operations into memcpy's or something...



    Helped?
    If not, ask again
    Peter





  3. #3
    Join Date
    Oct 1999
    Location
    WA
    Posts
    2,393

    Re: Cannot load a bitmap from CLongBinary object

    There are two problems.

    1) What is the format of data represented by m_hData ? You need a pointer to a packed DIB from this handle.

    2) From a DIB pointer, you can either use it directly, or convert to a DDB/DIB section.


  4. #4
    Join Date
    May 2008
    Posts
    6

    Re: Cannot load a bitmap from CLongBinary object

    Code:
    //try gdi plus
    
    #include <gdiplus.h>
    #pragma comment(lib, "Gdiplus.lib")
    
    CBitmap* GetBitmapP(CLongBinary* src)
    {
        CBitmap* back = nullptr;
        HBITMAP *hbmp = new HBITMAP();
    	
        // start gdi
        Gdiplus::GdiplusStartupInput gdiplusStartupInput;
        ULONG_PTR gdiplusToken;
        Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
        Gdiplus::Status s;
    	
        // stream from global
        IStream *stream = nullptr;
    
        if (CreateStreamOnHGlobal(src->m_hData, TRUE, (LPSTREAM*)&stream) != S_OK)
        {
            return nullptr;
        }
    
        //load bitmap from stream
        Gdiplus::Bitmap *image = Gdiplus::Bitmap::FromStream(stream);
    
        s = image->GetLastStatus();
        if (s == S_OK)
        {
            //get CBitmap from handle
            s = image->GetHBITMAP(Gdiplus::Color::Transparent, hbmp);
            back  = CBitmap::FromHandle(*hbmp);
        }
    
        //shutdown gdi
        Gdiplus::GdiplusShutdown(gdiplusToken);
        return back;
    }
    Last edited by 2kaud; August 5th, 2019 at 08:45 AM. Reason: Added code tags

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,423

    Re: Cannot load a bitmap from CLongBinary object

    Quote Originally Posted by loupus View Post
    //try gdi plus
    Code:
    #include <gdiplus.h>
    #pragma comment(lib, "Gdiplus.lib")
    
    
    	CBitmap* GetBitmapP(CLongBinary* src)
    	{
    		CBitmap* back = nullptr;
    		HBITMAP *hbmp = new HBITMAP();
    		
    	
                   // start gdi
    		Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    		ULONG_PTR gdiplusToken;
    		Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    
    		Gdiplus::Status s;
    	
                    // stream from global
    		IStream *stream = nullptr;
    		if (CreateStreamOnHGlobal(src->m_hData, TRUE, (LPSTREAM*)&stream) != S_OK)
    		{
    			return nullptr;
    		}
    
                    //load bitmap from stream
    		Gdiplus::Bitmap *image = Gdiplus::Bitmap::FromStream(stream);
    		s = image->GetLastStatus();
    
    		if (s == S_OK)
    		{
                             //get CBitmap from handle
    			s = image->GetHBITMAP(Gdiplus::Color::Transparent, hbmp);
    			back  = CBitmap::FromHandle(*hbmp);
    			
    		}
    
                    //shutdown gdi
    		Gdiplus::GdiplusShutdown(gdiplusToken);
    
    		return back;
    	}
    1. Are you sure the OP still needs the solution for this problem? After almost 20 years?
    2. Please use CODE tags while posting code snippets!
    Victor Nijegorodov

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)