CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2010
    Posts
    6

    Question Saving image from hDC

    Hello,

    I'm a beginner in C# and I'm making an application for fun.
    This application however requires me to do one hard thing with which I hope you guys can help.
    (Small background: I hook BitBlt and would like to copy the source image thus getting source & destination IntPtr)

    I get a hDC of a bitmap(you call it like that?) in the memory and I would like to convert it to a Bitmap so I can confirm some actions on it and eventually save it. However I really don't get see how I can do this. I searched on google and msdn for hours but couldn't come up with anything. I found some procedures which require me to call BitBlt but it really didn't make me any wiser. So I hope somebody might post some example code.


    Thanks already.

    Ps:
    This is the only thing I could come up with after searching msdn, but it doesn't seem to work:

    Code:
    
    //IntPtr hObjSource;
    System.Drawing.Bitmap bitmap = System.Drawing.Image.FromHbitmap(hObjSource);
    // And then use bitmap.save(x,y,z);
    

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Saving image from hDC

    So first I would ask if interop is really necessary here. Marshalling complex data structures can be tough, and while this can be achieved, it may be unnecessary. So, perhaps you could describe the larger problem to us.

    Aside from that, you can create Bitmap objects from the pixel data. First tell us what "doesn't work" about using the FromHBitmap method. Have you read the documentation? What exactly doesn't work? You can also refer to the following article.

    http://www.vbforums.com/showthread.php?t=358917

  3. #3
    Join Date
    Feb 2010
    Posts
    6

    Re: Saving image from hDC

    Thanks for your reply. Hope this can clarify it a bit.

    Code:
            // Interception
            static bool DBitBlt_Hooked(IntPtr hObject, int nXDest, int nYDest, int nWidth,int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop)   
           {
                try
                {
                    Main This = (Main)HookRuntimeInfo.Callback;
                    lock (This.capBitBlt)
                    {
                        This.capBitBlt.Push("Start");
                        System.Drawing.Bitmap image1;
                        image1 = new System.Drawing.Bitmap (System.Drawing.Image.FromHbitmap(hObjSource));
                        
                        This.capBitBlt.Push("End");
                        // bitmap.Save();
                    }
    
                    // Gets the "bits" from the bitmap and copies them into a buffer 
                    // which is pointed to by lpbitmap.
    
                }
                catch
                {
                }
                // Calling original BitBlt();
                return BitBlt(hObject, nXDest, nYDest, nWidth, nHeight, hObjSource, nXSrc, nYSrc, dwRop);
            }
    I use easyhook to capture the calls. Seeing the fact only 'Start' gets displayed and not 'End' I know it crashes.. (lame method but yeahh.. told you i'm beginner ).
    So I still would like to get the bitmap hope someone can help me.

  4. #4
    Join Date
    Feb 2010
    Posts
    6

    Re: Saving image from hDC

    I forgot to mention that I looked at our URL but they all require you to have a bitmap already whereas I only got a hDC.

  5. #5
    Join Date
    Feb 2010
    Posts
    6

    Re: Saving image from hDC

    Solved the problem.
    This may be usefull to someone
    http://www.codeproject.com/KB/graphi...rescreen1.aspx

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Saving image from hDC

    Ok, glad to know that you have it solved, but in the future it would hep if you told us what you were trying to accomplish.

  7. #7
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Saving image from hDC

    Quote Originally Posted by BigEd781 View Post
    ...in the future it would hep if you told us what you were trying to accomplish.
    Quote Originally Posted by edgebird View Post
    I get a hDC of a bitmap(you call it like that?) in the memory and I would like to convert it to a Bitmap
    no harm meant but wasn't it clear enough?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Saving image from hDC

    I meant that I would have found more relevant examples if I had known that the goal was to capture a screen shot. When you narrow your request to a specific implementation you do not allow for other alternatives which may be better solutions.

  9. #9
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Saving image from hDC

    Quote Originally Posted by BigEd781 View Post
    I meant that I would have found more relevant examples if I had known that the goal was to capture a screen shot.
    good point.

    @edgebird: are you really taking a screenshot or is the example at codeproject only relevant because it shows how to utilize this hDC?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  10. #10
    Join Date
    Feb 2010
    Posts
    6

    Re: Saving image from hDC

    Only relevant because it shows how to utilize the hDC.
    I hook every BitBlt call the injected application makes and retrieve the source image(to whom i have the hDC).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured