Trying to BitBlt a Bitmap object to another

Posted on 2006-04-01
Last Modified: 2012-05-05

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



End Function
Question by:riceman0

    Author Comment


    Oops, I forgot to cut-and-paste "return newbmp", which I am using... it's not *that* easy.  :)
    LVL 85

    Accepted Solution

    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)
                Return bmp
            End Function

    Author Comment


    Any idea why mine doesn't work?
    LVL 48

    Expert Comment

           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.

    LVL 96

    Expert Comment

    by:Bob Learned
    What is m_bmp.PixelFormat?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now