使用
UDP传输图片,但是接收之后无法正常
显示,只显示一块黑色的图片
发送图片的代码:
QByteArray buffer( 6+3*image->width(), 0 );
QDataStream stream( &buffer, QIODevice::WriteOnly );
stream.setVersion( QDataStream::Qt_4_6 );
stream << (quint16)image->width() << (quint16)image->height();
quint16 y = qrand() % image->height();
stream << y;
for( int x=0; x<image->width(); ++x )
{
QRgb rgb = image->pixel( x, y ); stream << (quint8)qRed( rgb ) << (quint8)qGreen( rgb ) << (quint8)qBlue( rgb );
} socket->writeDatagram( buffer.data(),buffer.size(), QHostAddress::Broadcast, 8888 );
接收图片的代码:
while( socket->hasPendingDatagrams() )
{
QByteArray buffer( socket->pendingDatagramSize(), 0 );
socket->readDatagram( buffer.data(), buffer.size() );
QDataStream stream( buffer );
stream.setVersion( QDataStream::Qt_4_6 );
quint16 width, height, y;
stream >> width >> height >> y;
if( !image )
image = new
QImage( width, height, QImage::Format_RGB32 );
else if( image->width() != width || image->height() != height )
{
delete image;
image = new QImage( width, height, QImage::Format_RGB32 );
}
for( int x=0; x<width; ++x )
{
quint8 red, green, blue;
stream >> red >> green >> blue;
ui->textEdit->append("ddd");
image->setPixel( x, y, qRgb( red, green, blue ) );
} }
接收之后显示

找了好久没找出哪错了,请各位帮个忙呀