StretchBlt in VB3 on Win 95

Posted on 1997-06-12
Last Modified: 2008-03-03
Wow! What a time I'm having with StretchBlt.
I need to print a BMP to the printer along with text and drawing objects on the same page.
I have the code of an old demo program called BMPKIT.MAK which performs this using StretchBlt API call when run in Win 3.x but not when run on a 95 machine. It uses StretchBlt successfully to the screen but not ot the printer.
I have to produce an application which will run on both systems because users have both.

Any Ideas ?

PS Even when loaded in VB4 32bit . StretchBlt returns True but nothing
comes out the printer even though the Declares have been changed to the 32
bit library versions.
???????????????????????????????????????????????          :(
Question by:watin
  • 2
  • 2

Expert Comment

Comment Utility
It might help if you post the code you are using to do it.  I am familiar with StretchBLT and printing.


Author Comment

Comment Utility
Code used as Requested
Global scalex As Single
Global scaley As Single

'* Declarations for the GDI in Global Module.
Declare Function CreateCompatibleDC% Lib "GDI" (ByVal hdc%)
Declare Function SelectObject% Lib "GDI" (ByVal hdc%, ByVal hObject%)
Declare Function DeleteDC% Lib "GDI" (ByVal hdc%)
Declare Function Escape Lib "GDI" (ByVal hdc As Integer, ByVal nEscape As Integer, ByVal nCount As Integer, lplnData As Any, lpOutData As Any) As Integer

Declare Function StretchBlt% Lib "GDI" (ByVal hdc%, ByVal x%, ByVal y%, ByVal nWidth%, ByVal nHght%, ByVal hSrcDC%, ByVal xSrc%, ByVal ySrc%, ByVal nSrcWidth%, ByVal nSrcHeight%, ByVal dwRop&)
Declare Function StretchBltP Lib "c:\windows\system\Gdi32.dll" Alias "StretchBlt" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

Activating code
Sub Command3_Click ()
'these are difinde in order to try calling the GDI32 version of the
'call which requires LONGs. it didn't work.
Dim src, ApiError, printgraphicx, hMemoryDC, prhdc As Long
Dim hOldBitMap, printgraphicy As Long

'* Print all items other than the image first.

printer.ScaleMode = 5

printer.CurrentX = 3
printer.CurrentY = 3
printer.Print "Hello"

printer.CurrentY = 4
printer.CurrentX = 1
printer.DrawWidth = 5
printer.Line -Step(printer.CurrentX + 2, 0)

'*After printing all other elements include your BMP
'printing routine, as below.

'* Print Scaled Image

'*Make certain we have valid scaling values
If scalex = 0 Then scalex = 1
If scaley = 0 Then scaley = 1

'*Set GDI Constants
Const NULL = 0&
Const SRCCOPY = &HCC0020
Const NEWFRAME = 1
Const pixel = 3

'* StretchBlt requires pixel coordinates.

picture1.ScaleMode = pixel
printer.ScaleMode = pixel

'*First two variables are the x,y coordinates (in Pixels)
'for the placement of images on the printed page. Since the
'form's ScaleMode is 5 for inches, multiply by 300 to
'match the the current inch positions of Picture2 on
'the 300 DPI device.

'*Second pair of variables set the height and width of the
'graphic to match the pixel size of the original picture
'in the invisible Picture1 box.

printgraphicx = Picture2.Left * 300
printgraphicy = Picture2.Top * 300

printer.ScaleWidth = picture1.ScaleWidth * scalex
printer.ScaleHeight = picture1.ScaleHeight * scaley

'*Do not change the order of these statements.

hMemoryDC = CreateCompatibleDC(picture1.hDC)
hOldBitMap = SelectObject(hMemoryDC, picture1.Picture)
'Variables for the StretchBlt function:
'1. Source of image HMemeoryDC -- set above from original picture
'2,3. Positioning on output object. Must be integer, in pixels.
'4,5. Width, height of output image. Here we use the values set above.
'6, Source of this case the memory image set by CreateCompatibleDC function
'7,8. Don't know. Set to 0 works just fine! Experiment and let me know.
'9,10. X/Y coordinates for how much of original to put in new object. You can use
'these values for cropping, if you like.
'11. SRCCOPY, as set in constants above. Change at own risk.
prhdc = printer.hDC
ApiError = StretchBlt(prhdc, printgraphicx, printgraphicy, printer.ScaleWidth, printer.ScaleHeight, hMemoryDC, 0, 0, picture1.ScaleWidth, picture1.ScaleHeight, src)
MsgBox "Printing StretchBlt returned " & Format$(ApiError, "True/False")
   'this is the 32bit call but it didn't work
   'Declare Function StretchBlt Lib "gdi32" Alias "StretchBlt" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
hOldBitMap = SelectObject(hMemoryDC, hOldBitMap)
ApiError = DeleteDC(hMemoryDC)

'*Send image and text to printer

End Sub


Expert Comment

Comment Utility
This may or may not help but where are your defines for the 32 bit version of CreateCompatableDC and SelectObject and the other functions that go along with StretchBLT?

Accepted Solution

weekee earned 200 total points
Comment Utility
Actually, if you are using Visual Basic 4, there is a method known as paintpicture. It works so sort like StretchBlt. The details of all the parameters used can be found in the help file.
The advantage is that you need not need to do all those API declaration. I have tried to use the method to send to the printer.hdc and everything works fine. If you familar with StretchBlt, you will have no problems with the parameters for the paintpicture method becasue both are similar

Author Comment

Comment Utility
Thanks weekee & tward for your suggestions but I have found the problem and solved it.
I am not using VB4 because the same application compiled in vb4 takes 3 disks to distribute and in vb3 takes only 2/3 of one.
Also it takes longer to execute when run in vb4.

Anyway stretchblt now works
The problem was that my printer driver was slightly incompatible with the printer and although it worked well to all appearances it must not have given the right answers to stretchblt and wouldn't work. That's all folks. The easiest answer is often the one.

Now How do I remove this question so others don't try to answer it for ever ?

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now