Printing a graphics buffer using VB 2019

Les 281 Reputation points
2021-09-08T20:15:45.357+00:00

I know this may be an elementary question and I understand that at times it can be a difficult thing to do but I am going to keep it simple. I have a graphics buffer that I want to send to my printer for printing. All I want to be able to do is take whatever the size of the image buffer is and fit it on just 1 page. I don't care if it shrinks down the image just as long as it fits on my page. Its kind of like when you go to print something you are asked if you want to fit it on the page. Can someone explain to me how I can accomplish this?

Thanks

Les

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,578 questions
{count} votes

Accepted answer
  1. Castorix31 81,741 Reputation points
    2021-09-11T19:46:59.73+00:00

    If you want to keep the scaling, you will have to choose to scale to the page width or the page height
    For example, in my test, if I scale on the width only by keeping the original scaling of the image, I can do :

    Private Sub printdoc1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)      
        Dim pageBounds As Rectangle = e.PageBounds  
        Dim nFactor As Single = CSng(pageBounds.Width / img.Width)  
        Dim nNewHeight = img.Height * nFactor  
        pageBounds.Height = CInt(nNewHeight)  
        e.Graphics.DrawImage(img, New Rectangle(New Point(), pageBounds.Size), New Rectangle(New Point(), img.Size), GraphicsUnit.Pixel)  
    End Sub  
    

    And I will get the image covering all the page width only, with the right image scale :

    131284-print-page-scaling.jpg

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Castorix31 81,741 Reputation points
    2021-09-08T20:30:20.05+00:00

    Just use PrintDocument / PrintPage : How to: Print Graphics in Windows Forms
    (with e.Graphics.DrawImage , etc...)