Solved

Bitmaps with a specific color transparent.

Posted on 1998-03-16
18
315 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
  • 6
  • 3
  • 2
  • +7
18 Comments
 
LVL 1

Expert Comment

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

Author Comment

by:umesh053097
Comment Utility
Edited text of question
0
 
LVL 1

Expert Comment

by:stevesm
Comment Utility
Sorry, Not much help
Perhaps you should reopen the question to someone else.
0
 
LVL 4

Expert Comment

by:dpavusin
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

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

Expert Comment

by:abel
Comment Utility
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
Comment Utility
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
Comment Utility
Mr B.P

Could you be more elaborate in your suggestion.
0
 

Expert Comment

by:pasco
Comment Utility
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
Comment Utility
I don't have the flexibility of dealing with GIF images only BMPs
0
 
LVL 4

Expert Comment

by:BoRiS
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Several part series to implement Internet Explorer 11 Enterprise Mode
NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now