?
Solved

Trying to BitBlt a Bitmap object to another

Posted on 2006-04-01
8
Medium Priority
?
229 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
0
Comment
Question by:riceman0
5 Comments
 

Author Comment

by:riceman0
ID: 16350611

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

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 16351644
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
 

Author Comment

by:riceman0
ID: 16351688

Any idea why mine doesn't work?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16353176
       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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16360998
What is m_bmp.PixelFormat?

Bob
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

807 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