qtcpsocket send and recieve image

本文介绍了一个基于网络的应用程序,服务器截取屏幕截图并将其发送给客户端,客户端接收并显示图片。提供了发送和接收图像的代码示例,并讨论了确保所有数据接收和转换为QPixmap以供显示的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接
Hi All, 

I have two simple applications which are network based. The 'server'
takes a screenshot of its computer. When a 'client' connects, it will
receive the captured image. These are basically the fortune
server/client examples modified to transmit images instead of text.

Here is my code for sending the image:

      QByteArray block; 

    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_0);
    out << (quint16)0;
    out << originalPixmap;
    qDebug() << "Pixmap Sent";
    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));
    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    connect(clientConnection, SIGNAL(disconnected()),
            clientConnection, SLOT(deleteLater()));
    clientConnection->write(block);
    clientConnection->disconnectFromHost();


And here is my code for reading it

     QDataStream in(tcpSocket); 

    in.setVersion(QDataStream::Qt_4_0);
    if (blockSize == 0) {
    if (tcpSocket->bytesAvailable() < (int)sizeof(quint64))
            return;
        in >> blockSize;
    }
    if (tcpSocket->bytesAvailable() < blockSize)
        return;
    QPixmap nextPixmap;
    in >> nextPixmap;
    originalPixmap = currentPixmap;
    updateScreenshotLabel();
    getFortuneButton->setEnabled(true);
    received = false;


 
 
 

The client receives some data, but how to I:
    1. Ensure that all of the sent data is received, and
    2. Convert the received data into a QPixmap so that it can be
displayed.

I know it sounds simple but i have racked  my brains for several days
now and I just cant seem to get it. Please assist.

Thank you in advance,

Joshua W.

 

I am getting these errors:
QPixmap::scaled: Pixmap is a null pixmap 

QSize(-1, -1) 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值