Trying to BitBlt a Bitmap object to another


The function below is in a class that contains a member bitmap object, m_bmp.  The intent is to return a piece of the bitmap as a smaller bitmap object.  This is cribbed and slightly altered from some working code, and I can't figure out how I broke it -- the returned bitmap is just black.

Oh, and I'm not using Clone because Clone may be too smart, and seems to play with the data format... I'm trying dumber alternatives that hopefully won't affect that.

Any ideas?  Thanks.

******

  Public Function Copy(ByVal r As Rectangle) As Bitmap

        Dim newbmp As New Bitmap(r.Width, r.Height, m_bmp.PixelFormat)

        Dim newgraphic As Graphics = Graphics.FromImage(m_bmp)
        Dim oldgraphic As Graphics = Graphics.FromImage(newbmp)

        Dim newhdc As IntPtr = newgraphic.GetHdc
        Dim oldhdc As IntPtr = oldgraphic.GetHdc

        Dim i As Integer = BitBlt(newhdc, 0, 0, r.Width, r.Height, oldhdc, r.X, r.Y, SRCCOPY)  '<-- i returns 1, meaning success

        newgraphic.ReleaseHdc(newhdc)
        oldgraphic.ReleaseHdc(oldhdc)

        newgraphic.Dispose()
        oldgraphic.Dispose()

End Function
riceman0Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

riceman0Author Commented:

Oops, I forgot to cut-and-paste "return newbmp", which I am using... it's not *that* easy.  :)
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You can do this with the Native DrawImage() function of the Graphics class:

        Public Function Copy(ByVal r As Rectangle) As Bitmap
            Dim bmp As New Bitmap(r.Width, r.Height, m_bmp.PixelFormat)
            Dim g As Graphics = Graphics.FromImage(bmp)
            g.DrawImage(m_bmp, 0, 0, r, GraphicsUnit.Pixel)
            g.Dispose()
            Return bmp
        End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
riceman0Author Commented:

Any idea why mine doesn't work?
AlexFMCommented:
       Dim newgraphic As Graphics = Graphics.FromImage(newbmp)
        Dim oldgraphic As Graphics = Graphics.FromImage(m_bmp)

In any case, use the way suggested by Idle_Mind, there is no reason to use PInvoke here.


Bob LearnedCommented:
What is m_bmp.PixelFormat?

Bob
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.