Quickly Dimming an Arbitrary Bitmap in Compact Framework for VB.Net

I had a need to directly manipulate the bytes in a bitmap for my VB.Net app for Windows Phone.  I found quite a few articles with The Google on how to do this in C#, using the unsafe keyword.  That's a perfectly fine way of doing it for C# users.  However, VB.Net does not have anything like the unsafe keyword, so there is no way to run unsafe (unmanaged) code blocks.  Philosophically, this might be a "good" thing to Managed Code purists.  I'm actually neutral on the issue.  Still, just because you can't do arbitrary pointer and memory manipulation in VB.Net does not mean you can't do this very simple thing (dimming the color brightness of each byte of a bitmap).  It's pretty easy and I can't believe nobody out there has posted how to do it this way VB.Net or C#.  I came up with this code for the Compact Framework for Windows Phone specifically, but it will also work in the big Framework for big Windows.  It will work in both VB.Net and C# just fine.

The key to this method is using the Marshal.Copy method.  This copies a block of memory from an IntPtr to various types of arrays and then back again.  Whereas the unsafe method directly manipulates the bytes in the bitmap, this code block copies the unmanaged bytes to an array, manipulates them, then copies them back to the unmanaged buffer.  Using the 'unsafe' method is likely a hair faster, but in real world performance metrics, even on Windows Phone you would be hard pressed to see any noticeable difference between the two.  Copying blocks of memory is a very fast operation.

This code adjusts the brightness of a bitmap, dimming it by half (a bit shift of >>1 for each color component of each pixel).  Windows Phone natively uses 16bit bitmaps ('565' format, as in RRRRRGGGGGGBBBBB).  The below code block can be adjusted to work with 24 and 32 bit bitmaps as needed.  It's actually easier to use 24 and 32 bit bitmaps as you just manipulate the brightness of  individual Byte primitives without needing to break apart the Int16 (short) using bit masks, as below.  However, the act of converting the 16 bit bitmap to a 32 bit bitmap on Windows Phone seems to cause a significant a performance hit!  So don't do that on Windows Phone.  Stay with the 16-bit bitmap and use bit masks, as I did below.  For big Windows, you will want to use 32 bit bitmaps, not 16 bit bitmaps.

Private Sub DimBitmapByHalf(ByRef bmp As Bitmap)
  Dim bmd As Imaging.BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format16bppRgb565)
    Dim bufLen As Integer = (bmd.Stride * bmd.Height) / 2 ' Because bmd.Stride and bmd.Height are in units of 8-bit bytes
    Dim buf(bufLen - 1) As Short
    Dim r As Short
    Dim g As Short
    Dim b As Short

    System.Runtime.InteropServices.Marshal.Copy(bmd.Scan0, buf, 0, bufLen)
    For i = 0 To bufLen - 1
      '                              RRRRR GGGGGG BBBBB
      r = (buf(i) >> 1) And &H7800 ' 01111 000000 00000
      g = (buf(i) >> 1) And &H3E0  ' 00000 011111 00000
      b = (buf(i) >> 1) And &HF    ' 00000 000000 01111
      buf(i) = r Or g Or b
    System.Runtime.InteropServices.Marshal.Copy(buf, 0, bmd.Scan0, bufLen)
  End Try
End Sub


Water on the Moon! But...

I love science!  NASA has confirmed that the projectile launched into the moon's surface that offered a less than Hollywood spectacular boom uncovered 24 gallons of water.  That is so awesome!  Water on the moon has many benefits.  First, astronauts can drink it.  They can also breath the oxygen, once the water molecule is broken apart.  Finally, the hydrogen can be used for rocket fuel.  Since all these essential resources do not need to be taken to the hypothetical future moon base, a ton of money can be saved!

But with every bit of good news, seems to come a lot of bad, these days.  NASA's Ares rocket system (part of the Constellation program) is apparently over budget and the Obama administration is trying to figure out what to do about this.  This is sad.  After health care and jobs, science is a top priority in my book.  And highly visible, highly inspirational science like the space program should be a huge priority.

Please write the president and tell them how important the space program is to human kind.  Some good talking points would include the number of jobs created by the Ares program and potential future moon missions, the inspiration to the next generation of kids to get interested in science, and the importance of the USA becoming the space leader again. 

The money needed is a drop in the bucket compared to the perpetual needless wars we are involved in.  Only three billion more dollars is needed each year.  Compare that to the nearly one trillion dollars spent so far on killing people in the middle east.

If we don't lead the world on space exploration, it appears China will be glad to fill that role


My DNS Provider Was Hacked

If you tried to visit the Avian Waves website in the last 24 hours, you may have noticed that a spam page (with an embedded virus download) came up instead.  My site, itself, was not hacked.  No, it was much worse.  The DNS provider at Enom (the company the registrar I use, Arbor Domains, is a reseller for) was hacked.  A very large number of sites had their DNS redirected.

Time to switch to GoDaddy.  They are cheaper anyway.  Enom has proven they cannot be trusted with security.  Sorry, Arbor.  It's been a great ten plus year run.  But your DNS and registration tools haven't been updated since the early 2000s.  Plus, I need DNS service I can trust to not be hacked.  And if it is hacked, it needs to be fixed ASAP.


How Come Nobody Told Me The Prisoner Was Being Reimagined?!

Wow!  This is shaping up to be  a good season for teevee science fiction!  First, we get a remade V (which didn't disappoint, although the first episode wasn't as strong as the original, IMHO), now I see that The Prisoner (an absolutely iconic science fiction series from the 60s) is being remade for AMC.  Go AMC!  Go teevee!


More Energy Lobby Sock Puppets!

For several years now, BP has subjected us to the endless "common sense" of its scripted sock puppets on the teevee machine.  Now the Natural Gas lobby has started the same type of ad campaign.  Did you know we have over 100 years of Natural Gas?!  ZOMG BURN IT ALL UP NOW BABY!  SCREW THE ENVIRONMENT!

For crying out loud.  Give me Solar, damn it.  Screw your fossil fuels.



I am a big fan of the original mini-series, V, that aired in 1983.  I was pretty young back then, so my recollection of the series is primarily through re-runs on Syfy years ago before the channel had a stupid name.  The mini-series was gripping and original.  The WWII analogies were strong and well written, and the character reactions of the story were very plausible.  In addition, the main protagonist was a cameraman.  Give it up for the geeks!  The story ended with a victorious battle, but the war was very far from over.  There were lots of open ended questions and cliff hangers.

The next year, a sequel mini-series was made, V: The Final Battle.  It completely sucked1.  They either ignored or quickly wrapped up the very dramatically laid loose ends from the previous year in very unsatisfactory ways.  The story devolved from a WWII analogy into more of a religious analogy, with the hybrid human-visitor having magical Christ-like powers that neither parent possessed (seriously, wtf?).  And they had Michael Ironside(Has your favorite show jumped the shark?  If Michael Ironside was just added to the cast then the answer is yes!)   And to top it all off, the humans beat the visitors with the most lame War of the Worlds deus ex machina2 rip-off ending ever – complete with like fifteen minutes of stock footage cheering and rejoicing!  After that, NBC started a series sequel to V.  The visitors quickly developed an immunity to the poison dust, lost their warbled distorted voices (no explanation given), regained rule over the planet with little resistance, and the series continued to suck and disappoint like only a series that jumps the shark before it even begins can!

Anyway, so there's a new V series starting tomorrow.  I'm very excited about it.  The whole premise of V is very rich and exciting.  It's unique in that the aliens don't just nuke cities a la Independence Day, but instead use politics, torture, mind control, and media manipulation to gain control of Earth.  I think the WWII parallels also create a very eerie sense of "omg, dude, this could totally really actually happen" since in human history it did actually happen (sans aliens) in Germany.

The one thing that concerns me about the new V series is that the synopsis sounds more like the reimagined Battlestar Galactica than the old V.  There is plenty of room for the writers to come up with great storylines, don't get me wrong, I just hope that it does the original honor with a lot of manipulation and very little nuking.  Battlestar already thoroughly explored the human condition (IN SPACE) with regard to modern problems (terrorism, modern warfare, torture, etc.).  I want a strong manipulative invasion story, where humans are pitted against humans and the visitors are pulling the strings.  Here's hoping it doesn't suck!  (Reviews are good so far, so I'm excited!)

1. The Final  Battle was apparently made without much input from original "V" mythos creator, Kenneth Johnson.  Mr. Johnson eventually published a book in 2008 which contained (I would presume) his preferred ending to the V story, ignoring the entire Final Battle and subsequent series storylines completely.  I need to read this.

2. I should point out that War of the Worlds is not lame, despite the lameness of the similar ripped-off ending used in V: The Final Battle.


My Letter to Senator Hagan Regarding the American Health Security Act of 2009

Senator Hagan - I am writing this letter to urge you to support Senator Bernie Sanders' American Health Security Act of 2009, which is a Single Payer health care bill.  I have no delusions of this bill's ability to pass the Senate - it surely will not. 

The reason to vote in favor of it is to measure the support in the Senate of a single payer style health care bill.  This is something that likely will not be able to pass for decades, but what Sen. Sanders is doing is important because it gives us a baseline to see how many votes we have now, and how many we need to work for in the future.

I am currently 32 years old.  I hope to see a single payer system implemented in the USA before I die.  This is a fantastic first step.

Please show the rest of the country that North Carolinians are progressive Southerners -- not knee-jerk-reaction head-in-the-sand conservatives.

Please support the American Health Security Act of 2009.


Mystery of the Server 2008 + IIS7 + OLE = "MDAC Not Installed" Error

Talk about barking up the wrong tree!

I just solved a very peculiar error regarding migrating old ASP.Net OLEDB Excel/Access file access code to a new 64-bit Windows Server 2008 under IIS7.  The first steps for migrating this application were obvious.  The application pool needs to run under 32-bit compatibility mode, the pipeline needs to be classic, etc.  But when we got to the Excel export functionality, all hell broke loose.  No matter what we did, we would get the following exception.

Exception of type 'System.Web.HttpUnhandledException' was thrown. [System.Web.HttpUnhandledException]
The .Net Framework Data Providers require Microsoft Data Access Components(MDAC). Please install Microsoft Data Access Components(MDAC) version 2.6 or later. [System.InvalidOperationException]
Retrieving the COM class factory for component with CLSID {2206CDB2-19C1-11D1-89E0-00C04FD7A829} failed due to the following error: 800703fa. [System.Runtime.InteropServices.COMException]

The error was a red herring.  The problem is not that MDAC is not installed – it is far more obscure.  WDAC (MDAC's successor) is included in Windows Server 2008 and has plenty of backward compatibility built into it.  That part of the equation is perfectly fine.  In fact, if you run console/forms apps, you probably will never see this problem.  It is only when you are running ASP.Net code and use a custom identity for the Application Pool (possibly some other situations too) that this error occurs.

By doing a ProcMon trace, I was able to pin down the problem.  If you follow all the steps that w3wp.exe performs, you will see one really weird line.

Line from ProcMon 

Okay, the unknown result code is weird enough, but ProcMon couldn't even tell me which registry key it couldn't find!  Examining the stack trace confirmed that this was the only request logged that had ole32.dll loaded, plus it showed ole32.dll throwing an error, so it really appeared to be related to my problem.

ProcMon stack trace

Thankfully, the Great Gazoogle did not let me down.  It turns out that 0xC0000425 indicates that an application was attempting to access a registry hive  after it was unloaded.  What the heck?

This is a good time to explain how the registry works at a very high (and simple) level.  Each registry hive is a self-contained database in an independent file in the file system.  A good example is NTUSER.DAT located in your user profile folder.  That's your very own HKCU.  Not all of the registry is always loaded all the time.  This is what allows Windows to move your profile around, or copy your profile (registry included) without having to do individual registry read/writes (much slower).

If HKLM or HKCR were unloaded while Windows was running, your computer would probably just freeze up completely, so those were not good candidates for this problem.  It had to be a user hive.  Since it was the application pool identity that got this error, it had to be that user's profile.  But how can its own registry get unloaded in the middle of execution?

I found the answer at a Sun support forum, of all places.  It turns out that in previous versions of Windows, you could download a program called UPHClean that would help unload user profiles when they were no longer needed (supposedly) to avoid the annoying user profile deadlocks that would sometimes occur, causing you to have to use a temporary profile, instead of your own user profile.  If you haven't experienced this error before, you are lucky!  It is rare, but when it occurs, it is very annoying.  It's apparently more common on Terminal Servers than other servers, from what I've seen.

Well, in Server 2008, the functionality of the UPHClean service has been rolled into the User Profile service, which always runs.  So Server 2008, on the whole, more aggressively shuts down dangling registry handles when a user logs off.  The aforementioned support forum participant recommended shutting off the UPHClean functionality entirely.  I'm not a big advocate of fixing shovel-sized problems with a back ho, so I decided to dig a little more.

To see if this really was the cause of my woes, I searched for Event ID 1530 in the event log to see a user's HKCU was ever forcefully unloaded.  Yes, there have been occurrences, yes the process involved was w3wp.exe, and yes the SID corresponds to the application pool identity.  I'm getting closer to a solution!

Windows detected your registry file is still in use by other applications or services. The file will be unloaded now. The applications or services that hold your registry file may not function properly afterwards. 

2 user registry handles leaked from \Registry\User\S-1-5-21-1129910693-4165624395-2147873099-2135_Classes:
Process 6632 (\Device\HarddiskVolume1\Windows\SysWOW64\inetsrv\w3wp.exe) has opened key \REGISTRY\USER\S-1-5-21-1129910693-4165624395-2147873099-2135_CLASSES\Wow6432Node
Process 10144 (\Device\HarddiskVolume1\Windows\SysWOW64\inetsrv\w3wp.exe) has opened key \REGISTRY\USER\S-1-5-21-1129910693-4165624395-2147873099-2135_CLASSES\Wow6432Node

So why was this happening?  Clearly my application has not exited.  Why does the User Profile Service think it can arbitrarily close that registry hive while my application pool is still doing work?  I'm guessing that UPHClean detects the open handles and when enumerating loaded profiles, sees that the user's profile is not loaded, so decides that those open handles must be dangling and should be forcefully closed.

The solution to preventing this problem turns out to be a very simple configuration setting in IIS7's Application Pool Advanced Settings.  To fix this problem, all you need to do is enable profile loading in IIS.  What this does is tell IIS to completely load the user profile for the entirety of the execution of the application.  The default approach is the IIS6 approach, where the profile is not loaded when the user is impersonated by the IIS service.  In IIS6, this was okay because on Server 2003 UPHCLean was an optional download and not installed by default.  If it's not installed, it can't unload the hive, obviously.  :-) I would imagine if you did install it on 2003, you would have a similar problem in a similar situation.

IIS7 advanced settings

So, in conclusion, no profile loading means premature registry unloading, which means no OLE, no MDAC, and, therefore, no Excel/Access.

Mystery solved.


Fixing Slow .Net Compact Framework Windows Mobile Compilation Times

A colleague of mine found a great article with a tip to significantly speed up the compilation time of large .Net Compact Framework projects.  I was just living it with, thinking there was nothing I could do.  I'm so glad he found that article!  It is helping speed up development of my mobile hobby projects significantly.  The slowness is due to a verification step that ensures that your application complies with its target.  Obviously during debugging, this step does not need to be executed every single time you build!  The article suggests creating a new environment variables, but I think a better approach is to use the $(ConfigurationName) variable.  When I compile for release, I want this check.  For debug, I don't.  So go read the article!  Then, if you like, take my suggestion and change the condition to...

Condition="'$(ConfigurationName)' != 'Debug'"


Sunday Night Football

I didn't think anything could be more annoying than the Monday Night Football theme song.  Then NBC created a carbon copy (but worse) semi-country, semi-rock song sung by somebody sort of famous, but highly annoying for Sunday night.  Because, yes, it is entirely MNF's song that makes it successful in prime time and not an exciting football game itself!

Teevee executives are retarded.

(Yes, I know NBC's SNF is not exactly new, but this has been something I've been meaning to complain about since they took over in 2006. lol.)


Avian Waves on Twitter Avian Waves on Facebook Avian Waves on Spotify

Recent Comments
  1. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Camilo: Edward comment solved the issue! After installing the 16.5.3 (from the link provided and without uninstalling...
  2. Re: It's Coming Any Day Now...
    Tim: Awesome to hear. Can't wait to check it out.
  3. Re: RD Tabs Security Advisory - 2.0 and 2.1 Beta
    Roman: Hi admin having same materiel as i need. Also get some extra stuff here: [url="hit5k.com"]Patch Applications...
  4. Re: RD Tabs Security Advisory - 2.0 and 2.1 Beta
    Roman: Hi admin having same materiel as i need. Also get some extra stuff here: Patch Applications
  5. Re: 3.0 is coming...
    Sean: Great news, can't wait!
  6. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Funny Guy: To add my 2 cents - after a day of fight it appears that DPM installation uses WMI queries to detect...
  7. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Funny Guy: To add my 2 cents - after a day of fight it appears that DPM installation uses WMI queries to detect...
  8. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Funny Guy: To add my 2 cents - after a day of fight it appears that DPM installation uses WMI queries to detect...
  9. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    kAM aCOSTA: Thanks Edward !!!
  10. Re: 3.0 is coming...
    Dave: Very Cool!