none
[VB.NET] Can we draw an image in PictureBox without rotation? RRS feed

  • Question

  • Hi all,
    A PictureBox in Windows Form slways shows an image from a file as a landscape image, even if it is portrait originally.
    Me.PictureBox.Image = Image.FromFile(...file name with full path...)
    Actual screenshot of the program.
       OK: landscape image
          
       NG: portrait originally

          

    P.S.
      I don't use EXIF meta data if files have it.
      When a file has EXIF data, a portrait image seems to be rotated as landscape (rotated with 90 degrees).

    Regards,

    Ashidacchi -- http://hokusosha.com




    • Edited by Ashidacchi Wednesday, January 22, 2020 7:39 AM
    Wednesday, January 22, 2020 4:26 AM

Answers

  • Hi tommytwotrain,

    Thank you. I appreciate taking your precious time. 

    My answers are below. 

    1) I took this photo with the camera rotated (as we do when taking a tall tower: I say this as vertical position).
    I don't rotate it in code.

    2) A PictureBox in a Windows Form shows a picture without rotation, or shows it as it is taken by a camera with horizontal position.
    As a result, a picture took by a camera with vertical position will be seen rotated in the PictureBox.

      Here's a flow of 1) and 2)

       

    3) So, I need to get EXIF data (Orientation) in the photo and rotate it in code (this is shown in your code: RotateFlip), in order to see it natural.

    4) I have code which gets EXIF data and rotate an image using Rotate.Flip.  I should have used it in my current software.

    Thank you so much.
    (Sorry for my poor English. I'd like to close this thread.)

    Regards,

    Ashidacchi -- http://hokusosha.com




    • Marked as answer by Ashidacchi Thursday, January 23, 2020 1:03 AM
    • Edited by Ashidacchi Thursday, January 23, 2020 1:11 AM
    Thursday, January 23, 2020 12:50 AM

All replies

  • Ash,

    I can not reproduce the problem?

    However I am not sure I understand the problem?

    Please give the code for a reproducible example of the problem. Include an image that reproduces the problem.

    When I put my .jpg camera images in a picturebox they don't rotate (ie they stay the position the camera was in when the photo was taken). How you you make your images rotate now?


    Wednesday, January 22, 2020 11:31 AM
  • Hi tommytwotrain,

    Thank you for reply.
    Sorry, I cannot (won't) provide whole my code. Instead, I will explain my scenario:
      1) user: drag & drop a folder or a file/files onto Windows Form
      2) program: accept user's drop if file extension is .jpg, .png, .bmp, etc.
          and store file path in an array and DataGridView
          (the array stores file path, the dgv stores the same file path as the array and other information)   3) user: select a row of the DataGridView
      4) program: draw an image of the file (selected row of the DGV = the array) 

    Code :(3-4)
    Private Sub dgv_File_SelectionChanged(sender As Object, e As EventArgs) Handles dgv_File.SelectionChanged
        If (Me.IgnoreEvent) Then Exit Sub
        ' ---
        Dim myRow As Integer = Me.dgv_File.CurrentCell.RowIndex
        Me.pct_Picture.Image = Image.FromFile(Me.Ary_DD_FileName(myRow))
    End Sub
    
    I've shared a JPEG file which I took by my digital camera (no editing).
    ASH_6146.JPG   (you can see its EXIF data: Orientation=270 CW)

    ... Oh, I got it. I should rotate an image by EXIF data...
    Regards,

    Ashidacchi -- http://hokusosha.com

    Wednesday, January 22, 2020 1:02 PM
  • Hi tommytwotrain,

    Thank you for reply.
    Sorry, I cannot (won't) provide whole my code. Instead, I will explain my scenario:
      1) user: drag & drop a folder or a file/files onto Windows Form
      2) program: accept user's drop if file extension is .jpg, .png, .bmp, etc.
          and store file path in an array and DataGridView
          (the array stores file path, the dgv stores the same file path as the array and other information)   3) user: select a row of the DataGridView
      4) program: draw an image of the file (selected row of the DGV = the array) 

    Code :(3-4)
    Private Sub dgv_File_SelectionChanged(sender As Object, e As EventArgs) Handles dgv_File.SelectionChanged
        If (Me.IgnoreEvent) Then Exit Sub
        ' ---
        Dim myRow As Integer = Me.dgv_File.CurrentCell.RowIndex
        Me.pct_Picture.Image = Image.FromFile(Me.Ary_DD_FileName(myRow))
    End Sub
    I've shared a JPEG file which I took by my digital camera (no editing).
    ASH_6146.JPG   (you can see its EXIF data: Orientation=270 CW)

    ... Oh, I got it. I should rotate an image by EXIF data...
    Regards,

    Ashidacchi -- http://hokusosha.com

    I cant reproduce the problem.

    Public Class Form1
        Private pct_Picture As New PictureBox With {.Parent = Me,
            .Dock = DockStyle.Fill, .SizeMode = PictureBoxSizeMode.Zoom}
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.pct_Picture.Image = Image.FromFile("c:\bitmaps\ASH_6146.JPG")
        End Sub
    End Class


    PS The example makes a picturebox with code. To run the example just paste the code into a new empty form. Change the form name as required.

    Wednesday, January 22, 2020 1:22 PM
  • PS Was the camera rotated when that image was taken? Are you sure this is the portrait image?

    Here is an easy way to rotate the image if that helps.

    PS Are you sure your control is not already rotating?

    Public Class Form4
        Private pct_Picture As New PictureBox With {.Parent = Me,
            .Dock = DockStyle.Fill, .SizeMode = PictureBoxSizeMode.Zoom}
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Me.pct_Picture.Image = Image.FromFile("c:\bitmaps\ASH_6146.JPG")
    
            pct_Picture.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
        End Sub
    End Class


    Wednesday, January 22, 2020 2:04 PM
  • Hi tommytwotrain,

    Thank you. I appreciate taking your precious time. 

    My answers are below. 

    1) I took this photo with the camera rotated (as we do when taking a tall tower: I say this as vertical position).
    I don't rotate it in code.

    2) A PictureBox in a Windows Form shows a picture without rotation, or shows it as it is taken by a camera with horizontal position.
    As a result, a picture took by a camera with vertical position will be seen rotated in the PictureBox.

      Here's a flow of 1) and 2)

       

    3) So, I need to get EXIF data (Orientation) in the photo and rotate it in code (this is shown in your code: RotateFlip), in order to see it natural.

    4) I have code which gets EXIF data and rotate an image using Rotate.Flip.  I should have used it in my current software.

    Thank you so much.
    (Sorry for my poor English. I'd like to close this thread.)

    Regards,

    Ashidacchi -- http://hokusosha.com




    • Marked as answer by Ashidacchi Thursday, January 23, 2020 1:03 AM
    • Edited by Ashidacchi Thursday, January 23, 2020 1:11 AM
    Thursday, January 23, 2020 12:50 AM