[Webinar] Streamline your web hosting managementRegister Today


How do I print bitmaps using the API?

Posted on 2000-01-11
Medium Priority
Last Modified: 2013-12-03
How do I print bitmaps to the default printer using the Windows API?
Question by:ddg_

Author Comment

ID: 2341987
Edited text of question.
LVL 14

Expert Comment

ID: 2342032
See the following microsoft page:

How to Print a VB Picture Control Using Windows API Functions

It's a 16-bit example, but all you have to do is use the 32-bit API calls.  You can find them in the API Text Viewer...


Author Comment

ID: 2345517
Thanx mcrider, but I am trying to print bitmap files without using picturebox controls.  I do not want to load a bitmap into a picturebox each time before printing it.  Do you or anyone else know of a way to do this?
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.


Expert Comment

ID: 2346269

Author Comment

ID: 2346815
The problem I am experiencing with pictureboxes is that they must be visible in order to print them.
Basically, I want to load a bitmap from a file into memory, and then manipulate the memory's hdc to print it to the printer.
LVL 14

Expert Comment

ID: 2346841
pictureboxes DO NOT need to be visible to print them...


Accepted Solution

KhollAD earned 200 total points
ID: 2350254
You can use the Loadimage Api to open a bmp in memory without use picturebox and then print this bmp with the blt and escape features.

Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As BITMAP) As Long

Private Type BITMAP '14 bytes
        bmType As Long
        bmWidth As Long
        bmHeight As Long
        bmWidthBytes As Long
        bmPlanes As Integer
        bmBitsPixel As Integer
        bmBits As Long
End Type

Private Declare Function Escape Lib "gdi32" (ByVal hdc As Long, ByVal nEscape As Long, ByVal nCount As Long, ByVal lpInData As String, lpOutData As Any) As Long

Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
Private Declare Function StretchBlt Lib "gdi32" (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

Private Const SRCCOPY = &HCC0020

Private Buffer, Buffer_hBitmap As Long

Private Sub BmpPrint(FileName As String)

    Dim lRet As Long
    Dim BmpInfo As BITMAP
    Printer.ScaleMode = 1
    Printer.Print ""
    Buffer = CreateCompatibleDC(0)
    Buffer_hBitmap = LoadImage(ByVal 0&, FileName, 0, 0, 0, &H10)
    lRet = SelectObject(Buffer, Buffer_hBitmap)
    lRet = GetObject(Buffer_hBitmap, Len(BmpInfo), BmpInfo)
    lRet = StretchBlt(Printer.hdc, 0, 0, Printer.ScaleX(BmpInfo.bmWidth, 3, 1), Printer.ScaleY(BmpInfo.bmHeight, 3, 1), Buffer, 0, 0, BmpInfo.bmWidth, BmpInfo.bmHeight, SRCCOPY)
    lRet = DeleteDC(Buffer)
    lRet = DeleteObject(Buffer_hBitmap)
    lRet = Escape(Printer.hdc, 1, 0, 0&, 0&)

End Sub

Private Sub Command1_Click()

    Call BmpPrint("c:\mypic.bmp")
End Sub


Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

590 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