Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

ByteArray to Image



  • Hello Friends

    i am working on TCP/IP Application
    I am sending image using QTcpSocket and receive image in server Side application

    i am getting bytearray data from socket now i want to convert my bytearray Data into image

    My Server Side Code Is :

                        socket = reinterpret_cast<QTcpSocket*>(sender());                    
                        socket->setReadBufferSize(4096);                                                           
                        QFile target;                    
                        target.setFileName(QString("ABC.bmp"));                                                                
                        qDebug() << socket->readBufferSize();                    
                        qDebug() << socket->readAll();                    
                        Data = socket->readAll();                                                          
                        if (!target.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::ReadWrite))                    
                        {                    
                            qDebug() << "Can't open file for written";                    
                            return;                    
                        }                                                            
                        target.write(Data);                    
                        target.close();                                                          
                        ui->lbl_Image_From_Client->setPixmap(QPixmap("ABC.bmp"));                    
                        ui->lbl_Image_From_Client->update();
    

    My Client Side Code Is :

                                 if(socket->isOpen())                               
                                 {                                                                                            
                                    QFile file("Screen.bmp");                               
                                    if(!file.open(QIODevice::ReadWrite))                               
                                        qDebug() << "Can't Send";                               
                                    Data = file.readAll();                                      
                                    socket->setReadBufferSize(4096);                               
                                    socket->write(Data);                               
                                    qDebug() << socket->readAll();                               
                                    socket->flush();                               
                                 }
    

    i didn't get image at server side
    please help me to solve this problem


  • Lifetime Qt Champion

    @Ketan__Patel__0011 Your server code (did not check your client code) is not going to work if it is in a slot connected to readyRead signal (please post more complete code, so it is clear where that code chunk is actually located). The problem is: the data will arrive in several chunks not just one. That means you need to accumulate the data in a buffer until you got whole data and only then use it to create the image.


Log in to reply