Re: Capturing Window Image + Content + Printing to a JPG/PNG file

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Alexander Nickolov (agnickolov_at_mvps.org)
Date: 08/05/04


Date: Wed, 4 Aug 2004 22:08:16 -0700

Read chapters 10-13 of "Inside OLE" by Kraig Brockschmidt.
It used to be available on MSDN Library, so if you have old
VC6 MSDN Library CDs or from around that time...

-- 
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
"Manoj K Srivastava" <ManojKSrivastava@discussions.microsoft.com> wrote in
message news:B685CC99-68FE-43CD-B1D5-51EDE44CDD05@microsoft.com...
> Would you please elaborate how I can use IDataObject? Is there any sample
> code available on MSDN you can point me to?
>
> Thanks,
> Manoj
>
> "Alexander Nickolov" wrote:
>
> > Try using IDataObject instead of IViewObject.
> >
> > -- 
> > =====================================
> > Alexander Nickolov
> > Microsoft MVP [VC], MCSD
> > email: agnickolov@mvps.org
> > MVP VC FAQ: http://www.mvps.org/vcfaq
> > =====================================
> > "Manoj K Srivastava" <ManojKSrivastava@discussions.microsoft.com> wrote
in
> > message news:9DA9D49F-F2E3-47C4-8557-27E4A05B0595@microsoft.com...
> > > Hello Alexander,
> > >
> > > Thanks for the correction, but my problem is with the size of the
bitmap.
> > Since the current window does not display all the content of the page
(i.e.
> > it has scrollbars) providing the window size as the target size if not
going
> > to capture the entire page content. So how can I make it do that?
> > >
> > > Regards,
> > > Manoj
> > >
> > > "Alexander Nickolov" wrote:
> > >
> > > > You never retrieved your bitmap from the off-screen DC.
> > > > You need to select back the original bitmap into it:
> > > >
> > > > HBITMAP hbmOld = SelectObject(targetHdc, bitmap);
> > > > ....
> > > > SelectObject(targetHdc, hbmOld);
> > > > // Now your bitmap is accessible again
> > > >
> > > > I'm not sure what CreateBMPFile and ConvertBmpToPng do,
> > > > so I can't comment there.
> > > >
> > > > BTW, note that you acquired a device-dependent bitmap. If
> > > > you wanted a specific image format, you need to use a DIB
> > > > instead. See CreateDIBSection.
> > > >
> > > > -- 
> > > > =====================================
> > > > Alexander Nickolov
> > > > Microsoft MVP [VC], MCSD
> > > > email: agnickolov@mvps.org
> > > > MVP VC FAQ: http://www.mvps.org/vcfaq
> > > > =====================================
> > > >
> > > > "Manoj K Srivastava" <ManojKSrivastava@discussions.microsoft.com>
wrote
> > in
> > > > message news:6CF458CC-FB5E-493B-AC6B-EB469E20D064@microsoft.com...
> > > > > Igor,
> > > > >
> > > > > I have been trying to do as you suggested but have some technical
> > > > difficulties.
> > > > > Methods CreateCompatibleBitmap and IViewObject->Draw take a rect
> > parameter
> > > > where I must specify the size. How would I know how many pixels (or
how
> > many
> > > > printed pages if I were to send the output to a printer) in length
the
> > data
> > > > would be? The browser is showing part of the page and it shows more
(and
> > > > different) data when the user scrolls up or down. When I print from
a
> > File
> > > > menu, IE renders the whole content page by page to the printer. I
want
> > the
> > > > same behavior exept the output needs to go to a file in a image
format.
> > > > >
> > > > > Any ideas?
> > > > >
> > > > > Thanks,
> > > > > Manoj
> > > > > PS: Here is my code:
> > > > > STDMETHODIMP CRenderer::Capture(IDispatch* document, BSTR*
> > rImageFilePath)
> > > > > {
> > > > > HDC targetHdc, hdcScreen;
> > > > > RECTL rect;
> > > > > RECT window_rect;
> > > > > IViewObject *pIViewObject=NULL;
> > > > > PBITMAPINFO pbmi;
> > > > > HBITMAP bitmap;
> > > > > static char TempPath[MAX_PATH+1];
> > > > > char imageFilePath[MAX_PATH+1];
> > > > > static char BmpFilePathBuffer[MAX_PATH+1];
> > > > > HWND hWnd = GetForegroundWindow();
> > > > >
> > > > > GetClientRect(hWnd, &window_rect);
> > > > > document->QueryInterface(__uuidof(IViewObject), (void
> > **)&pIViewObject);
> > > > > hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
> > > > > targetHdc = CreateCompatibleDC(hdcScreen);
> > > > > bitmap = CreateCompatibleBitmap(hdcScreen, window_rect.right,
> > > > window_rect.bottom);
> > > > > SelectObject(targetHdc, bitmap);
> > > > > rect.left = 0; rect.top = 0; rect.bottom = window_rect.bottom;
> > rect.right
> > > > = window_rect.right;
> > > > > pIViewObject->Draw(DVASPECT_DOCPRINT,0, NULL, NULL, NULL,
targetHdc,
> > > > &rect, 0, 0,0);
> > > > > //I want to convert to a PNG. The following functions are taken
from
> > MSDN
> > > > examples
> > > > > pbmi = CreateBitmapInfoStruct(hWnd, bitmap); //From MSDN Example
> > > > >
> > > > > // Create a temporary file.
> > > > > GetTempPath((DWORD)MAX_PATH+1, TempPath);
> > > > > GetTempFileName(TempPath, // dir. for temp. files
> > > > > "IMG",                // temp. file name prefix
> > > > > 0,                    // create unique name
> > > > > BmpFilePathBuffer);          // buffer for name
> > > > >
> > > > >
> > > > > CreateBMPFile(hWnd, BmpFilePathBuffer, pbmi, bitmap, targetHdc);
> > //From
> > > > MSDN Example
> > > > > GetTempFileName(TempPath, // dir. for temp. files
> > > > > "IMG",                // temp. file name prefix
> > > > > 0,                    // create unique name
> > > > > imageFilePath);          // buffer for name
> > > > > strcat((char *)imageFilePath, (const char *)".png");
> > > > > ConvertBmpToPng(BmpFilePathBuffer, imageFilePath); // From MSDN
> > Example
> > > > > DeleteFile(BmpFilePathBuffer);
> > > > >
> > > > > DeleteDC(hdcScreen);
> > > > > ReleaseDC(hWnd, targetHdc);
> > > > > *rImageFilePath = A2WBSTR((LPCSTR)imageFilePath);
> > > > > return S_OK;
> > > > > }
> > > > >
> > > > > "Igor Tandetnik" wrote:
> > > > >
> > > > > > "Manoj K Srivastava"
<ManojKSrivastava@discussions.microsoft.com>
> > wrote
> > > > > > in message
news:8820F41E-0C92-49A0-A73F-B22FB4DB99EC@microsoft.com
> > > > > > > Thanks for the pointer. Can you tell me how I get get/make an
> > > > > > > off-screen DC backed by a bitmap? Is there a way to just use
> > Windows
> > > > > > > default DC, which is certainly bitmap backed?
> > > > > >
> > > > > > See CreateCompatibleDC, CreateCompatibleBitmap, SelectObject
> > > > > > -- 
> > > > > > With best wishes,
> > > > > >     Igor Tandetnik
> > > > > >
> > > > > > "For every complex problem, there is a solution that is simple,
> > neat,
> > > > > > and wrong." H.L. Mencken
> > > > > >
> > > > > >
> > > > > >
> > > >
> > > >
> > > >
> >
> >
> >


Relevant Pages