 |
|
 |
Hi,
I have 2 questions:
1) I have tried to use your code to enhance my application to save the current frame to bitmap but I encounter the following error:
>d:\visual studio 2005\projects\windowsformscpp\windowsformscpp\Form1.h(292) : error C2665: 'System::Drawing::Image::FromHbitmap' : none of the 2 overloads could convert all the argument types
1> c:\winnt\microsoft.net\framework\v2.0.50727\system.drawing.dll: could be 'System::Drawing::Bitmap ^System::Drawing::Image::FromHbitmap(System::IntPtr)'
1> while trying to match the argument list '(HBITMAP)'
2) Can I avoid to use pictureBox1 object and save the bitmap to file directly from hMemDC
Below you have a listing with my code:
HDC hDC;
HWND hWnd=(HWND)this->Handle.ToInt32(); //Get HWND of form
hDC = GetWindowDC(hWnd); //Now get it's DC handle
HDC hMemDC = CreateCompatibleDC(hDC);
RECT r;
GetWindowRect((HWND)this->Handle.ToInt32(),&r); //need this for Form
SIZE size;
size.cx = r.right-r.left;
size.cy = r.bottom-r.top;
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, size.cx, size.cy);
if (hBitmap)
{
HBITMAP hOld = (HBITMAP) SelectObject(hMemDC, hBitmap);
BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, hOld);
pictureBox1->Image = Image::FromHbitmap(hBitmap);
//pbox->Image->Save("C:\\Z.Jpg", Drawing::Imaging::ImageFormat::Jpeg);
DeleteDC(hMemDC);
ReleaseDC(NULL, hDC);
DeleteObject(hBitmap);
}
|
|
|
|
 |
|
 |
I assume you've correctly converted from MC++ to C++/CLI.
For (1) you may need to cast to (IntPtr). Try:
Image::FromHbitmap((IntPtr)hBitmap);
And for (2) FromHbitmap returns a Bitmap object on which you can directly call Save.
|
|
|
|
 |
|
 |
Hi Nish,
thank you very much for your suggestions.
Now the program is working without errors.
To save the image I used the following syntax:
Bitmap bmp=Image::FromHbitmap((IntPtr)hBitmap);
bmp.Save("C:\\Z.Jpg", Drawing::Imaging::ImageFormat::Jpeg);
But the problem is that I save now the Visual C environment in the picture and not the graphics that is drawn in the Form1.
I used your code in the Form1.h, Form1_Load function (private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)).
I have tried to use your code in WindowsFormsCPP.cpp after Application::Run(gcnew Form1()) function, but I encounter the errors below.
Please let me know where to place this code in order to capture the Form 1 graphics or how to point to this graphics.
If my problem is not clear for you please send me some contact email to send you the code.
Best regards,
Bogdan
>.\WindowsFormsCPP.cpp(25) : error C2673: 'main' : global functions do not have 'this' pointers
1>.\WindowsFormsCPP.cpp(25) : error C2227: left of '->Handle' must point to class/struct/union/generic type
1>.\WindowsFormsCPP.cpp(25) : error C2228: left of '.ToInt32' must have class/struct/union
1>.\WindowsFormsCPP.cpp(31) : error C2673: 'main' : global functions do not have 'this' pointers
1>.\WindowsFormsCPP.cpp(31) : error C2227: left of '->Handle' must point to class/struct/union/generic type
1>.\WindowsFormsCPP.cpp(31) : error C2228: left of '.ToInt32' must have class/struct/union
|
|
|
|
 |
|
 |
i want the total form content that will be saved as an image in .net windows application. i am getting the image as desktop capturing but i need to save the image with total content of the form. kindly help me plz.
Raghu
|
|
|
|
 |
|
 |
For Example
HBITMAP DirectBitmap= CreateDIBSection(DirectDC, (BITMAPINFO *)&RGB32BitsBITMAPINFO, DIB_RGB_COLORS,(void **)&ptPixels, NULL, 0);
is not Manage C++
I Want to Eork With Manage
Please Convert Code To Manage
|
|
|
|
 |
|
 |
I am using the new C++/CLI syntax and I am getting compiler errors when I try and create an image object from the HBITMAP pointer. I have tried a couple of different things including wrapping it in a new IntPtr object. Any other ideas?
|
|
|
|
 |
|
 |
I have fixed that error now I am having issues with trying to assign it to a Bitmap object. I get error C3073: ref class does not have a user-defined copy constructor. The line is: Bitmap image = Image::FromHBitmap((IntPtr)hBitmap));
|
|
|
|
 |
|
 |
Fixed it by not assigning the bitmap to the variable but returning it directly. However this doesn't allow the destruction of hBitmap with DestroyObject and could cause a memory leak unless c++/cli automatically takes care of that.
|
|
|
|
 |
|
 |
hi nish, i found your article very interesting, but following it i encountered a problem o nullreferenceexception probably due tu hdc because at debug you can read that hdc has an undefined value...what am i doing wrong?
i'm linking to a dll that need an hdc to draw an image modifying one passed and this is the error:
hwnd{HWND__} { unused= } HWND__*
on this source code:
private: System::Void btn_link_Click(System::Object * sender, System::EventArgs * e)
{
typedef UINT (CALLBACK* LPFNDLLFUNC1)();
typedef UINT (CALLBACK* LPFNDLLFUNC2)(DWORD,UINT);
typedef UINT (CALLBACK* LPFNDLLFUNC3)(void*,void*,HWND);
System::Drawing::Rectangle rect;
rect.set_X(10);
rect.set_Y(10);
rect.set_Height(10);
rect.set_Width(10);
Bitmap* bmp = new Bitmap("C:\\Documents and Settings\\mr_anderson\\Desktop\\ProvaDll\\Immagine4.bmp");
Graphics* g=Graphics::FromImage(bmp);
g = pictureBox1->CreateGraphics();
g->DrawImage(bmp,1,1);
System::Drawing::Imaging::BitmapData* bmpdata = bmp->LockBits(rect, ImageLockMode::ReadWrite, PixelFormat::Format24bppRgb);
/*IntPtr punt= bmpdata->get_Scan0();
void* temp = punt.ToPointer();*/
//***********************************************************
void* temp = bmpdata->get_Scan0().ToPointer();
//***********************************************************
//***********************************************************
System::Reflection::MethodInfo* method;
method= this->GetType()->GetMethod("condClr");
IntPtr ptr = method->MethodHandle.GetFunctionPointer();
void* funcptr = ptr.ToPointer();
//***********************************************************
//************************************************************
HWND hWnd=(HWND)pictureBox2->Handle.ToInt32();
HDC hdc = ::GetDC(hWnd);
//HDC hMemDC = CreateCompatibleDC(NULL);
////GetWindowRect((HWND)pictureBox2->Handle.ToInt32(),pictureBox2);
//
//*************************************************************
HINSTANCE hDLL; // Handle to DLL
LPFNDLLFUNC1 start,stop;
LPFNDLLFUNC3 execute;
LPFNDLLFUNC2 setsize; // Function pointer
/*DWORD dwParam1;
UINT uParam2, uReturnVal;*/
hDLL = LoadLibrary("C:\\Documents and Settings\\mr_anderson\\Desktop\\ProvaDll\\xvBlob.dll");
MessageBox::Show("eccezione");
if (hDLL != NULL)
{
MessageBox::Show("Dll linkata");
start = (LPFNDLLFUNC1)GetProcAddress(hDLL, "BlobStart");
stop = (LPFNDLLFUNC1)GetProcAddress(hDLL, "BlobStop");
execute = (LPFNDLLFUNC3)GetProcAddress(hDLL, "BlobExcecute");
setsize = (LPFNDLLFUNC2)GetProcAddress(hDLL, "BlobSetSize");
if (!start && !stop && !execute && !setsize)
{
// handle the error
FreeLibrary(hDLL);
}
else
{
// call the function
start();
MessageBox::Show("start funziona");
setsize(bmp->Width,bmp->Height);
MessageBox::Show("setsize funziona");
/*execute(temp, funcptr, hMemDC);*/
execute(temp, funcptr, hWnd);
MessageBox::Show("execute funziona");
stop();
MessageBox::Show("stop funziona");
MessageBox::Show("Funzione linkata");
FreeLibrary(hDLL);
bmp->UnlockBits(bmpdata);
::ReleaseDC(hWnd, hdc);
}
}
}
thanks in advance
donato
|
|
|
|
 |
|
 |
hello???
i want to know how SetDIBitsToDevice works ???
I have tryied it to display bitmap DIB data to directly to a
CPaintDC object,but displayed bitmap is cutting bitmap that is not a full bitmap so can any You tell how SetDIBitsToDevice works or any other idea????
|
|
|
|
 |
|
 |
Hi, i'm using Borland C++ 6.0 and when I try to compile the code I get an error on the following line:
pbox->Image = Image::FromHbitmap(hBitmap);
so i changed that line for a function that saves the hBitmap to a file, bue then i get only a black screen on the file.
Any ideas why?
|
|
|
|
 |
|
 |
I've solved this problem for C++Builder.
You can use a TImage :
screen->Piture->Bitmap->Handle=hBitmap;
here is the code(just for the whole screen):
HDC hDC;
hDC=GetDC(NULL);
HDC hMemDC=CreateCompatibleDC(hDC);
RECT r;
HWND hwnd=this->Handle;
GetWindowRect(hwnd,&r);
SIZE size;
size.cx = GetSystemMetrics(SM_CXSCREEN);
size.cy = GetSystemMetrics(SM_CYSCREEN);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, size.cx, size.cy);
if (hBitmap)
{
HBITMAP hOld = (HBITMAP) SelectObject(hMemDC, hBitmap);
BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, hOld);
DeleteDC(hMemDC);
ReleaseDC(NULL, hDC);
screen->Picture->Bitmap->Handle=hBitmap;
}
have fun!
-- modified at 12:44 Friday 27th April, 2007
|
|
|
|
 |
|
 |
hi,
you save the image to file by using the picture box.
How can i save the image without first displaying it on the picture box? i mean i dont want to display the screenshot but to save it to a file in jpg format.
i searched to save the device context object that is created by the bitblt method but couldnt find.
thanx...
|
|
|
|
 |
|
|
 |
|
|
 |
|
 |
the screen capture doesn't grab any windows with transparency on win2k/xp
|
|
|
|
 |
|
 |
Good Effort! If you write articles at this speed you will soon become the code project top article writer by the end of this year
IJW is quite easy and simple to use so some performance considerations can easily be overlooked. In general the managed to unmanaged transitions should be minimized. For instance the CaptureScreen method has simply too many calls to unmanaged methods. Each of these calls would cause a transition so you pay a performance penalty.
Luckily VC++ provides #pragma unmanaged. Using which you can produce native output for function following the #pragma. You can refactor the CaptureScreen functions so that it takes only handle to the current window and fullscreen parameter, and returns an image handle. Ofcourse you need to make it a global function. You could do something like this
#pragma unmanaged
HBITMAP CaptureScreen(bool FullScreen, HWND hwnd)
{
HDC hDC;
if(FullScreen)
hDC = GetDC(NULL); else
{
hDC = GetWindowDC(hwnd); }
HDC hMemDC = CreateCompatibleDC(hDC);
RECT r;
GetWindowRect(hwnd,&r); SIZE size;
if(FullScreen)
{
size.cx = GetSystemMetrics(SM_CXSCREEN);
size.cy = GetSystemMetrics(SM_CYSCREEN);
}
else
{
size.cx = r.right-r.left;
size.cy = r.bottom-r.top;
}
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, size.cx, size.cy);
if (hBitmap)
{
HBITMAP hOld = (HBITMAP) SelectObject(hMemDC, hBitmap);
BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, hOld);
DeleteDC(hMemDC);
ReleaseDC(NULL, hDC);
}
return hBitmap;
}
#pragma managed
Then you can call this function in the regular way. The advantage you minimize the transistions to just 1, and make the world a better place to live in
|
|
|
|
 |
|
 |
Thanks Rama
I really think that you should write an article on VC++.NET optimizations.
Nish
The posting stats are now in PDF:-
http://www.busterboy.org/codeproject/
Feel free to make your comments.
Updated - May 04th, Saturday
|
|
|
|
 |
|
 |
I need to finish my screen saver first. Today I added a best of Nish feature
|
|
|
|
 |
|
 |
Rama Krishna wrote:
I need to finish my screen saver first. Today I added a best of Nish feature
Oh, okay
Nish
The posting stats are now in PDF:-
http://www.busterboy.org/codeproject/
Feel free to make your comments.
Updated - May 04th, Saturday
|
|
|
|
 |
|
 |
Thanks, I had forgotten all about that too. Now to modify my MC++ class to put the GDI stuff in unmanaged sections
James
Simplicity Rules!
|
|
|
|
 |
|
|
 |
|
 |
Yep, MS didn't give us access to the CachedBitmap class in GDI+; so I had to fake my own with CreateCompatibleDC and CreateCompatibleBitmap. Thats essentially what WinForms does to create the double-buffer.
James
Simplicity Rules!
|
|
|
|
 |
|
 |
Cool! That explains UtilityLibrary.dll. I was wondering why you guys wanted an extra DLL. Usually screensavers are sorta stand-alone.
Nish
The posting stats are now in PDF:-
http://www.busterboy.org/codeproject/
Feel free to make your comments.
Updated - May 04th, Saturday
|
|
|
|
 |
|
 |
UtilityLibrary contains the Color picker used for the Options dialog.
I don't think the MC++ stuff is used yet. If I can find out why my OptimizedBitmap results in a slow down of the screensaver but shows an increase in my tests we'll use that for our drawing instead of our current method.
James
Simplicity Rules!
|
|
|
|
 |