Results 1 to 2 of 2

Thread: Create Transparent PNG Image

  1. #1
    Join Date
    Aug 2009
    Posts
    11
    Thanks
    1

    Default Create Transparent PNG Image

    I want to convert SVG to PNG without background (transparent)
    I use QtWebKit to acquire this

    But it's not work, are you give me some idea to fix it ?

    svg2png.h
    Qt Code:
    1. #ifndef WEBKIT_SVG_RENDERER
    2. #define WEBKIT_SVG_RENDERER
    3.  
    4. #include <QWebView>
    5.  
    6. class WebKitSVGRenderer : public QWebView
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. QString fileName;
    12. QSize targetSize;
    13. WebKitSVGRenderer(QWidget *parent = 0);
    14.  
    15. private slots:
    16. void saveResult(bool ok);
    17. };
    18.  
    19. #endif // WEBKIT_SVG_RENDERER
    To copy to clipboard, switch view to plain text mode 


    svg2png.cpp
    Qt Code:
    1. #include "svg2png.h"
    2.  
    3. #include <iostream>
    4.  
    5. #include <QtGui>
    6. #include <QtWebKit>
    7.  
    8. // large enough to enclose the SVG icons
    9. #define MAGIC_SIZE 4095
    10.  
    11. WebKitSVGRenderer::WebKitSVGRenderer(QWidget *parent) : QWebView(parent)
    12. {
    13. connect(this, SIGNAL(loadFinished(bool)), this, SLOT(saveResult(bool)));
    14. setFixedSize(MAGIC_SIZE, MAGIC_SIZE);
    15.  
    16. QPalette pal = palette();
    17. pal.setColor(QPalette::Background, Qt::transparent);
    18. setPalette(pal);
    19. }
    20.  
    21. #define EVAL_JS(x) page()->mainFrame()->evaluateJavaScript((x))
    22.  
    23. void WebKitSVGRenderer::saveResult(bool ok)
    24. {
    25. // crude error-checking
    26. if (!ok) {
    27. std::cerr << "Failed loading " << qPrintable(url().toString()) << std::endl;
    28. QApplication::instance()->exit(1);
    29. return;
    30. }
    31.  
    32. // ensure it is an SVG
    33. QString root = EVAL_JS("document.rootElement.nodeName").toString();
    34. if (root.isEmpty() || root.compare("svg", Qt::CaseInsensitive)) {
    35. std::cerr << "Not an SVG! " << qPrintable(url().toString()) << std::endl;
    36. close();
    37. return;
    38. }
    39.  
    40. // get the dimension, i.e. the width and height attributes
    41. // Note: if an attribute is not defined WebKit would return the view's dimension
    42. // hence the hack of checking for the MAGIC_SIZE
    43. double ww = EVAL_JS("document.rootElement.width.baseVal.value").toDouble();
    44. double hh = EVAL_JS("document.rootElement.height.baseVal.value").toDouble();
    45. if (ww == 0.0 || hh == 0.0 || ww == MAGIC_SIZE || hh == MAGIC_SIZE) {
    46. std::cerr << "SVG does not specify proper width and height! " << std::endl;
    47. close();
    48. return;
    49. }
    50.  
    51. // try to give the best output file
    52. if (fileName.isEmpty()) {
    53. fileName = QFileInfo(url().path()).completeBaseName();
    54. if (fileName.isEmpty())
    55. fileName = "result";
    56. fileName += ".png";
    57. }
    58.  
    59. // create the target surface
    60. QSize t = targetSize.isValid() ? targetSize : QSize(ww, hh);
    61. QImage img(t, QImage::Format_ARGB32_Premultiplied);
    62. img.fill(Qt::transparent);
    63.  
    64. // prepare the painter
    65. QPainter p(&img);
    66. qreal xs = targetSize.isValid() ? targetSize.width() / ww : 1.0;
    67. qreal ys = targetSize.isValid() ? targetSize.height() / hh : 1.0;
    68. p.scale(xs, ys);
    69.  
    70. // the best quality
    71. p.setRenderHint(QPainter::Antialiasing);
    72. p.setRenderHint(QPainter::TextAntialiasing);
    73. p.setRenderHint(QPainter::SmoothPixmapTransform);
    74.  
    75. page()->mainFrame()->render(&p);
    76. p.end();
    77.  
    78. if (img.save(fileName, "png"))
    79. std::cout << "Result saved to " << qPrintable(fileName) << std::endl;
    80. else
    81. std::cout << "Failed to save to " << qPrintable(fileName) << std::endl;
    82.  
    83. close();
    84. }
    85.  
    86. static void usage()
    87. {
    88. std::cout << "Rasterize an SVG icon to a PNG image" << std::endl << std::endl;
    89. std::cout << " svg2png input.svg [output.png [width height]]" << std::endl << std::endl;
    90. std::cout << "Examples: " << std::endl;
    91. std::cout << " svg2png tiger.svg" << std::endl;
    92. std::cout << " svg2png icon.svg icon.png 256 256" << std::endl;
    93. std::cout << std::endl;
    94. }
    95.  
    96. int main(int argc, char * argv[])
    97. {
    98. if ((argc < 2) || (argc == 4)) {
    99. usage();
    100. return 0;
    101. }
    102.  
    103. QApplication a(argc, argv);
    104.  
    105. WebKitSVGRenderer renderer;
    106. if (argc < 3)
    107. renderer.fileName = QString::fromLatin1(argv[2]);
    108. if (argc == 5) {
    109. int w = QString::fromLatin1(argv[3]).toInt();
    110. int h = QString::fromLatin1(argv[4]).toInt();
    111. renderer.targetSize = QSize(w, h);
    112. if (!renderer.targetSize.isValid() || w <= 0 || h <= 0) {
    113. std::cerr << "Please specify a valid target size !" << std::endl;
    114. return 0;
    115. }
    116. }
    117.  
    118. QString str = QFileInfo(QString::fromLatin1(argv[1])).absoluteFilePath();
    119. renderer.load(QUrl::fromLocalFile(str));
    120.  
    121. return a.exec();
    122. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Last edited by wysota; 2nd August 2009 at 14:32.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Create Transparent PNG Image

    Have you tried QSvgRenderer? Currently if you render the frame, it probably contains its own background (the webpage is not transparent) which is also rendererd onto the device.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How can I create a transparent about box like SKYPE?
    By ricardo in forum Qt Programming
    Replies: 3
    Last Post: 19th May 2009, 18:57
  2. Replies: 1
    Last Post: 31st January 2009, 23:00
  3. How can I create a transparent image with QImage?
    By learning_qt in forum Qt Programming
    Replies: 1
    Last Post: 28th January 2009, 15:06
  4. Need help to create Transparent QTreeView
    By vajindarladdad in forum Newbie
    Replies: 1
    Last Post: 9th January 2009, 14:29
  5. Using a 2d data array to create an image
    By dbrmik in forum Newbie
    Replies: 20
    Last Post: 28th October 2008, 17:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.