Click here to Skip to main content
15,867,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new in qt c++. I am trying to load blob image which is stored in .bin file in phpmyadmin db .That .bin file store blob image in form of string like BM6 ect(each .bin have 3 to 4 char). Here is my code that execute on button click

driver = get_driver_instance();
 con = driver->connect("localhost","","");
 con->setSchema("BitmapImagesSchema");
 stmt = con-> createStatement();
 string 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-> getString("ImagesBitMap");
 break;
 }
 char fetchedData[4000];
 strncpy((char*) fetchedData,blobData.c_str(),sizeof(fetchedData));

 QPixmap p(fetchedData);
 if(!widget.graphicsView->scene()){
 QGraphicsScene  *scene = new QGraphicsScene(this);
 widget.graphicsView->setSene(scene);
 }
 widget.graphicsView->scene->addPixmap(p);

 delete res;
 delete stmt;
 delete con;


Pixmap(graphics view) is not display blob image on button click that execute above code.how to display blob image by using above code?

What I have tried:

Here is code that i have tried.
While(res->last()){
  blobData = res-> getString("ImagesBitMap");
  break;
  }
  char fetchedData[4000];
  strncpy((char*) fetchedData,blobData.c_str(),sizeof(fetchedData));

  QPixmap p(fetchedData);
  if(!widget.graphicsView->scene()){
  QGraphicsScene  *scene = new QGraphicsScene(this);
  widget.graphicsView->setSene(scene);
  }
  widget.graphicsView->scene->addPixmap(p);


But it (fetchedData) is displaying string like this
Quote:
BM6

How to convert this string BM6 in to image for display in Pixmap(graphics view)?
Posted
Updated 18-Sep-22 6:02am
v2
Comments
Richard MacCutchan 18-Sep-22 11:52am    
You should not use
char fetchedData[4000];

strncpy((char*) fetchedData,blobData.c_str(),sizeof(fetchedData));

A bitmap image is a binary file not a character string.

1 solution

BM6 is the header of a bitmap file, followed by a double quote character - 0x22. Following the 0x22, there are four nulls. You can not expect to copy a bitmap's binary data using strncpy because there are null characters in it and they will terminate the copy. If you store and read the data as a string you will get exactly five bytes, including the null character. It needs to be saved as exactly how you described it - a binary blob.

This page describes how the data is stored in a file : BMP file format - Wikipedia[^]. It might be useful for you to load a bitmap file into a hex editor so you can see the data for yourself. You need to determine how Qt wants to see the data in memory for display purposes and then implement code to translate it into that format.
 
Share this answer
 
Comments
Member 8840306 22-Sep-22 3:28am    
Please provide me code for doing getting blob image using getBlob() and storing is isstream for display in pixmap!???

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