I have a conceptually simple task:
  1. Read in two TIFF images
  2. Overlay the second image onto the first
  3. Save as a new TIFF

One catch: The first TIFF is 40,000 pixels by 30,000 pixels, 8-bit indexed color. Yes, I need a lot of memory. I'd like to get to that later. (The second TIFF is also 8-bit.)

I've found GDI+ (System.Drawing) to be incredibly restrictive. I cannot create a Graphics object from an 8-bit indexed color bitmap, for one thing (something that would help with memory).

So I am trying to learn WPF (System.Windows.Media.Imaging). I've been following several examples from MSDN and looking all around but nothing is working. Here's what I have so far:

Code:
        Dim workingDir As String = "C:\temp"
        Dim baseimg As String = "base.tif"
        Dim overlayimg As String = "overlay.tif"
        Dim outputFile As String = "output.tif"

        Dim dg As New DrawingGroup()
        Dim imgBase As New BitmapImage(New Uri(workingDir + "\" + baseimg))
        Dim imgOverlay As New BitmapImage(New Uri(workingDir + "\" + overlayimg))
        Dim rtb As New RenderTargetBitmap(200, 200, 200, 200, PixelFormats.Default)
        Dim dv As New DrawingVisual()
        Dim dc As DrawingContext = dv.RenderOpen()
        dc.DrawRectangle(
            New ImageBrush(imgBase),
            New Pen(Windows.Media.Brushes.Navy, 2),
            New Windows.Rect(10, 10, 100, 100)
        )
        rtb.Render(dv)

        Dim tif As New TiffBitmapEncoder()
        tif.Frames.Add(BitmapFrame.Create(rtb))
        Dim stm As System.IO.Stream = System.IO.File.Create(workingDir + "\" + outputFile)
        tif.Save(stm)
        stm.Dispose()
I think the saving part is working. Well, I get a TIFF image that is completely black (possibly transparent). It's as if rtb has nothing rendered in it.

Can someone lay out the basics for me here, or point me to a website that addresses this solution? Thanks.

Oh yeah, about memory. If I try to create rtb with 40,000x30,000, I get a parameter out of range exception or something. My hunch is it's a memory exception. (My 64-bit system has only 4GB.) Would streaming the files and merging the pixels individually be a better solution? With GDI+ there is LockBits and WriteableBitmap seems to be an option with WPF, but those two are beyond my skills at this moment. Help appreciated. Thanks!