Printing a screen image (StretchBlt error)
-
I am writing a graphic design tool. I am using a device context to produce the image on screen. Now I want to be able to print the image to the printer. What I am doing so far is: Create the screen device context : CDC *m_pDC = CreateDC (); Do all kinds of weird and wonderful things to produce the image (all of this works, since I do get the image I want on screen) When the user presses the Print option from the menu, I use the CPrintDialog class to select the printer, and subsequently I get a HDC by “CPrintDialog Print.CreatePrinterDC ();” Then I calculate the size on paper by the following piece of code: POINT point; POINT start; point.x = GetDeviceCaps (hDC, PHYSICALWIDTH); start.x = GetDeviceCaps (hDC, PHYSICALOFFSETX); point.x -= (start.x*2); point.y = GetDeviceCaps (hDC, PHYSICALHEIGHT); start.y = GetDeviceCaps (hDC, PHYSICALOFFSETY); point.y -= (start.y*2); DPtoLP (hDC, &point, 1); DPtoLP (hDC, &start, 1); I already have a POINT named “Source”, that holds the size of the screen DC. Then I call: if (!StretchBlt (hDC, start.x, start.y, point.x, point.y, m_pDC->GetSafeHwnd (), 0, 0, Source.x, Source.y, SRCCOPY)) And this is where all goes wrong! This fails, and the error message (as obtained by FormatMessage and GetLastError) says: “The parameter is incorrect” As far as I can see, the size on paper is correct, since I also write a (test) rectangle with these positions: Rectangle (hDC, start.x, start.y, point.x, point.y); and this produces a rectangle on paper with the size and position as I would expect. Can anyone help me out here. William -- modified at 7:29 Thursday 17th August, 2006
-
I am writing a graphic design tool. I am using a device context to produce the image on screen. Now I want to be able to print the image to the printer. What I am doing so far is: Create the screen device context : CDC *m_pDC = CreateDC (); Do all kinds of weird and wonderful things to produce the image (all of this works, since I do get the image I want on screen) When the user presses the Print option from the menu, I use the CPrintDialog class to select the printer, and subsequently I get a HDC by “CPrintDialog Print.CreatePrinterDC ();” Then I calculate the size on paper by the following piece of code: POINT point; POINT start; point.x = GetDeviceCaps (hDC, PHYSICALWIDTH); start.x = GetDeviceCaps (hDC, PHYSICALOFFSETX); point.x -= (start.x*2); point.y = GetDeviceCaps (hDC, PHYSICALHEIGHT); start.y = GetDeviceCaps (hDC, PHYSICALOFFSETY); point.y -= (start.y*2); DPtoLP (hDC, &point, 1); DPtoLP (hDC, &start, 1); I already have a POINT named “Source”, that holds the size of the screen DC. Then I call: if (!StretchBlt (hDC, start.x, start.y, point.x, point.y, m_pDC->GetSafeHwnd (), 0, 0, Source.x, Source.y, SRCCOPY)) And this is where all goes wrong! This fails, and the error message (as obtained by FormatMessage and GetLastError) says: “The parameter is incorrect” As far as I can see, the size on paper is correct, since I also write a (test) rectangle with these positions: Rectangle (hDC, start.x, start.y, point.x, point.y); and this produces a rectangle on paper with the size and position as I would expect. Can anyone help me out here. William -- modified at 7:29 Thursday 17th August, 2006
http://www.codeproject.com/tools/screen_snaper.asp[^] check the above link
SaRath.
_"Before you can learn to recognize what's wrong, you must learn to recognize what's right" - Raymond Chen."
-
http://www.codeproject.com/tools/screen_snaper.asp[^] check the above link
SaRath.
_"Before you can learn to recognize what's wrong, you must learn to recognize what's right" - Raymond Chen."
Sarath, Thanks for the tip. I checked the link, but it does not solve my problem, since it does not refer to sending the captured image to the printer. I do have a device context with a valid image. However, when I try to copy this image into a printer device context (using StretchBlt), I get an error message. William
-
I am writing a graphic design tool. I am using a device context to produce the image on screen. Now I want to be able to print the image to the printer. What I am doing so far is: Create the screen device context : CDC *m_pDC = CreateDC (); Do all kinds of weird and wonderful things to produce the image (all of this works, since I do get the image I want on screen) When the user presses the Print option from the menu, I use the CPrintDialog class to select the printer, and subsequently I get a HDC by “CPrintDialog Print.CreatePrinterDC ();” Then I calculate the size on paper by the following piece of code: POINT point; POINT start; point.x = GetDeviceCaps (hDC, PHYSICALWIDTH); start.x = GetDeviceCaps (hDC, PHYSICALOFFSETX); point.x -= (start.x*2); point.y = GetDeviceCaps (hDC, PHYSICALHEIGHT); start.y = GetDeviceCaps (hDC, PHYSICALOFFSETY); point.y -= (start.y*2); DPtoLP (hDC, &point, 1); DPtoLP (hDC, &start, 1); I already have a POINT named “Source”, that holds the size of the screen DC. Then I call: if (!StretchBlt (hDC, start.x, start.y, point.x, point.y, m_pDC->GetSafeHwnd (), 0, 0, Source.x, Source.y, SRCCOPY)) And this is where all goes wrong! This fails, and the error message (as obtained by FormatMessage and GetLastError) says: “The parameter is incorrect” As far as I can see, the size on paper is correct, since I also write a (test) rectangle with these positions: Rectangle (hDC, start.x, start.y, point.x, point.y); and this produces a rectangle on paper with the size and position as I would expect. Can anyone help me out here. William -- modified at 7:29 Thursday 17th August, 2006
some devices cannot handle large bitmaps (see GetDeviceCaps(...RASTERCAPS), so you have to do your output in bands. some devices cannot do StretchBlt or StretchDIBits (again, see GetDeviceCaps)
-
I am writing a graphic design tool. I am using a device context to produce the image on screen. Now I want to be able to print the image to the printer. What I am doing so far is: Create the screen device context : CDC *m_pDC = CreateDC (); Do all kinds of weird and wonderful things to produce the image (all of this works, since I do get the image I want on screen) When the user presses the Print option from the menu, I use the CPrintDialog class to select the printer, and subsequently I get a HDC by “CPrintDialog Print.CreatePrinterDC ();” Then I calculate the size on paper by the following piece of code: POINT point; POINT start; point.x = GetDeviceCaps (hDC, PHYSICALWIDTH); start.x = GetDeviceCaps (hDC, PHYSICALOFFSETX); point.x -= (start.x*2); point.y = GetDeviceCaps (hDC, PHYSICALHEIGHT); start.y = GetDeviceCaps (hDC, PHYSICALOFFSETY); point.y -= (start.y*2); DPtoLP (hDC, &point, 1); DPtoLP (hDC, &start, 1); I already have a POINT named “Source”, that holds the size of the screen DC. Then I call: if (!StretchBlt (hDC, start.x, start.y, point.x, point.y, m_pDC->GetSafeHwnd (), 0, 0, Source.x, Source.y, SRCCOPY)) And this is where all goes wrong! This fails, and the error message (as obtained by FormatMessage and GetLastError) says: “The parameter is incorrect” As far as I can see, the size on paper is correct, since I also write a (test) rectangle with these positions: Rectangle (hDC, start.x, start.y, point.x, point.y); and this produces a rectangle on paper with the size and position as I would expect. Can anyone help me out here. William -- modified at 7:29 Thursday 17th August, 2006
Engberts wrote:
if (!StretchBlt (hDC, start.x, start.y, point.x, point.y, m_pDC->GetSafeHwnd (), 0, 0, Source.x, Source.y, SRCCOPY))
6th parameter of API is HDC,whats is
m_pDC->GetSafeHwnd ()
? I guess you are passing incorrect type of parameter. It should HDC(source).Prasad Notifier using ATL
-
Engberts wrote:
if (!StretchBlt (hDC, start.x, start.y, point.x, point.y, m_pDC->GetSafeHwnd (), 0, 0, Source.x, Source.y, SRCCOPY))
6th parameter of API is HDC,whats is
m_pDC->GetSafeHwnd ()
? I guess you are passing incorrect type of parameter. It should HDC(source).Prasad Notifier using ATL
Sorry, this is a typing error, I of course used GetSafeHdc () and not GetSafeHwnd (). Meanwhile however, I think I found the solution. Since I am copying bitmaps between diferrent devices, I cannot directly BitBlt or StretchBlt between them. I am now using GetDIBits to get the device independant bits out of the screen's device context and then use StretchDIBits to move them into the printer's device context. This does require a lot of programming, but it does result in the required image on paper. Thanks to you all for your efforts! William
-
Sarath, Thanks for the tip. I checked the link, but it does not solve my problem, since it does not refer to sending the captured image to the printer. I do have a device context with a valid image. However, when I try to copy this image into a printer device context (using StretchBlt), I get an error message. William
Before using stretchblt, i think you need to set SetStretchBltMode printing to DC is not a big deal. it's same as using CDC
SaRath.
_"Before you can learn to recognize what's wrong, you must learn to recognize what's right" - Raymond Chen."
-
Sorry, this is a typing error, I of course used GetSafeHdc () and not GetSafeHwnd (). Meanwhile however, I think I found the solution. Since I am copying bitmaps between diferrent devices, I cannot directly BitBlt or StretchBlt between them. I am now using GetDIBits to get the device independant bits out of the screen's device context and then use StretchDIBits to move them into the printer's device context. This does require a lot of programming, but it does result in the required image on paper. Thanks to you all for your efforts! William
Never mind. Importantly, you shared your solution, that's great !
Prasad Notifier using ATL