Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in C++ .I am trying to load blob image which is stored in .bin file from sql Database. That .bin file store blob image in form of string like BM6 ect(each .bin have 3 to 4 char).

Here is code(working) that save the blob image(.bmp) in db

C++
QPixmap p;
            p.load(filename);
    
      
            string a = "/root/QtApplication_1/file";        
            string b = boost::lexical_cast<std::string>(counter);
            string c = ".bmp";
            
            p.save(("/root/QtApplication_1/file"+boost::lexical_cast<std::string>(counter)+".bmp").c_str(),"BMP");
            std::string d = a + b + c ;
            
            
            std::ifstream blob_file;
            blob_file.open(d.c_str(), std::ios_base::binary | std::ifstream::in);
            
        driver = get_driver_instance();
        con = driver->connect("localhost","","");
        con->setSchema("BitmapImagesSchema");
    
        
        prep_stmt = con->prepareStatement("Insert into bitmapImagesTable(`ID`,`ImageDir`,`ImagesBitMap`) values(?,?,?)");
        prep_stmt->setInt(1,counter);
     // 
        prep_stmt->setString(2,d.c_str());
      //  byte *p = "" ;
        prep_stmt->setBlob(3,&blob_file);
            
        prep_stmt->executeQuery();
       
        delete prep_stmt;


What I have tried:

Here is my code(not working) that i am trying to get blob image from db and dispaly in QgraphicsView using pixmap

C++
driver = get_driver_instance();
    con = driver->connect("localhost","","");
    con->setSchema("BitmapImagesSchema");
    stmt = con-> createStatement();
    
    std::istream *blobData;
    res = stmt->executeQuery("select `ImagesBitMap` from bitmapImagesTable where `ID`='"+boost::lexical_cast<std::string>(counter)+"'order by `ID` DESC");
    while(res->last()){
        blobData = res->getBlob("ImagesBitMap");
        break;
    }
    
    std::istreambuf_iterator<char> isb = std::istreambuf_iterator<char>(*blobData);
    std::string blobString  = std::string(isb,std::istreambuf_iterator<char>());
    const char * image = blobString.c_str();
    blobData->seekg(0,ios::end);
    size_t imagesize = blobData->tellg();
    
    cout<<"aaa="<<image<<"\n";
    QPixmap p;
    p.load(image);
    if(!widget.graphicsView_2->scene()){
    QGraphicsScene  *scene = new QGraphicsScene(this);
    widget.graphicsView_2->setScene(scene);
    }
            
    widget.graphicsView_2->scene()->addPixmap(p);
    
   
   
    delete res;
    delete stmt;
    delete con;

Graphics view that is using pixmap is not display blob image on button click that execute above code.
The code for reading blob image i have taken from this
link

Here is blob data base that is .bin file
image


Here is the out put of the image varibale

C++
cout<<"aaa="<<image<<"\n";



image


How to display blob image by using above code?
Posted
Updated 23-Sep-22 21:27pm
v4
Comments
Richard MacCutchan 24-Sep-22 2:51am    
You are converting the blob image to a character string. That conversion means it is no longer in image form. You must always process it as a pure binary array.
Member 8840306 24-Sep-22 3:15am    
please prove me the code snippet to correct the above task( dispaly blob in QgraphicsView using pixmap)?
Richard MacCutchan 24-Sep-22 11:33am    
As I said you cannot treat an image file as a character string. You must read it in binary mode and store it as a byte array, or stream.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900