• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

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
0
riceman0
Asked:
riceman0
1 Solution
 
riceman0Author Commented:

Oops, I forgot to cut-and-paste "return newbmp", which I am using... it's not *that* easy.  :)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
riceman0Author Commented:

Any idea why mine doesn't work?
0
 
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.


0
 
Bob LearnedCommented:
What is m_bmp.PixelFormat?

Bob
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now