Results 1 to 4 of 4

Thread: Troubleshooting with Alphablend and TransparentBlt

  1. #1
    Join Date
    Jun 2011
    Posts
    91

    Troubleshooting with Alphablend and TransparentBlt

    Hey can anyone help me? I have a bit troubleshooting with the TransparentBlt and AlphaBlend. Actually my objective is draw a bitmap using TransparentBlt on to a memory device so that I can blend it with the device context. To create the bitmap and devices I have tried the below attempts such as:
    Code:
    //Create the bitmaps and devices
    HBITMAP oldBMP,bitBMP;
    HDC memdc,transparentdc;
    
    //Initiate the devices
    transparentdc = CreateCompatibleDC(NULL);
    memdc = CreateCompatibleDC(NULL);
    
    //Select the bitmaps into devices...
    oldBMP = (HBITMAP)SelectObject(transparentdc, bitmap);
    //Create a empty bitmap for use when transfering bits.. this can the error point. Don't thinks so still.
    bitBMP = CreateCompatibleBitmap(hdc,width,height);
    SelectObject(memdc,bitBMP);
    
    if(transparent)//Om jag har en transparentpixel(color of my choice)
    //getPixelColor is local funtion that gets color of a pixel... works..
    {
    
    if(!TransparentBlt(memdc,0,0,width,height,transparentdc,0,0,width,height,getPixelColor(transparentpixel.x,transparentpixel.y)))
    {
    MessageBox(NULL,"Transparent","TransparentBlt Error ",MB_OK);
    
    }
    
    }
    else
    {
    //If there is no transparentpixel just ship it of...
    
    BitBlt(memdc,0,0,width,height,transparentdc,0,0,SRCCOPY);
    
    }
    
    if(alphablend)//if there are some alphablend to be done...
    {
    
    BLENDFUNCTION bf;
    bf.AlphaFormat = 0;
    bf.BlendFlags = 0;
    bf.BlendOp = AC_SRC_OVER;
    bf.SourceConstantAlpha = alphavalue;
    
    AlphaBlend(hdc,x,y,width,height,memdc,0,0,width,height,bf); 
    }
    else
    {
    
    BitBlt(hdc,x,y,width,height,memdc,0,0,SRCCOPY);
    
    }
    
    // Now if alphablend and transparent was set there should be a bitmap that has been removed of transparent color and alphablended...
    //the alphablend part is correct... but the transparentBlt doesn't work..
    
    SelectObject(transparentdc, oldBMP);
    DeleteDC(memdc);
    DeleteDC(transparentdc);

  2. #2
    Join Date
    May 2009
    Posts
    527

    Re: Troubleshooting with Alphablend and TransparentBlt

    I have very well understood what you are trying to say. I have tried the below coding , it worked fine in my Windows XP operating system :
    Code:
    void Draw(HBITMAP hBitmap, BITMAP bBitmap, HDC hDC, long Left, long Top, long Width, long Height,
    	long rectLeft, long rectTop, long RectWidth, long RectHeight, const COLORREF Color, BYTE Alpha)
    {
    	HDC BufferDC = CreateCompatibleDC(hDC);
    	HBITMAP hBitmapBuffer = CreateCompatibleBitmap(hDC, Width, Height);
    	HBITMAP hOldBitmap = (HBITMAP)SelectObject(BufferDC, hBitmapBuffer);
    
    	HDC SrcDC = CreateCompatibleDC(BufferDC);
    	GetObject(hBitmap, sizeof(BITMAP), &bBitmap);
    	SelectObject(SrcDC, hBitmap);
    	TransparentBlt(BufferDC, 0, 0, Width, Height, SrcDC, rectLeft, rectTop, RectWidth, RectHeight, Color);
    	DeleteDC(SrcDC);
    
    	BLENDFUNCTION BlendFunction;
    	BlendFunction.AlphaFormat = AC_SRC_ALPHA;
    	BlendFunction.BlendFlags = 0;
    	BlendFunction.BlendOp = AC_SRC_OVER;
    	BlendFunction.SourceConstantAlpha = Alpha;
    
    	AlphaBlend(hDC, Left, Top, Width, Height, BufferDC, 0, 0, Width, Height, BlendFunction);
    
    	SelectObject(BufferDC, hOldBitmap);
    	DeleteDC(BufferDC);
    	DeleteObject(hBitmapBuffer);
    }

  3. #3
    Join Date
    Apr 2009
    Posts
    488

    Re: Troubleshooting with Alphablend and TransparentBlt

    According to me a function draws a part of an image or the whole image scales itself and makes a color transparent. It draws the image with alpha blend on the given HDC. Since you have not mentioned about what operating system you use so would like to make you aware that on Windows7 the AlphaBlend function does not work. So to solve this issue you can set AlphaBlend to 0. After setting AlphaBlend to 0 you can easily use the above code. For example
    Code:
    {
    	AlphaBlend =0;
    
    	SelectObject(BufferDC, hOldBitmap);
    	DeleteDC(BufferDC);
    	DeleteObject(hBitmapBuffer);
    }

  4. #4
    Join Date
    May 2009
    Posts
    539

    Re: Troubleshooting with Alphablend and TransparentBlt

    Those who are having this same kind of problem, I want to make them aware that the positions Left, Top, Width and Height of an image are drawn. Also from it the size of an image is drawn. The parameters actually are the piece of the image that you want to draw. Before creating a bitmap you should be aware of these important points and apply them in your coding. For example if you have an image of 800x600 and you want to draw it on a surface of 1024x768 with red transparent color and then alpha blend it 50%, it code will be :

    Code:
    Draw(hBitmap, bBitmap, hDC, 0, 0, 1024, 768, 0, 0, 800, 
    600, RGB(255, 0, 0), 128);

Similar Threads

  1. Troubleshooting msi KT6V
    By Jagathi in forum Motherboard Processor & RAM
    Replies: 6
    Last Post: 07-06-2011, 01:28 PM
  2. Ram not detected (Troubleshooting)
    By Chickasaw in forum Motherboard Processor & RAM
    Replies: 6
    Last Post: 28-09-2010, 06:08 AM
  3. Troubleshooting ICQ on MAC
    By Aadi007 in forum Technology & Internet
    Replies: 5
    Last Post: 13-02-2010, 01:48 PM
  4. HS-600 microphone troubleshooting
    By Banjiji in forum Hardware Peripherals
    Replies: 3
    Last Post: 03-07-2009, 11:50 PM
  5. Ram Troubleshooting
    By monsitj in forum Hardware Peripherals
    Replies: 2
    Last Post: 30-07-2008, 03:59 PM

Tags for this Thread

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
  •  
Page generated in 1,750,009,738.16054 seconds with 16 queries