Results 1 to 20 of 20

Thread: JPEG Auto Rotate Picture Box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    JPEG Auto Rotate Picture Box

    Hi.
    My project have a Picture box control for loading pictures but it's worked fine
    my problem is...
    some vertical Jpg picture was shown Horizontally in windows explorer and also my picutre box control at that same that file was opened with vertically in editors like photoshop. but i need not picture rotate codes i want to Vertical picture was shown as vertically into my picture box control is't possible,
    please help me.
    ..........SORRY FOR MY POOR ENGLISH...

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: JPEG Auto Rotate Picture Box

    The image will be displayed in a PictureBox with its original orientation. For instance, if you rotate a camera 90 degrees to take a photo, the resulting image will be displayed rotated by 90 degrees. The image doesn't know that you want it to be rotated. Some image editing software may be smart enough to recognise such images and rotate them automatically, but the logic to do that is very, very sophisticated. If you want a VB app to display an image rotated by 90 degrees then you have to specifically code it to do so. Also, it cannot be done by setting the Image of a PictureBox. You would have to draw the Image yourself using GDI+, transforming a Graphics object first. E.g.

    http://www.vbforums.com/showthread.p...otatetransform
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    Re: JPEG Auto Rotate Picture Box

    Thank You Mr jmcilhinney
    Thanks for your nice informations...........

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: JPEG Auto Rotate Picture Box

    It's easier to do it with an Image.RotateFlip statement:

    Code:
    'to rotate 90 degrees clockwise:
    
    PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone) 
    
    'to rotate 90 degrees anticlockwise: 
    PictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
    BB

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: JPEG Auto Rotate Picture Box

    Quote Originally Posted by boops boops View Post
    It's easier to do it with an Image.RotateFlip statement:

    Code:
    'to rotate 90 degrees clockwise:
    
    PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone) 
    
    'to rotate 90 degrees anticlockwise: 
    PictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
    BB
    Good point. I forgot that that was available for multiples of 90 degrees.

    Do keep in mind that the change only affects the current Image object, so if you want to update the original file then you would have to call its Save method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: JPEG Auto Rotate Picture Box

    Quote Originally Posted by boops boops View Post
    It's easier to do it with an Image.RotateFlip statement:

    Code:
    'to rotate 90 degrees clockwise:
    
    PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone) 
    
    'to rotate 90 degrees anticlockwise: 
    PictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
    BB
    i was going to say...

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    Re: JPEG Auto Rotate Picture Box

    Hello Friends. thanks for your reply's
    this is my need.......a folder contain 100 wedding photos lot of Horizontal and vertical(it's show horizontally in WIndows Explorer Thumbnail Mode but in Image Editor shows Vertically) orientation
    so i want to find out how much Horizontal and Vertical photos are in this folder.


    'to rotate 90 degrees clockwise:
    PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
    'to rotate 90 degrees anticlockwise:
    PictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)


    how can use this code to find HV orientation contain 100 Files Folder............

    Thanks..............

  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: JPEG Auto Rotate Picture Box

    Hi medsont,

    The code I gave you only turns the image in the picture box. It doesn't find the orientation. To do that, you have to analyse the EXIF data which digital cameras store in their JPEG images.

    In VB.Net, EXIF data is found in the class Imaging.PropertyItems. I have used that myself for getting embedded thumbnails but I haven't tried it yet for getting orientation information. Still, it's something I would like to do, and I know it's possible, so I'll see if I can work out some code. In the meantime, if anyone else has a readymade solution ... please post!

    BB

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    Re: JPEG Auto Rotate Picture Box

    Hey Boops Boops thanks for your great reply i solved my problem myself i am very glad to post here
    ya i got the Orientation flag from the Exif data and rotated my pictures, yup thanks for your nice info
    today i will sleep with good dreams.........
    Thanks for your great helps...............

  10. #10

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: JPEG Auto Rotate Picture Box

    ok boops, i'm begging howto?

  12. #12
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: JPEG Auto Rotate Picture Box

    OK Paul, as it's you you're not required to grovel (much). Here's a function that returns a correctly oriented image from a digital camera JPEG file:

    vb.net Code:
    1. Private Function OrientedImageFromFile(ByVal photoFileName As String) As Image
    2.    Dim img As Image = Image.FromFile(photoFileName) 'get the image
    3.    Dim pi As Imaging.PropertyItem = Array.Find(img.PropertyItems, _
    4.         Function(T As Imaging.PropertyItem) T.Id = &H112) '&H112 = PropertyTagOrientation
    5.    Select Case pi.Value(0) 'value is array of Int16
    6.        Case 3 : img.RotateFlip(RotateFlipType.Rotate180FlipNone) 'upside-down
    7.        Case 6 : img.RotateFlip(RotateFlipType.Rotate90FlipNone) 'rotated right
    8.        Case 8 : img.RotateFlip(RotateFlipType.Rotate270FlipNone) 'rotated left
    9.    End Select
    10.    Return img
    11. End Function

    BB

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: JPEG Auto Rotate Picture Box

    it doesn't work with jpg's from my nokia.
    pi.value always =

    (0)1
    (1)0

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: JPEG Auto Rotate Picture Box

    i did some research. i can't fault your code. must be my jpg

  15. #15
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: JPEG Auto Rotate Picture Box

    Now Paul, don't they have real cameras in Chelmsford? It looks like your Nokia doesn't write EXIF data, or at least not the Orientation field.

    I've only tried it with my own camera (Panasonic FZ50) but I should think all recent cameras get at least the orientation right. To come to think of it, the last time I saw images wrongly oriented in Windows Explorer they were from a mobile phone. I wonder if phones have their own way of encoding orientation?

    BB

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: JPEG Auto Rotate Picture Box

    it's not a new phone

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    Re: JPEG Auto Rotate Picture Box

    hey paul do you want to EXIF binary .......you can modify and edit Exif data. and save .............

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: JPEG Auto Rotate Picture Box

    Quote Originally Posted by medsont View Post
    hey paul do you want to EXIF binary .......you can modify and edit Exif data. and save .............
    no. the images i was using don't contain orientation values

  19. #19
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    340

    Lightbulb Re: JPEG Auto Rotate Picture Box

    Quote Originally Posted by boops boops View Post
    OK Paul, as it's you you're not required to grovel (much). Here's a function that returns a correctly oriented image from a digital camera JPEG file:
    .....
    BB
    Ok, sir, i know it's very late, but still, as i come to this thread today, others may come to later also. so, i decided to reply,

    your mentioned code will throw error (or even crash without proper error handling), if the image/file does not have the correct exif tag.

    that's why i added a line to your code and hope it may help noobs like me

    vb.net Code:
    1. Private Function OrientedImageFromFile(ByVal photoFileName As String) As Image
    2.         Dim img As Image = Image.FromFile(photoFileName) 'get the image
    3.         Dim pi As Imaging.PropertyItem = Array.Find(img.PropertyItems, _
    4.              Function(T As Imaging.PropertyItem) T.Id = &H112) '&H112 = PropertyTagOrientation
    5.         If pi IsNot Nothing Then
    6.             Select Case pi.Value(0) 'value is array of Int16
    7.                 Case 3 : img.RotateFlip(RotateFlipType.Rotate180FlipNone) 'upside-down
    8.                 Case 6 : img.RotateFlip(RotateFlipType.Rotate90FlipNone) 'rotated right
    9.                 Case 8 : img.RotateFlip(RotateFlipType.Rotate270FlipNone) 'rotated left
    10.             End Select
    11.         End If
    12.         Return img
    13.     End Function

    hope it may help someone, some day..

    of course nevertheless, thanks for sharing your code.
    Last edited by Shohag_ifas; Sep 30th, 2019 at 08:32 AM.

  20. #20
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: JPEG Auto Rotate Picture Box

    The function could certainly be better designed, but I don't think a missing exif tag is the problem: If pi IsNot Nothing deals with that. But if I were writing the Function now I would leave Image.FromFile out and pass the Image as an argument instead of the file name. Image.FromFile would belong a separate Sub or Function, where it should get some extra care such as checking the file extension first (*.jpg, *.jpeg etc.) and Try...Catch to protect against an incorrect file format. BB

    [edit]I just realized I was looking at the wrong code. You added If pi IsNot Nothing yourself, and I agree it's better.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width