• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1365
  • Last Modified:

Asp.net/Vb.net 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.
0
jtmerch
Asked:
jtmerch
  • 8
  • 7
1 Solution
 
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 codeproject.com as well...
0
 
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?
0
 
Robert SchuttSoftware EngineerCommented:
Assuming you don't want an external tool, this seems like a nice one: http://www.codeproject.com/Articles/4956/The-ExifWorks-class 

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.
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

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

something like this maybe: http://msdn.microsoft.com/en-us/library/system.drawing.image.rotateflip(v=vs.100).aspx 

What are you using now to resize the image?
0
 
Robert SchuttSoftware EngineerCommented:
I seem to be cross-posting all the time, my mail is lagging behind... Glad to be of assistance!
0
 
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)

        Try
            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
            Else
                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

            'Save
            medium.Save(newImage, myImageCodecInfo, myEncoderParameters)
            medium.Dispose()
            photo.Dispose()



        Catch ex As Exception
            Response.Write(ex.Message)
            Exit Sub
        End Try

    End Sub
0
 
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.
0
 
Robert SchuttSoftware EngineerCommented:
Actually you may not need external code. I'm having a look now, for you it would be:
photo.PropertyItems

Open in new window

It looks like the info may be in there.
0
 
jtmerchAuthor Commented:
Ok great, and any idea what I would look for to tell whether or not the photo is sideways
0
 
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!
0
 
jtmerchAuthor Commented:
Ahhh, ok thanks!
0
 
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
        Next
        Response.Write("</table>")
    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)
        Next
        Return str
    End Function

End Class

Open in new window

0
 
jtmerchAuthor Commented:
Wow, this is great enough, thanks again for all your help.  Awesome!
0
 
Robert SchuttSoftware EngineerCommented:
OK, enjoy!
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now