question

Les-0449 avatar image
0 Votes"
Les-0449 asked XingyuZhao-MSFT commented

Printing a graphics buffer using VB 2019

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

dotnet-visual-basic
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Les-0449 ,
Also check : Printing image with PrintDocument. how to adjust the image to fit paper size
You can convert the code to VB. NET.
Besides, if I have any misunderstanding, please let me know.

0 Votes 0 ·
Les-0449 avatar image Les-0449 XingyuZhao-MSFT ·

Hi XZ,

Well I am not at all familiar with C but perhaps I can make a suggestion. Figure I have this big graphics buffer and lets just say its 5000x5000. Now the image I send to this buffer is lets say 2534X1785 and the way I know this is I keep track of where the right most and bottom most graphic is done. So what I like to do is just send that 2534x1785 over to the printer and have print on just one page. Now if the image is larger then the paper is there a way of telling the printer to fit it onto the one page? If you have a good link as to how to do this I would certainly appreciate it since I am not asking for you to convert the C code but if you have that link that would be very helpful.

Thank you XZ

Les

0 Votes 0 ·
Castorix31 avatar image
1 Vote"
Castorix31 answered XingyuZhao-MSFT commented

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



· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Cast,

You the man !!! You know your stuff. Listen if I want to learn about this aspect of printing using vb can you refer me to either a book or link that goes into very clear details about this part of vb??. Not to waste your time but I have 3 pics I am going to show you. The first one is the graph that appears in my picturebox, second is the image that gets printed where the image was very scuwd and the third is the printer image after I ran your second code snippet. As you can see there is a big difference. The whole purpose of this graph is to show that a normal line is perpendicular to the tangent line (blue) but as you can see even though the math is right the visual does not.

I do not know what the heck I did here in this forum, I thought I sent you a comment with 3 images to show you and for some reason they seem to be closer to the top of this thread. Now I went to click on ACCEPTED ANSWER but I think it went under my name. Can you please tell me where do I click on accepted answer please! And is it the proper thing to do to communicate with people out here by using comment? When I send you comments does everyone in the thread see it also? I am just trying to learn.

Thx Cast

Les

131334-screen-shot.jpg131342-printer-graphics-before-scaling.jpg131210-printer-graphics-after-scaling.jpg


0 Votes 0 ·

Hi @Les-0449 ,

Can you please tell me where do I click on accepted answer please

I have converted the last comment of Castorix31 to answer, and you can click "Accept Answer" and upvote it.

0 Votes 0 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered XingyuZhao-MSFT converted comment to answer

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


· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Cast,

If I do just print page I do not think I will get the entire image that is stored in my graphics buffer but I am not an expert here. Your thoughts. All I want is the entire image in my buffer to fill an entire page. By the way cast I sent a comment to someone who made a suggestion to me in the forum yet I do not see that comment?? How do I do that?

Thanks Les

0 Votes 0 ·

Draw on the whole page
For example, for an Image img, in the PrintPage :

  Dim pageBounds As Rectangle = e.PageBounds
  e.Graphics.DrawImage(img, New Rectangle(New Point(), pageBounds.Size), New Rectangle(New Point(), img.Size), GraphicsUnit.Pixel)

0 Votes 0 ·

Hi Cast,

Well I tried your code but it is not working. What would help if I had some type of general understanding as to what I am doing. The error stmnt its showing me is e.PageBounds is not a member of EventArgs??? I placed your code into a button click routine.

Any help would be appreciated

Les

0 Votes 0 ·
Show more comments