Home » VB.NetRSS

Print a image from file

Am having no luck printing a stored .jpg image . i have included the src code for any one that wants to help. The stored image dimensions are about 7.25 by 10.5 when printed in landscape mode. What i get is about 3/4 of the image. Can anyone see what i am doing wrong? Note the saved rotated image is fine when displayed with paint. It just won't print correctly. This is for vbexpress 2010

 

 

Imports System.Drawing.Printing
Imports System.Drawing.Image

Public Class Form1

    Private strFileName As String
    Private strPrintRecord As String
    Private WithEvents DialogsPrintDocument As PrintDocument
   
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub DialogsPrintDocument_QueryPageSettings(ByVal sender As Object, ByVal e As  _
                                                       System.Drawing.Printing.QueryPageSettingsEventArgs) _
                                                       Handles DialogsPrintDocument.QueryPageSettings

        e.PageSettings.Landscape = True

    End Sub
    Private Sub DialogsPrintDocument_PrintPage(ByVal sender As Object, _
                                               ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles DialogsPrintDocument.PrintPage

        Dim img As Image
        Dim rect As RectangleF

        img = Image.FromFile("C:\xxx.jpg")
        img.RotateFlip(RotateFlipType.Rotate180FlipNone)

        e.HasMorePages = False

        e.Graphics.DrawImage(img, 0, 0)

        img.Save("C:\rotated.jpg")
        img.Dispose()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        DialogsPrintDocument = New PrintDocument

        With PrintDialog1

            .AllowCurrentPage = False
            .AllowPrintToFile = False
            .AllowSelection = False
            .AllowSomePages = False
            .Document = DialogsPrintDocument
            .PrinterSettings.DefaultPageSettings.Margins.Top = 0
            .PrinterSettings.DefaultPageSettings.Margins.Bottom = 0
            .PrinterSettings.DefaultPageSettings.Margins.Left = 0
            .PrinterSettings.DefaultPageSettings.Margins.Right = 0

        End With

        If PrintDialog1.ShowDialog = DialogResult.OK Then

            DialogsPrintDocument.PrinterSettings = PrintDialog1.PrinterSettings

            DialogsPrintDocument.Print()

        End If
    End Sub
End Class

 

24 Answers Found

 

Answer 1

Use a DrawImage Overload that takes a size.
 

Answer 2

i tried that still no go here is what i used.

Dim img As Image
     
        ' Create coordinates for upper-left corner

        ' of image  and for size of image.
        Dim x As Single = 100.0F
        Dim y As Single = 100.0F
        Dim width As Single
        Dim height As Single

        img = Image.FromFile("C:\xxx.jpg")
        img.RotateFlip(RotateFlipType.Rotate180FlipNone)

        width = img.Width
        height = img.Height

        e.HasMorePages = False

        e.Graphics.DrawImage(img, x, y, width, height)

        img.Save("C:\rotated.jpg")
        img.Dispose()

 

Answer 3

i tried that still no go here is what i used.

Dim img As Image
     
        ' Create coordinates for upper-left corner

        ' of image  and for size of image.
        Dim x As Single = 100.0F
        Dim y As Single = 100.0F
        Dim width As Single
        Dim height As Single

        img = Image.FromFile("C:\xxx.jpg")
        img.RotateFlip(RotateFlipType.Rotate180FlipNone)

        width = img.Width
        height = img.Height

        e.HasMorePages = False

        e.Graphics.DrawImage(img, x, y, width, height)

        img.Save("C:\rotated.jpg")
        img.Dispose()


Don't you want the image to fit the page?  You haven't changed anything with this code.
 

Answer 4

No i just want the actual image  size
 

Answer 5

I don't understand your problem then.  The code  in your first post should print  the image  at its actual size.  You need bigger sheets of paper.

 

Answer 6

Try this code.  Add a Button, a PrintDocument and a PrintPreviewDialog to a new WindowsForms application.  Set the PrintPrevievDialog1.Document to PrintDocument1.  Replace the code  on Form1 with

Imports System.Drawing.Printing
Public Class Form1
  
Dim Bmp As Bitmap
  
Private Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs) Handles Button1.Click
    
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
      PrintDocument1.OriginAtMargins = True
      Bmp = New Bitmap(OpenFileDialog1.FileName)
      
If Bmp.Width > Bmp.Height Then PrintDocument1.DefaultPageSettings.Landscape = True
      MessageBox.Show("Width = " + Bmp.Width.ToString + ", Height = " + Bmp.Height.ToString)
      PrintPreviewDialog1.ShowDialog()
    
End If
  End Sub
  Private Sub PrintDocument1_PrintPage(ByVal sender As ObjectByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
Dim WidthRatio As Double = e.MarginBounds.Width / Bmp.Width
    
Dim HeightRatio As Double = e.MarginBounds.Height / Bmp.Height
    
Dim Ratio As Double = If(WidthRatio < HeightRatio, WidthRatio, HeightRatio)
    e.Graphics.DrawImage(Bmp, 0, 0, 
CInt(Bmp.Width * Ratio), CInt(Bmp.Height * Ratio))
  
End Sub
End
 Class

Run the program and post what shows in the MessageBox.

 

Answer 7

width = 2151

height = 1445

 

impage prints but is scaled

paint says width and height same 120 dpi but prints full image  not scaled

 

Answer 8

You need paper (21.51 / 1.2) X (14.45 / 1.2) inches plus the hard margins to print  the image  at its recorded size.
 

Answer 9

ok so what is the 1.2 for ? 120 dpi?

i don't understand why paint  would print  the correct img size simply by hitting the print key.All i no is load the pic and print it. From the code  you gave me the  img is all there but dimensions  are not correct ( to small)

 

Answer 10

ok so what is the 1.2 for ? 120 dpi?

i don't understand why paint  would print  the correct img size simply by hitting the print key.All i no is load the pic and print it. From the code  you gave me the  img is all there but dimensions  are not correct ( to small)


How are you printing  a 17.925 inch x l2.04 inch image?  What is the size of your paper? 
 

Answer 11

its just 8.5 by 11 this is what i cant understand img prints to actual size from paint
 

Answer 12

its just 8.5 by 11 this is what i cant understand img prints to actual size from paint

The actual size of the image  is 17.925 X 12.04.  paint  can't print  that size image on an 8.5 x 11 sheet of paper.  If you set the margins to (0,0,0,0), the code  I posted will print the image as large as it can be printed  on the paper in the printer.
 

Answer 13

0,0,0,0 gives no image. I wanted to attach a jpg  but i can't see how to do it in this forum.

What i  am trying to do is print  a img of a dvd cover from vb net. I just had a look online

can you try this pic with your code  with the ratio 1.2 you will find it prints but not to the correct size unless you throw it into paint.

http://www.cdcovers.cc/view/268354/front/the-departed-special-edition-r1

 

Answer 14

Here is the code  modified to set the margins to (0,0,0,0)

Imports System.Drawing.Printing
Public Class Form1
  
Dim Bmp As Bitmap
  
Private Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs) Handles Button1.Click
    
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
      PrintDocument1.DefaultPageSettings.Margins = New Margins(0, 0, 0, 0)
      PrintDocument1.OriginAtMargins = 
True
      Bmp = New Bitmap(OpenFileDialog1.FileName)
      
If Bmp.Width > Bmp.Height Then PrintDocument1.DefaultPageSettings.Landscape = True
      MessageBox.Show("Width = " + Bmp.Width.ToString + ", Height = " + Bmp.Height.ToString)
      PrintPreviewDialog1.ShowDialog()
    
End If
  End Sub
  Private Sub PrintDocument1_PrintPage(ByVal sender As ObjectByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
Dim WidthRatio As Double = e.MarginBounds.Width / Bmp.Width
    
Dim HeightRatio As Double = e.MarginBounds.Height / Bmp.Height
    
Dim Ratio As Double = If(WidthRatio < HeightRatio, WidthRatio, HeightRatio)
    e.Graphics.DrawImage(Bmp, 0, 0, 
CInt(Bmp.Width * Ratio), CInt(Bmp.Height * Ratio))
  
End Sub
End
 Class

Can you explain how paint  is printing  a 17.925 x 12.04 image  on an 8.5 x 11 sheet of paper and it is printing it actual zise? The numbers you have posted must be incorrect.  Either the number of pixels or the pixels per inch.  What are the dimensions  and dpi in Paint? 

 

Answer 15

ok will try the code. Gone till Sat. dimensions  are correct 120 dpi verified with your pgm and paint  properties!
 

Answer 16

ok will try the code. Gone till Sat. dimensions  are correct 120 dpi verified with your pgm and paint  properties!
But the actual size that you want doesn't conform to the dimensions.  What size do you want the print  out to be and how did you determine the size?
 

Answer 17

getting very close now just a`small prob with about .25" of image  on right margin getting cropped but the overall img dimensions  are ok. Have no answer about why img with

w 3240 h 2175 prints on page. Will play with it a bit on Monday

 

 

Answer 18

here is the latest code print  preview shows entire image  and looks to be  scaled correctly

However when actually sending it to the printer i loose .5" strip of the right hand size of the image. The width is correct though. Loading with paint  prints fine! This img is

2146* 1431 pixels 120 dpi. I don't know what else to try. JohnWein your last code  does the same thing.

 

Dim bmp As Bitmap
 Private WithEvents DialogsPrintDocument As PrintDocument

 Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage

  Dim WidthRatio As Double = e.MarginBounds.Width / bmp.Width
  Dim HeightRatio As Double = e.MarginBounds.Height / Bmp.Height
  Dim Ratio As Double = If(WidthRatio < HeightRatio, WidthRatio, HeightRatio)
  ' Create rectangle for displaying image.
  Dim rect As New RectangleF(0.0F, 0.0F, 1100.0F, 700.0F)
  'e.Graphics.DrawImage(bmp, 0, 0, CInt(bmp.Width * Ratio), CInt(bmp.Height * Ratio))
  e.Graphics.DrawImage(bmp, rect)

 End Sub

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

  If OpenFileDialog1.ShowDialog = DialogResult.OK Then
   PrintDocument1.DefaultPageSettings.Landscape = True
   PrintDocument1.DefaultPageSettings.Margins = New Margins(0, 1100, 0, 850)
   PrintDocument1.OriginAtMargins = False
   bmp = New Bitmap(OpenFileDialog1.FileName)

   PageSetupDialog1.ShowDialog() 
PrintPreviewDialog1.ShowDialog()
 End If

 

 

 

Answer 19

The code  I posted will print  your image  within the bounds of the rectangle described by the margins.  When you decide what the size of the image should be, simply set the Left and Top margins to the hard margins and the right and bottom margins to size the image to your desired size.
 

Answer 20

just not getting this img needs to be this 10.25 by 7

would this not work? Cause it don't

PrintDocument1.DefaultPageSettings.Margins = New
 Margins(0, 1100, 0, 850)

or
PrintDocument1.DefaultPageSettings.Margins = New Margins(0, 1025, 0, 700)

 

Answer 21

You have to set the margins as I detailed in my last post.  You'll probably need additional guidance from the help  files.  Or, you can keep guessing until you get what you want.
 

Answer 22

Ok will play some more with it however your margin setting from last post did not print  correctly on my printer. Print preview showed no loss of img. Hardcopy looses .5" on right margin
 

Answer 23

You must set where the image  will be printed.  Here's another way to do it. This code  will print  an image in 4" x 3" landscape  orientation at the location of the printer hard margins: 

Imports System.Drawing.Printing
Public Class Form1
  
Dim Bmp As Bitmap
  
Private Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs) Handles Button1.Click
    
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
      With PrintDocument1.DefaultPageSettings
        .Landscape = 
True
        .Margins = New Margins(CInt(.HardMarginY), 0, CInt(.HardMarginX), 0)
      
End With
      PrintDocument1.OriginAtMargins = True
      Bmp = New Bitmap(OpenFileDialog1.FileName)
      PrintPreviewDialog1.ShowDialog()
    
End If
  End Sub
  Private Sub PrintDocument1_PrintPage(ByVal sender As ObjectByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    e.Graphics.DrawImage(Bmp, 0, 0, 400, 300)
  
End Sub
End
 Class

 

Answer 24

Hi John and everyone,

I think he needs to study a little on

http://www.bobpowell.net/coordinatesystems.htm

John if my memory severd me correctly  you were the one who gave me this site. But I think you also gave a site that diminstartes how the corordinte systems is related to printers. But I haven't found it. If it was you that gave me that site too, he might gain from it as I did.

Curtis

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter