We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Trying to BitBlt a Bitmap object to another

riceman0
riceman0 asked
on
Medium Priority
242 Views
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

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

        newgraphic.Dispose()
        oldgraphic.Dispose()

End Function
Comment
Watch Question

Author

Commented:

Oops, I forgot to cut-and-paste "return newbmp", which I am using... it's not *that* easy.  :)
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:

Any idea why mine doesn't work?

Commented:
       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.


CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
What is m_bmp.PixelFormat?

Bob
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.