Click here to Skip to main content
15,904,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone i used this code in C# for draw bitmap on Control,Unfortunately, the Drawed background color is gray But my Bitmap is Transparent.Can you help me please?
try
                {
                    hdcDest = grpDest.GetHdc();
                    hdcSrc = grpSrc.GetHdc();
                    hBitmap = bmp.GetHbitmap();

                    hOldObject = SelectObject(hdcSrc, hBitmap);
                    if (hOldObject == IntPtr.Zero)
                        throw new Win32Exception();
                   
                    if (!BitBlt(hdcDest, 0, 0, Width, Height,
                        hdcSrc, 0, 0,SRCCOPY))
                        throw new Win32Exception();
                }
                finally
                {
                    if (hOldObject != IntPtr.Zero) SelectObject(hdcSrc, hOldObject);
                    if (hBitmap != IntPtr.Zero) DeleteObject(hBitmap);
                    if (hdcDest != IntPtr.Zero) ReleaseHdc(hdcDest);
                    if (hdcSrc != IntPtr.Zero) ReleaseHdc(hdcSrc);
                }
Posted
Updated 14-Feb-15 0:09am
v2

You may have to use the AlphaBlend function.
 
Share this answer
 
Doesn't matter i found this,a transparent bitbit.

[System.Runtime.InteropServices.DllImportAttribute ("msimg32.dll")]
       public  static extern bool TransparentBlt (IntPtr hdc, int nXOriginDest, int nYOriginDest, int nWidthDest, int hHeightDest, IntPtr hdcSrc, int nXOriginSrc,  int nYOriginSrc, int nWidthSrc, int nHeightSrc, uint crTransparent);
 
Share this answer
 
Comments
Vladimir Svyatski 16-Feb-15 7:48am    
Well, this one is not exactly drawing 32 bit bmp. It emulates transpanency making pixels with certain color transparent. How do you display properly a pixel with alpha = 128 using this function? Didn't AlphaBlend work? And by the way, why don't you just leverage GDI+ drawing functions like DrawImage here? You're in .NET. I find original GDI useful in .NET only if you're to draw text on controls, because the text drawn with GDI+ differs in size.
Silent Boy 17-Feb-15 10:14am    
Yes Sir, AlphaBlend doesn't Work For Me And Dot Net Controls Have Many flickers.I Used SetStyle ,DoubleBuffer And CreateParams Function For Free flicker and Nothing Changed.BitBit is free flicker Drawing But Only Problem is Transparent Color.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900