Solved

Bitmaps with a specific color transparent.

Posted on 1998-03-16
18
347 Views
Last Modified: 2013-12-22
I need to have the ability to display a bitmap with a specified color depicting the transparent part of the bitmap. In my case it is pink. If you have any code to do this I would really appreciate it.

I'm using Win16/Win32 API(should work on Win3.1 & 95).

Bitmap is drawn in a dailog box.


Software Used: VC++4.2    for Win32
                       VC++1.52  for Win16

0
Comment
Question by:umesh053097
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +7
18 Comments
 
LVL 1

Expert Comment

by:stevesm
ID: 1708986
What software are you using, and where do you want to display the bitmap?
0
 

Author Comment

by:umesh053097
ID: 1708987
Edited text of question
0
 
LVL 1

Expert Comment

by:stevesm
ID: 1708988
Sorry, Not much help
Perhaps you should reopen the question to someone else.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:dpavusin
ID: 1708989
I have a source code written in C for a program named transgif. You can list values of all colors in gif and than select one value and make it transparent. Again IT is for gif format only. I'm not sure if it can help you..

Damir
0
 
LVL 2

Expert Comment

by:avi_shava
ID: 1708990
I don't think bitmaps can be transperent. bitmap format (24 bit color, for example), save for any pixel a value of the color (at 24 bit). Because there is no color value for transperency, i don't think bitmaps (bmp) can be transperent. Gif's, on the other hand, can be transperent very easily.
0
 
LVL 39

Expert Comment

by:abel
ID: 1708991
A bitmap itself can't be transparent, but it is quite easy to display it as transparent. Your question is about displaying, so here it goes.

Take a look at BitBlt. Use it to display your image. You have to display your image twice, using a mask-image. Your mask image is a two color image. Use black for the non-transparent colors, the opposite color of your color that has to be transparent as the transparent color. (you  get that by inverting the image in an editor)
First, the mask picture must be copied to the display with the SRCAND as dwRop in BitBlt (the last parameter), second, you use the SRCINVERT as dwRop to display the original image. Both on the same location. As a result your bitmap's color you would like to be transparent shows transparent.

BTW the imagelist can be used as well for displaying pictures transparent, but even than you have to use a mask for that. The problem with the imagelist is that all pictures must be of the same size.

BTW2 using a mask is (by my knowledge) the most common (and fast) way of displaying a bitmap transparent.

Hope this helps you out. For any questions, please place a comment, if this doesn't apply to your problem, reopen the q. for other experts.

Regards, Abel
0
 

Expert Comment

by:hack031798
ID: 1708992
In Windows95 the defalt transparentcy color is blue 100% blue if your going to make an icon u'd use blue for the transparent part.. i dont know if that helps but it cant hurt...
0
 

Author Comment

by:umesh053097
ID: 1708993
Abel,
Your answer is the approach I'm currently taking. I have all the bitmaps and their masks too. The only reason I asked the question was, so that I didn't have to create a new mask everytime I got a new bitmap. But thanks anyway.  
0
 
LVL 39

Expert Comment

by:abel
ID: 1708994
Would some code on how to make a mask of a bitmap suffice? Then you can make it at runtime and in memory, so you don't need making the masks by hand.
0
 

Author Comment

by:umesh053097
ID: 1708995
Yes, I would certainly take a look at it and try it.
0
 
LVL 39

Expert Comment

by:abel
ID: 1708996
If you don't mind I'll post that tomorrow. It's almost night up here, so I'm going home now. I hope that before that no one else will answer this q., otherwise my hours of work would be in vain. :-)

Regards, Abel
0
 
LVL 1

Expert Comment

by:barakp
ID: 1708997
In Windows95 the 1,1 pixel is the transparent color. Check if it works for Win3.11 too.

B.P.
0
 

Author Comment

by:umesh053097
ID: 1708998
Mr B.P

Could you be more elaborate in your suggestion.
0
 

Expert Comment

by:pasco
ID: 1708999
When editing colormapped (palette-color) images, many image editors allow you to designate one of the palette colors as "transparent" if the file format permits it.  For example, when
saving a GIF image, Cerious Software's Thumbs Plus allows you to click on "Transparent" and then click somewhere in the image to designate the color which is to be transparent.
0
 

Author Comment

by:umesh053097
ID: 1709000
I don't have the flexibility of dealing with GIF images only BMPs
0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1709001
I can give you Delphi code to create a transparent bitmap, then you can convert it to C

Let me know ¿ by comment.....

Later

BoRiS
0
 

Author Comment

by:umesh053097
ID: 1709002
Boris,
I will certainly take a look at it. Even though I don't know Delphi, I think Pascal would be pretty easy to understand. Go ahead and post it.
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 500 total points
ID: 1709003
go to www.codeguru.com - you should find some code.

This is the code that I use (derived form CBitmap)

void CMyBitmap::GetWidthAndHeight(int* pw, int* ph) const {
      if (! GetSafeHandle()) {
            if (pw) *pw = 0;
            if (ph) *ph = 0;
      } else {
            BITMAP bm; GetObject(sizeof(bm), &bm);
            if (pw) *pw = bm.bmWidth;
            if (ph) *ph = bm.bmHeight;
      }
}

void CMyBitmap::Draw(CDC* pDC, int x,int y, int w,int h, bool bTransparent, CPalette* pPalette) const {
      GdiFlush();

    // DC for Image
    CDC dcImage;
    dcImage.CreateCompatibleDC(pDC);
      if (! dcImage.GetSafeHdc()) return false;
      CPalette* pPalOldImage = NULL;
      if (pPalette) pPalOldImage = dcImage.SelectPalette(pPalette,false);
      dcImage.RealizePalette();
         CBitmap* pBmpOldImage = NULL;
      if (dcImage.GetSafeHdc()) pBmpOldImage = dcImage.SelectObject(const_cast<QBitmap*>(this));

      int ww = w < 0 ? -w : w;
      int hh = h < 0 ? -h : h;
      // DC for mask
      int dx,dy; GetWidthAndHeight(&dx,&dy);
      CDC dcMask;
      VERIFY(dcMask.CreateCompatibleDC(pDC));
      CBitmap bmpMask;
      VERIFY(bmpMask.CreateBitmap(dx,dy, 1, 1, NULL));
      CBitmap* pBmpOldMask = dcMask.SelectObject(&bmpMask);

      // Create mask
      COLORREF oldBkColor = dcImage.SetBkColor(dcImage.GetPixel(0,0));
      dcMask.BitBlt(0,0, dx,dy, &dcImage, 0,0, SRCCOPY);
      dcImage.SetBkColor(oldBkColor);

      // DC for off-screen
      CDC dcOffscreen;
      VERIFY(dcOffscreen.CreateCompatibleDC(pDC));
      CPalette* pPalOldOffscreen= NULL;
      pPalOldOffscreen = dcOffscreen.SelectPalette(pPalette,false);
      dcOffscreen.RealizePalette();
      CBitmap bmpOffscreen;
      VERIFY(bmpOffscreen.CreateCompatibleBitmap(pDC,ww,hh));
      CBitmap* pBmpOldOffscreen = dcOffscreen.SelectObject(&bmpOffscreen);
      if (w == ww && h == hh) {
            dcOffscreen.BitBlt(0,0, ww,hh, pDC, x,y, SRCCOPY);
      } else {
            dcOffscreen.StretchBlt(0,0, ww,hh, pDC, x,y, w,h, SRCCOPY);
      }

      COLORREF oldBkColorOffScr = pDC->SetBkColor(rgbWhite);
      if (dx == ww && dy == hh) {
            dcOffscreen.BitBlt(0,0, ww,hh, &dcImage, 0,0, DSx);
            dcOffscreen.BitBlt(0,0, ww,hh, &dcMask , 0,0, DSa);
            dcOffscreen.BitBlt(0,0, ww,hh, &dcImage, 0,0, DSx);
      } else {
            dcOffscreen.StretchBlt(0,0, ww,hh, &dcImage, 0,0, dx,dy, DSx);
            dcOffscreen.StretchBlt(0,0, ww,hh, &dcMask , 0,0, dx,dy, DSa);
            dcOffscreen.StretchBlt(0,0, ww,hh, &dcImage, 0,0, dx,dy, DSx);
      }
      pDC->SetBkColor(oldBkColorOffScr);

      if (w == ww && h == hh) {
            pDC->BitBlt(x,y, ww,hh, &dcOffscreen, 0,0, SRCCOPY);
      } else {
            pDC->StretchBlt(x,y, w,h, &dcOffscreen, 0,0, ww,hh, SRCCOPY);
      }

      // Tidy up
      if (pPalOldOffscreen) {
            dcOffscreen.SelectPalette(pPalOldOffscreen,false);
            dcOffscreen.RealizePalette();
      }
      dcOffscreen.SelectObject(pBmpOldOffscreen);
      dcMask.SelectObject(pBmpOldMask);

      if (dcImage.GetSafeHdc() && pPalOldImage) {
            dcImage.SelectPalette(pPalOldImage,false);
            dcImage.RealizePalette();
      }
      if (dcImage.GetSafeHdc()) dcImage.SelectObject(pBmpOldImage);

      GdiFlush();
}

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question