-
February 11th, 2010, 05:28 PM
#1
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);
-
February 11th, 2010, 06:49 PM
#2
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
-
February 12th, 2010, 04:18 AM
#3
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.
-
February 12th, 2010, 04:37 AM
#4
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.
-
February 12th, 2010, 02:19 PM
#5
Re: Saving image from hDC
-
February 12th, 2010, 03:05 PM
#6
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.
-
February 12th, 2010, 03:27 PM
#7
Re: Saving image from hDC
Originally Posted by BigEd781
...in the future it would hep if you told us what you were trying to accomplish.
Originally Posted by edgebird
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
-
February 12th, 2010, 04:01 PM
#8
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.
-
February 12th, 2010, 04:12 PM
#9
Re: Saving image from hDC
Originally Posted by BigEd781
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
-
February 12th, 2010, 07:54 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|