Tute
Tute

Reputation: 7013

How to use 32bit alpha-blended BMP in .Net

Is there any way to use this kind of format in .Net (C#)? I want to use the same skin format that uTorrent uses in my app, but i can't get the transparent background. Any ideas? Thanks for your time.

Upvotes: 4

Views: 2335

Answers (3)

NetForeverOften
NetForeverOften

Reputation: 21

While the BMP format does support an alpha channel, Windows ignores it. Luckily, the format is rather simple and you can read it using System.IO.BinaryReader then create a Drawing.Bitmap using the LockBits and UnlockBits methods to write the data to it.

Upvotes: 2

Ishmaeel
Ishmaeel

Reputation: 14373

You need to do two things:

  • Copy your bitmap onto the form as background.
  • Call the UpdateLayeredWindow (user32.dll) to enable per-pixel alpha transparency.

The code is a liitle bit bulky, but here is a very nice sample application with source code: Per Pixel Alpha Blend in C#

Upvotes: 3

samjudson
samjudson

Reputation: 56853

The PixelFormat enumeration lists the formats of 'bitmaps' you can create in .Net, so you'd want PixelFormat.Format32bppArgb:

http://msdn.microsoft.com/en-us/library/system.drawing.imaging.pixelformat.aspx

http://msdn.microsoft.com/en-us/library/3z132tat.aspx

However I'm not entirely sure that the BMP file format supports transparency - so you would have to save the file as perhaps a PNG file instead.

Upvotes: 2

Related Questions