Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make the image background become transparent?
Forum Updated to NodeBB v4.3 + New Features

How to make the image background become transparent?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mistao
    wrote on last edited by
    #1

    Hi,
    I want to make the image background become transparent.
    The image has a pink background, but I only need the icon.
    So is there a way to make the pink become transparent?

    This is my code, but it does not work:

    QPixmap pixmap = QPixmap(":/icon/Manuel López Muñiz/database-normal.bmp");
    QPixmap temp(pixmap.size());
    temp.fill(Qt::transparent);

    QPainter p(&temp);
    p.setCompositionMode(QPainter::CompositionMode_Source);
    p.drawPixmap(0, 0, pixmap);
    p.setCompositionMode(QPainter::CompositionMode_SourceOver);
    p.fillRect(temp.rect(), QColor(255, 0, 255, 0));
    p.end();
    pixmap = temp;

    Thanks for your time.

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      see this https://en.wikipedia.org/wiki/BMP_file_format for a description of the BMP format. It does NOT support alpha channels. If you wish to use transparency then you have two choices:

      1. use an image format that supports transparencies
      2. translate the RGB BMP image into a format that replaces the pink pixels with pixels where the alpha value is zero (0) and use that converted image.
      M 1 Reply Last reply
      0
      • Kent-DorfmanK Kent-Dorfman

        see this https://en.wikipedia.org/wiki/BMP_file_format for a description of the BMP format. It does NOT support alpha channels. If you wish to use transparency then you have two choices:

        1. use an image format that supports transparencies
        2. translate the RGB BMP image into a format that replaces the pink pixels with pixels where the alpha value is zero (0) and use that converted image.
        M Offline
        M Offline
        Mistao
        wrote on last edited by Mistao
        #3

        @Kent-Dorfman Thanks for your reply!

        So I tried your suggestion, and this did not work as I expected.
        Is any thing wrong with my code?

        QImage bmp = QImage(":/icon/Manuel López Muñiz/database-normal.bmp");
        bmp.convertToFormat(QImage::Format_RGBA8888_Premultiplied);
        int width = bmp.width();
        int height = bmp.height();
        QColor change(255, 0, 255);
        QColor want(255, 0, 255, 0);
        for (int i = 0; i < width; i++)
        {
        for (int j = 0; j < height; j++)
        {
        if (bmp.pixelColor(i, j) == change)
        bmp.setPixelColor(i, j, want);
        }
        }

        QPixmap pixmap = QPixmap::fromImage(bmp);
        database = new QAction;
        QIcon icon(pixmap);
        database->setIcon(icon);
        ui->mainToolBar->addAction(database);

        1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by Kent-Dorfman
          #4

          have you verified that QColor(0xff00ff) is in fact the color that you need to replace? maybe add a count of pixels that it changes and then inspect that count to see if anything is actually being changed.

          Personally, I would have taken the first approach. Use an image editor and edit the BMP file so that it is in the correct format, and uses alpha correctly.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved