I have the following problem.
when i pres buttun printpreview (button 5) the the print preview fires up.
and when i close the print preview without printing and minimize the form (after close the printpreview),
and when i maximize the form again the image in the picturebox is turned 90 dergees. How is that pissible ?
i have the following code for print preview.

Public Class Form1
Dim fotoheight As Integer
Dim fotowidth As Integer

Private Sub Button5_MouseDown(sender As Object, e As MouseEventArgs) Handles Button5.MouseDown
        If e.Button = MouseButtons.Left Then
            If PictureBox1.Image Is Nothing Then
                MessageBox.Show("Er is niets te printen, er is geen akte aanwezig.",
                              "opmerking.",
                              MessageBoxButtons.OK,
                              MessageBoxIcon.Information)
                Return
            End If
            Dim imgwidth As Integer
            Dim imgheight As Integer
            imgwidth = Me.PictureBox1.Image.Width
            imgheight = Me.PictureBox1.Image.Height
            If imgwidth > imgheight Then
                PictureBox1.Image.RotateFlip((RotateFlipType.Rotate90FlipNone))
                PrintPreviewDialog1.Document = PrintDocument1
                PrintPreviewDialog1.ShowDialog()
            Else
                PrintPreviewDialog1.Document = PrintDocument1
                PrintPreviewDialog1.ShowDialog()
            End If
        End If
    End Sub

    Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
     Dim newimage As Image = PictureBox1.Image
        Dim thumbimage As Bitmap
        Dim originalimage As Bitmap
        fotoheight = 1000
        fotowidth = 1000
        If (newimage.Width >= 1000 And newimage.Height <= 1000) Or (newimage.Width <= 1000 And newimage.Height >= 1000) Then
            fotowidth = 1000
            fotoheight = 1000
        End If
        If newimage.Width < newimage.Height Then
            If newimage.Width <= 1000 And newimage.Height <= 1000 Then
                fotowidth = newimage.Height
                fotoheight = newimage.Height
            End If
        End If
        If newimage.Width > newimage.Height Then
            If newimage.Width <= 1000 And newimage.Height <= 1000 Then
                fotowidth = newimage.Width
                fotoheight = newimage.Width
            End If
        End If

        Dim newwidth As Integer
        Dim newheight As Integer
        originalimage = newimage
        If originalimage.Width > originalimage.Height Then
            newheight = originalimage.Height / originalimage.Width * fotoheight
            newwidth = fotowidth
        Else
            newheight = fotoheight
            newwidth = originalimage.Width / originalimage.Height * fotowidth
        End If
        Dim img As Image = Me.PictureBox1.Image
        Dim image As New Bitmap(img)

        '  Dim image As Bitmap = Me.PictureBox1.Image
        thumbimage = New Bitmap(newwidth, newheight)
        Dim gr As Graphics = Graphics.FromImage(thumbimage)
        gr.DrawImage(originalimage, 0, 0, newwidth, newheight)
        ' PictureBox1.Image = thumbimage

        Dim g As Graphics = e.Graphics
        g.PageUnit = GraphicsUnit.Inch
        Dim imga As Image = thumbimage
        Dim gg As Graphics = Graphics.FromImage(imga)
        Dim marginBounds As RectangleF = e.MarginBounds
        If Not PrintDocument1.PrintController.IsPreview Then marginBounds.Offset(-e.PageSettings.HardMarginX, -e.PageSettings.HardMarginY)
        Dim x As Single = marginBounds.X / 100.0F + (marginBounds.Width / 100.0F - CSng(img.Width) / gg.DpiX) / 2.0F
        Dim y As Single = marginBounds.Y / 100.0F + (marginBounds.Height / 100.0F - CSng(img.Height) / gg.DpiY) / 2.0F
        g.DrawImage(img, x, y)
        gg.Dispose()
        End Sub

        End Class

Thanks in advice John

Why gg.Dispose() ??

fro my opinion from there your problem has been arised.

The problem is in here
PictureBox1.Image.RotateFlip((RotateFlipType.Rotate90FlipNone))

if i delete it, then there is no problem, only the image is not turned 90 degrees in prinpreview. And i want it to turn 90 degrees in print preview. when
image.with > image.height

oké i have the siolotion found.
I changed a lot of code into the following:

in the properties from PrintPreviewDialog1 i changed the document in PrintDocument1

and the code
into

 Private Sub Button5_MouseDown(sender As Object, e As MouseEventArgs) Handles Button5.MouseDown
        If e.Button = MouseButtons.Left Then
            If PictureBox1.Image Is Nothing Then
                MessageBox.Show("Er is niets te printen, er is geen akte aanwezig.",
                              "opmerking.",
                              MessageBoxButtons.OK,
                              MessageBoxIcon.Information)
                Return
            End If
             PrintPreviewDialog1.ShowDialog()
             End If
    End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
 Dim imgwidth As Integer
        Dim imgheight As Integer
        imgwidth = Me.PictureBox1.Image.Width
        imgheight = Me.PictureBox1.Image.Height
        If imgwidth > imgheight Then
            Dim g As Graphics = e.Graphics
            g.PageUnit = GraphicsUnit.Inch
            Dim imga As New Bitmap(Me.PictureBox1.Image)
            imga.RotateFlip((RotateFlipType.Rotate90FlipNone))
            Dim gg As Graphics = Graphics.FromImage(imga)
            Dim marginBounds As RectangleF = e.MarginBounds
            If Not PrintDocument1.PrintController.IsPreview Then marginBounds.Offset(-e.PageSettings.HardMarginX, -e.PageSettings.HardMarginY)
            Dim x As Single = marginBounds.X / 100.0F + (marginBounds.Width / 100.0F - CSng(imga.Width) / gg.DpiX) / 2.0F
            Dim y As Single = marginBounds.Y / 100.0F + (marginBounds.Height / 100.0F - CSng(imga.Height) / gg.DpiY) / 2.0F
            G.DrawImage(imga, x, y)
              gg.Dispose()

        Else

            Dim g As Graphics = e.Graphics
            g.PageUnit = GraphicsUnit.Inch
            Dim imga As Image
            imga = Me.PictureBox1.Image

            Dim gg As Graphics = Graphics.FromImage(imga)
            Dim marginBounds As RectangleF = e.MarginBounds
            If Not PrintDocument1.PrintController.IsPreview Then marginBounds.Offset(-e.PageSettings.HardMarginX, -e.PageSettings.HardMarginY)
            Dim x As Single = marginBounds.X / 100.0F + (marginBounds.Width / 100.0F - CSng(imga.Width) / gg.DpiX) / 2.0F
            Dim y As Single = marginBounds.Y / 100.0F + (marginBounds.Height / 100.0F - CSng(imga.Height) / gg.DpiY) / 2.0F
            g.DrawImage(imga, x, y)
            gg.Dispose()
        End If

    End Sub

This works and image is exacly in centre of page and problem with picturebox is gone.
Thanks for your time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.