rotating uploaded images

Hello, I currently have a function that resizes uploaded images, but when the user uploads the image sideways I would like to rotate it right side up.  I'm wondering if there is any way to detect whether or not the image is sideways, and if so, rotate it right side up.
Who is Participating?
Robert SchuttConnect With a Mentor Software EngineerCommented:
Assuming you don't want an external tool, this seems like a nice one: 

It gives you a library function that you can use to get the EXIF field "Orientation", which you should be able to use to rotate the image.

Note: you might still want to build an option on your site to manually rotate images, in my experience you can still have problem files because
1) some (older) cameras don't record the orientation.
2) if somebody doesn't take care when taking the picture (especially pointing straight up or down) the orientation can be recorded wrong. Of course, even more modern and/or expensive cameras give the option to correct that right away.
Robert SchuttSoftware EngineerCommented:
If you are talking about digital photos then you can usually get the rotation from the EXIF data within the file. I've done it in a perl script (download an EXIF info module from CPAN), and for a photobook on a classic asp website using ImageMagick which also has that option built in. I've not done it in ASP.NET but should be possible. I see a couple of results on as well...
jtmerchAuthor Commented:
Ok thanks for the answer, but it doesn't help with this situation.  I'm having trouble finding anything on the web that addresses this, are there any documents or tutorials that tells me how to rotate a sideways digital photo/image in .net?
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

jtmerchAuthor Commented:
Ok I understand, thanks for your help!
Robert SchuttSoftware EngineerCommented:
Ah sorry, misunderstood you there, I thought the rotating would be the easy part. :-]

something like this maybe: 

What are you using now to resize the image?
Robert SchuttSoftware EngineerCommented:
I seem to be cross-posting all the time, my mail is lagging behind... Glad to be of assistance!
jtmerchAuthor Commented:
I am using the following code to resize:

 Private Sub ResizeImage(ByVal newImage As String, ByVal origImage As String, ByVal desiredWidth As Integer)

            Dim photo As System.Drawing.Bitmap

            origImage = Replace(origImage, "\", "/")
            newImage = Replace(newImage, "\", "/")

            photo = New System.Drawing.Bitmap(origImage)
            'Get current size
            Dim oWidth As Integer = photo.Width
            Dim oHeight As Integer = photo.Height

            'Declare resize to variables
            Dim w As Integer
            Dim h As Integer

            'Resize to medium
            If oWidth < desiredWidth Then
                w = oWidth
                h = oHeight
                w = desiredWidth
                h = photo.Height / (photo.Width / w)
            End If

            Dim medium As New System.Drawing.Bitmap(w, h)
            Dim mG As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(medium)

            mG.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
            mG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic
            mG.DrawImage(photo, 0, 0, w, h)

            Dim myImageCodecInfo As System.Drawing.Imaging.ImageCodecInfo
            myImageCodecInfo = GetEncoderInfo("image/jpeg")
            Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
            Dim myEncoderParameters As System.Drawing.Imaging.EncoderParameters = New System.Drawing.Imaging.EncoderParameters(1)
            Dim myEncoderParameter As System.Drawing.Imaging.EncoderParameter = New System.Drawing.Imaging.EncoderParameter(myEncoder, 100)
            myEncoderParameters.Param(0) = myEncoderParameter

            medium.Save(newImage, myImageCodecInfo, myEncoderParameters)

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub
jtmerchAuthor Commented:
Ok thanks for the new post.  Again, what I'm trying to do is detect whether or not it is sideways and rotate, that's where I'm having trouble, rotating based on whether or not the image is sideways.
Robert SchuttSoftware EngineerCommented:
Actually you may not need external code. I'm having a look now, for you it would be:

Open in new window

It looks like the info may be in there.
jtmerchAuthor Commented:
Ok great, and any idea what I would look for to tell whether or not the photo is sideways
Robert SchuttSoftware EngineerCommented:
With the information from that ExifWorks class I saw that property item with ID &H112 is Orientation, so I made a little program to extract just that value which contains 2 bytes. So far I have 2 images, 1 is 'normal', landscape and the property value is 01/00, another image (from another camera) is upright, actually would need to be rotated 90 degrees clockwise and contains 06/00. Wait I'll just make some new images!
jtmerchAuthor Commented:
Ahhh, ok thanks!
Robert SchuttSoftware EngineerCommented:
Hmm, that's interesting. My camera doesn't seem to record being upside down :-(

It does however record being turned 'portrait' left or right:
capturethe code I used (I just called this with the individual file names for now):
Imports System.Drawing
Imports System.Drawing.Imaging

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Const C_ID_Orientation As Short = &H112

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub PrintImageInfo(imgname As String)
        Dim img As New Bitmap(Server.MapPath("~/images") & "/" & imgname)
        Response.Write("<table border=1>")
        Response.Write("<tr><td rowspan=2><img src='images/" & imgname & "' height=150></td><th colspan=2>" & imgname & "</th></tr>")
        For Each propitem As PropertyItem In img.PropertyItems
            If propitem.Id = C_ID_Orientation Then
                Response.Write("<tr><td>" & propitem.Id & "</td><td>" & GetPropertyItemValue(propitem) & "</td></tr>")
            End If
    End Sub

    Protected Function GetPropertyItemValue(pm_pi As PropertyItem)
        Dim str As String = ""
        For Each b As Byte In pm_pi.Value
            str &= String.Format("{0:X2}", b)
        Return str
    End Function

End Class

Open in new window

jtmerchAuthor Commented:
Wow, this is great enough, thanks again for all your help.  Awesome!
Robert SchuttSoftware EngineerCommented:
OK, enjoy!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.