Load Image From Db Problem
-
@M4RZB4Ni said in Load Image From Db Problem:
i think problem is converting from table(IMAGE DATATYPE) TO byte array !
are you using
image
as field type in your database? if so you shouldn't, it's deprecated and will be removed (see https://msdn.microsoft.com/en-us/library/ms187993.aspx). usevarbinary(max)
instead.Also, you should use QImage instead of QPixmap.
Qt provides four classes for handling image data: QImage, QPixmap, QBitmap and QPicture. QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen. QBitmap is only a convenience class that inherits QPixmap, ensuring a depth of 1. Finally, the QPicture class is a paint device that records and replays QPainter commands.
-
How do you save it?
You should definitely use VARBINARY:
From Microsoft, page linked above:
IMPORTANT! ntext, text, and image data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.
-
@M4RZB4Ni
this is all of my codes:
see it and if you can correct it
.h file:QImage *pix; QImage originalPixmap; QPixmap outPixmap; QPixmap pxp; QGraphicsPixmapItem pixItem; QByteArray inByteArraye; QByteArray outByteArray;
.cpp file:
void Medical_Records::shootScreen() { QScreen *screen = QGuiApplication::primaryScreen(); if (const QWindow *window = windowHandle()) screen = window->screen(); if (!screen) return; originalPixmap=ui->graphicsView_2->grab().toImage(); updateGraphicView(); } void Medical_Records::initalizeVarables() { shootScreen(); QBuffer inBuffer( &inByteArraye ); inBuffer.open(QIODevice::WriteOnly); originalPixmap.save(&inBuffer,"PNG"); } void Medical_Records::readSkeletonPic() { moveToThread(readSkeletonPicThread); readSkeletonPicQry.exec("SELECT SskeletonPic FROM Patient_File WHERE SmeliCode='"+seMcode+"'"); readSkeletonPicQry.next(); outByteArray = readSkeletonPicQry.value(0).toByteArray(); //outPixmap = QPixmap(); originalPixmap.loadFromData(outByteArray,"PNG"); qDebug() << "outByteArray :" << outByteArray.size(); qDebug() << "outByteArray :" << originalPixmap.size(); outPixmap.fromImage(originalPixmap.fromData(outByteArray,"PNG")); scene->addPixmap(outPixmap); ui->graphicsView_2->setScene(scene); qDebug() << readSkeletonPicQry.exec(); readSkeletonPicThread->start(); }
-
@VRonin
yes of course
thanks so muchthis is code :
void Medical_Records::initalizeVarables() { shootScreen(); QBuffer inBuffer( &inByteArraye ); inBuffer.open(QIODevice::WriteOnly); originalPixmap.save(&inBuffer,"PNG"); } void Medical_Records::submitWithScreenShot() { initalizeVarables(); QSqlQuery sql; sql.exec("UPDATE Patient_File SET SskeletonPic='"+inByteArraye+"' WHERE SmeliCode='"+seMcode+"';"); }
-
yep, you should check QSqlQuery::bindValue
sql.prepare("UPDATE Patient_File SET SskeletonPic= :skp WHERE SmeliCode= :smc"); sql.bindValue(":skp",inByteArraye); sql.bindValue(":smc",seMcode); sql.exec();
this also prevents SQL Injection. You should never really use unescaped input directly to build the query string