Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Load Image From Db Problem
Forum Updated to NodeBB v4.3 + New Features

Load Image From Db Problem

Scheduled Pinned Locked Moved Unsolved General and Desktop
loading imagesdatabaseproblem
26 Posts 4 Posters 12.7k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • VRoninV VRonin

    @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). use varbinary(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.

    M4RZB4NiM Offline
    M4RZB4NiM Offline
    M4RZB4Ni
    wrote on last edited by
    #14

    @VRonin
    yes i use IMAGE because when i use VABBINARY(MAX) when i want save image
    program show error
    "Cant Convert varchar datatype to VARBINARY(MAX)!
    and i should Use IMAGE datatype!

    Thanks
    M4RZB4Ni

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #15

      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.

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • M4RZB4NiM M4RZB4Ni

        Hi
        I have saved a pic to my Sql Server Db
        but When i want To load it in my Graphic View
        DO nothing :|
        this is my code:
        .h file

                QPixmap originalPixmap;
                QPixmap outPixmap;
                QByteArray inByteArraye;
                QByteArray outByteArray;
        

        .cpp file

            readSkeletonPicQry.exec("SELECT SskeletonPic FROM Patient_File WHERE SmeliCode='"+seMcode+"'");
            outByteArray = readSkeletonPicQry.value(0).toByteArray();
            outPixmap = QPixmap();
            outPixmap.loadFromData(outByteArray,"PNG");
            scene->addPixmap(outPixmap);
            ui->graphicsView_2->setScene(scene)
        

        my datatype in sql server Table in IMAGE and its store pics Fine
        but i cant Show pic in graphic View From Table
        *My Table Pic
        whats Wrong?

        M4RZB4NiM Offline
        M4RZB4NiM Offline
        M4RZB4Ni
        wrote on last edited by M4RZB4Ni
        #16

        @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();
        }
        

        Thanks
        M4RZB4Ni

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #17

          i don't get the threading thing but it's probably not the point here. where did you get the
          "Cant Convert varchar datatype to VARBINARY(MAX)" error?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          M4RZB4NiM 1 Reply Last reply
          0
          • VRoninV VRonin

            i don't get the threading thing but it's probably not the point here. where did you get the
            "Cant Convert varchar datatype to VARBINARY(MAX)" error?

            M4RZB4NiM Offline
            M4RZB4NiM Offline
            M4RZB4Ni
            wrote on last edited by
            #18

            @VRonin
            when i want insert image into database after capture screenshot

            Thanks
            M4RZB4Ni

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #19

              Can you post that code please?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              M4RZB4NiM 1 Reply Last reply
              0
              • VRoninV VRonin

                Can you post that code please?

                M4RZB4NiM Offline
                M4RZB4NiM Offline
                M4RZB4Ni
                wrote on last edited by
                #20

                @VRonin
                yes of course
                thanks so much

                this 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+"';");
                }
                

                Thanks
                M4RZB4Ni

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #21

                  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

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  M4RZB4NiM 1 Reply Last reply
                  1
                  • VRoninV VRonin

                    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

                    M4RZB4NiM Offline
                    M4RZB4NiM Offline
                    M4RZB4Ni
                    wrote on last edited by
                    #22

                    @VRonin
                    thanks but my problem is in Load image not save it!
                    Thank a lot My Friend :)

                    Thanks
                    M4RZB4Ni

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #23

                      make the field varbinary(max), save it that way and the code you have will work, no changes needed on the load part

                      image in SQL server is a fixed length string. it has nothing to do with images

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      4
                      • KutyusK Offline
                        KutyusK Offline
                        Kutyus
                        wrote on last edited by
                        #24

                        Hi!
                        The same problem, you could solve?
                        Thanks

                        VRoninV 1 Reply Last reply
                        0
                        • KutyusK Kutyus

                          Hi!
                          The same problem, you could solve?
                          Thanks

                          VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #25

                          @Kutyus

                          Summary of the above:

                          • in the database use the type VABBINARY not IMAGE
                          • use QSqlQuery::bindValue instead of string concatenation to build your query

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          KutyusK 1 Reply Last reply
                          3
                          • VRoninV VRonin

                            @Kutyus

                            Summary of the above:

                            • in the database use the type VABBINARY not IMAGE
                            • use QSqlQuery::bindValue instead of string concatenation to build your query
                            KutyusK Offline
                            KutyusK Offline
                            Kutyus
                            wrote on last edited by
                            #26

                            @VRonin Thank you for reply, but my problem is the load image from database, please see my another question:
                            QT 5.8 image from database error

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved