Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Drawing / September 2003

Tip: Looking for answers? Try searching our database.

Understanding BitmapData, LockBits, and .BMP files with Alpha channels

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris Dunaway - 26 Sep 2003 20:54 GMT
I am using the code below to read a .bmp file and write it out to a
new file.  This small example just tries to duplicate the file.
Later, I'm going to add code to process the file further.  But this
code illustrates my current problem which is this:

The source file is a .BMP file that has a depth of 32 bpp.  The
additional bits being the Alpha channel.  I can load the source file
in PhotoShop and it correctly reads the file and the Alpha channel.

However, when I load the file using .Net, the file seems to be read in
Format32bbRgb mode instead of Format32bppArgb mode.  Consequently, the
Alpha data in the file is set to &HFF rather than what is in the file.
The alpha channel is all white.  How can I specify that the bitmap
shoudl use the Format32bppArgb mode.  The Bitmap constructor does not
seem to provide for this.

Also, what is the correlation between the data returned by LockBits
and the actual data in the file?  When I look at the file in a Hex
editor, the first pixel's data is (in hex) 89 88 88 AD  for the Blue,
Green, Red, and Alpha channels.  But in the code below when I examine
the byt array in the debugger, it shows the values as (also in Hex) 94
94 94 FF.  Yet, when I examine the output file, the values are (in
hex) 89 88 88 FF.  Does the BitmapData object interpret the data
differently?

Can anyone help here?

Thanks in advance.

Chris

'CODE BEGINS HERE
'\\\\\\\\\\\
Dim sFileName As String = sArgs(0)

Dim bmIn As New Bitmap(sFileName)
Dim bmOut As New Bitmap(bmIn.Width, bmIn.Height, Format32bppArgb)

Dim bmInData As BitmapData = bmIn.LockBits(New Rectangle(0, 0,
bmIn.Width, bmIn.Height), ImageLockMode.ReadOnly, Format32bppArgb)

Dim bmOutData As BitmapData = bmOut.LockBits(New Rectangle(0, 0,
bmIn.Width, bmIn.Height), ImageLockMode.ReadWrite, Format32bppArgb)

Dim byt(3) As Byte
Dim ofs As Integer

For y As Integer = 0 To bmInData.Height - 1
   For x As Integer = 0 To bmInData.Width - 1
       ofs = (bmInData.Stride * y) + (4 * x)
       For i As Integer = 0 To 3
           byt(i) = Marshal.ReadByte(bmInData.Scan0, ofs + i)
       Next

       For i As Integer = 0 To 3
           Marshal.WriteByte(bmOutData.Scan0, ofs + i, byt(i))
       Next
   Next
Next

bmOut.UnlockBits(bmOutData)
bmIn.UnlockBits(bmInData)

bmOut.Save("Outtest.bmp", Imaging.ImageFormat.Bmp)

bmIn.Dispose()
bmOut.Dispose()
'//////////
Bob Powell [MVP] - 27 Sep 2003 15:05 GMT
The following article explains the LockBits method and the BitmapData class.

http://www.bobpowell.net/lockingbits.htm

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

> I am using the code below to read a .bmp file and write it out to a
> new file.  This small example just tries to duplicate the file.
[quoted text clipped - 64 lines]
> bmOut.Dispose()
> '//////////
Michael Phillips, Jr. - 27 Sep 2003 15:26 GMT
The memory layout of a bitmap is as follows:
BITMAPFILEHEADER
BITMAPINFOHEADER
Color Table, If any or Color Masks if BI_BITFIELDS is set.
Bitmap bits

Support for Alpha is not possible in the above.  However,
Microsoft introduced the BITMAPV5HEADER which
does support a Bitmap with an Alpha channel in Windows 2000,
Windows XP, and Windows 2003 Server.

You may load a Bitmap created by PhotoShop with an Alpha
channel by substituting a BITMAPV5HEADER in place of
the BITMAPINFOHEADER.  You will have to open the file
yourself and and read and interpret the structures before
creating your gdi+ bitmap with Format32bppArgb.

The format32bppArgb specifies an unsigned integer
organized as AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB.

If you read or write as bytes instead of an unsigned integer, you will
read in the following order: BBBBBBBB ... AAAAAAAA

> I am using the code below to read a .bmp file and write it out to a
> new file.  This small example just tries to duplicate the file.
[quoted text clipped - 64 lines]
> bmOut.Dispose()
> '//////////

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.