Bitmaps with a specific color transparent.

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

umesh053097Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stevesmCommented:
What software are you using, and where do you want to display the bitmap?
0
umesh053097Author Commented:
Edited text of question
0
stevesmCommented:
Sorry, Not much help
Perhaps you should reopen the question to someone else.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

dpavusinCommented:
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
avi_shavaCommented:
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
abelCommented:
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
hack031798Commented:
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
umesh053097Author Commented:
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
abelCommented:
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
umesh053097Author Commented:
Yes, I would certainly take a look at it and try it.
0
abelCommented:
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
barakpCommented:
In Windows95 the 1,1 pixel is the transparent color. Check if it works for Win3.11 too.

B.P.
0
umesh053097Author Commented:
Mr B.P

Could you be more elaborate in your suggestion.
0
pascoCommented:
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
umesh053097Author Commented:
I don't have the flexibility of dealing with GIF images only BMPs
0
BoRiSCommented:
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
umesh053097Author Commented:
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
RONSLOWCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.