.NET Framework Bookmark and Share   
 index > .NET Base Class Library > CreateDIBitmap - Black background
 

CreateDIBitmap - Black background

Hi,

I am currently using the following routine to export an image which has transparent background from an Metafile. I am using the following routine. But the exported images has black background. Also please note that these images are transparent bitmap and pngs.


Is there any way to avoid blackbackground, my intention is to have transparent background.

private Bitmap GetBitmap( int imageOffset, uint imgSize, IntPtr ptr, IntPtr bitmapInfoPtr,
int iUsageSrc )
{
imageOffset -= ( 2 * IntSize );
IntPtr imgPtr = new IntPtr( ptr.ToInt64() + imageOffset );
byte[] imgData = new byte[ imgSize ];
Marshal.Copy( imgPtr, imgData, 0, imgData.Length );

IntPtr hBitmap = GdiApi.CreateDIBitmap(Objects.Handle, bitmapInfoPtr,
WinGdiConst.CBM_INIT, imgData, bitmapInfoPtr, (uint)iUsageSrc);

Bitmap bmp = null;

if( hBitmap != IntPtr.Zero )
{
// Clone image for preventing disposing it before document save.
bmp = Image.FromHbitmap( hBitmap );
bmp = bmp.Clone() as Bitmap;
}
else
{
Debug.WriteLine( "Can't create bitmap" );
}

GdiApi.DeleteObject( hBitmap );

return bmp;
}

Thanks in advance,
Karthik
  • Edited byKarthik_84 Monday, November 03, 2008 10:01 AM
  •  
Karthik_84
Well, that's pretty unclear. We have no idea what pixel format you chose. And GDI doesn't know beans about transparency. Use Bitmap.LockBits() instead. And Bitmap.MakeTransparent() to select a background color as the transparency if you don't have a format that supports alpha.
Hans Passant.
nobugz
Hi Hans,

Regarding the PixelFormat, the created bitmap is in 24bppRGB and not {ARGB}. I have already tried with
Bitmap.MakeTransparent() but it wasn't helped me.

Also regarding my application, my application basically processes metafiles and export its contents to PDF.

I am getting this background color problem, only with the text and images. Also these are
of record type. EMR_ALPHABLEND.

Thanks in advance,
Karthik


Karthik_84
Hi,

I found the following formula from the emf reference manual. This
formula deals with tow bitmaps for alpha-blending. Could anyone let
me know how do I apply the following equation in a single bitmap which
has the black color in place of the transparent areas.

src.Red = src.Red * (SrcConstantAlpha/255.0)
src.Green = src.Green * (SrcConstantAlpha/255.0)
src.Blue = src.Blue * (SrcConstantAlpha/255.0)

dst.Red = src.Red + (1.0 - (src.Alpha/255.0)) * dst.Red
dst.Green = src.Green + (1.0 - (src.Alpha/255.0)) * dst.Green
dst.Blue = src.Blue + (1.0 - (src.Alpha/255.0)) * dst.Blue

If the destination bitmap has an alpha channel, then it is blended as
follows:
src.Alpha = src.Alpha * (SrcConstantAlpha)/255.0)
dst.Alpha = src.Alpha + (1.0 - (src.Alpha/255.0)) * dst.Alpha
Note that if SrcConstantAlpha is 0xFF, there is in effect no
premultiplication of the source values.

Thanks in advance,
Karthik
Karthik_84

You can use google to search for other answers

Custom Search

More Threads

• cannot find my application settings
• Protecting Code Algorithms
• embedding application?
• Visual Basic 2008 in Russian
• How to use Configuration.GetSection() and ConfigurationManager.OpenMappedExeConfiguration()
• Using FileStream to index through bytes in a file
• Problem with COM+ Application
• Decimal question
• Error while building the following code
• Windows Service Reference