2015-02-09 83 views
2

翻轉/鏡像QImage的正確方法是什麼?以下代碼段不起作用。如何翻轉QImage

//allocate buffer 
BYTE* pRgb32Buffer = new BYTE[width*height* 4]; 
//create paint device 
QImage img = QImage(pRgb32Buffer , width, height, getStride(width, pixelFormat), QImage::Format_RGB32); 
//do some drawing on image (works!) 
QPainter painter(&img); 
painter.drawText(10, 50, QString("some text")); 
//mirrore image (doesn't mirror the orignal buffer!!!) 
img = img.mirrored(false,true); 
//doesn't work either 
//QImage mirrored = img.mirrored(); 
//img = mirrored; 
//mirrored.detach(); 
+0

故意做你'pRgb32Buffer'和'pBuffer':

可選,如果你想(請確保您有imageformats的DLL,或者不會寫),你可以保存文件? – fritzone 2015-02-09 12:03:51

+0

對不起。已更正 – maxw 2015-02-09 12:04:50

+0

您的意思是什麼不起作用? QImage :: mirrored()返回原始圖像的副本。鏡像不會影響pRgb32Buffer中的數據。除此之外,我看不出爲什麼它不起作用。 – 2015-02-09 12:20:41

回答

2

我有這樣的工作代碼到的QPixmap鏡像到一個QImage的。

QPixmap* source = //... Getting my pixmap form somewhere... 
QImage target(QSize(source->width(), source->height()), QImage::Format_ARGB32); 
QPainter painter(&target); 
QTransform transf = painter.transform(); 
transf.scale(-1, 1); 
painter.setTransform(transf); 
painter.drawPixmap(-source->width(), 0, *source); 

source包含該代碼之後的鏡像像素圖。您應該可以使用QImageQPainter::drawImage函數作爲替代方法。

QImageWriter writer("c:\\theimage.tiff", "tiff"); 
writer.setCompression(1); 
writer.write(target);