Printing a drawn image

GameMakerX

Member
Joined
May 30, 2008
Messages
10
Programming Experience
Beginner
I'm trying to figure out how to print a picturebox, which contains an image made from Drawline and DrawArc commands.

If someone has any idea how to do this, please let me know as soon as possible.

Appreciate it.
 
Assuming u got a print document call printDoc
Assuming u got a print preview dialog named printPreviewDlg

VB.NET:
    Private Sub print()
        'Sets the print preview document to printdoc
        printPreviewDlg.Document = printDoc
 
        'Enlarge the print preview to full screen start
        printPreviewDlg.Width = Screen.PrimaryScreen.Bounds.Width
        printPreviewDlg.Height = Screen.PrimaryScreen.Bounds.Height
         'Enlarge the print preview to full screen end
        
        'Set Zoom of print preview to 150%
        printPreviewDlg.PrintPreviewControl.Zoom = 1.5
        If printPreviewDlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
            printDoc.Print()
        End If
    End Sub

    'Calls when printDoc.Print() is executed
    Private Sub printDoc_PrintPage(ByVal sender As Object, ByVal e As_
System.Drawing.Printing.PrintPageEventArgs) Handles  printDoc.PrintPage
        e.Graphics.DrawImage(PictureBox1.Image, 0, 0)
    End Sub
 
The code works and I appreciate it, but I still have the same problem. I should have specified my problem more. I was able to get my printer to print, but prints a blank piece of paper. I used the code and the Print Preview still doesn't recognize my drawing. I would like to know how I get my drawn image recognized by the printer when it prints and not printing another blank piece of paper. Thanks again for the code anyways.
 
If you don't have an image in the Picturebox but instead draw to it with the Graphics of Paint event, then you have to draw the same to your PrintDocument PrintPage event. The other option is doing your dynamic drawing to a bitmap that is displayed by the Picturebox and drawn to the PrintDocument.
 
I tried doing what you said, with every possible way I could come up with, and i can't still get it to work. Is there anyway I can prevent my image(made out of Drawline commands) from resetting every time I try to print. Can I save these drawn lines into the image file or something like that? I'd really appreciate it that I could get the code along with the responses, so I can get an idea how to do it.

One more thing I try to do is this:
Private imageobject as Graphics = picturebox.CreateGraphics()

But I always get an error when I try to move from my first form to the form this code is in and the error is in the first form saying something about an inner exception and not a ObjectReference.
THe error is over this code:
My.Forms.form2.ShowDialog()

I'd would like to know what it means and how to fix this.
 
CreateGraphics is not used for drawing to controls, you just loose it right away. With the Graphics class you can draw to multiple surfaces like Controls through the Paint event, PrintDocuments through PrintPage event, and bitmaps. Example of the latter:
dim bmp as new bitmap(100,100)
dim g as graphics = graphics.fromimage(bmp)
g.draw here
g.dispose()
 
Let me see if I understand this, the code you gave me creates a bitmap and I then place the bitmap into the Graphics object. If that is correct, then how do I get the graphics object to appear in my picturebox? The idea of my project is to click a button and the image appears in the picturebox. Originally, I had all the code executed from the click event. So I thought that by clicking the button, the button's click event will then move to the paint event of my picture box ,with all the code to create the image, by calling the paint event, which will create the image inside the picturebox. But when I try to do this, an error kept popping up saying something about I can't transfer from a mouse event control type to a paint event control type. I tried to use CONVERT and similar commands but either they didn't work or at least not the way I thought they worked. If you have any ideas on how to fix these problems, send any and all ideas that may prove useful, and I would really appreciate it.
 
The code show how you can create an image and draw stuff to it, ie how to construct an image dynamically. The image can then be displayed by the Picturebox, you can also draw that image to printout.

You don't call the Paint event, you call Refresh/Invalidate methods and the Paint event will call you.
 
So I moved all the code that contains the calculations and how to plot my image from my button.click event to the picturebox.paint event. So then where do I place the refresh/invalidate commands. When I place the command in the paint event, the image just keeps on flashing from the moment the form is loaded. And when I try to put the command in the click event, nothing happens. What am i doing wrong?

(Note: the lines that are flashing are made from the creategraphics.drawline commands.)

You know that the image was originally made by this code

picturebox.creategraphics.drawline() (I know that this won't work)

The new code to draw a line is like this as g representing the graphics object

g.drawline()

Now how do I get g to appear as the image of my picturebox. Do I have to use a SAVE command for g. Or do I use the FromFile method from the picturebox. I'm starting to run out of ideas to get this thing working like I envisioned. If you have any ideas, please tell me. And please be specific as you can cause I will get a better picture with a more specific detailed explanation. Thanks again.
 
Now how do I get g to appear as the image of my picturebox. Do I have to use a SAVE command for g....

I think the problem you're having is understanding what role the graphics object plays. The graphics object is just a class that does the work of creating graphics, it's not the drawing surface being drawn on. The drawing surface is either the control, a bitmap, a printdocument, etc. My closest analogy to the real world for the graphics object would be your hand. When you want to create a line on a piece of paper, you tell your hand to draw a line. But first you have to tell it where to draw, which would be the piece of paper.
Your hand is the graphics object, when you first create it you have to tell it where you want to draw (in the case of the paint event, it is created for you and is told to paint on the control calling the paint event). The paper is the surface (the control or bitmap or printdocument, etc). This analogy is not exact but you should get the picture. No need to save your hand after drawing a line, you want to save the piece of paper, or show it to someone, etc.

So to restate what JohnH has already suggested: create a bitmap and draw on that bitmap. Then you can assign that bitmap to the image property of the picturebox and/or use the DrawImage method of the graphics class to paint it to the PrintDocument.

If you're interested in reducing the flickering, look at the Control.SetStyle Method. This is just for future knowledge, my suggestion above of drawing to a bitmap first is in essence it's own double-buffering.
 
Thanks. What you told me really helped me out. I thank everyone one of you who replied to this thread. I really appreciate it. Thanks again.
 
Back
Top