Click here to Skip to main content
15,889,034 members
Articles / Programming Languages / Visual Basic 12
Tip/Trick

Detect Image is Upside Down

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
2 Jun 2014CPOL 11.3K   2  
Check image is upside down in VisualBasic.NET

Introduction

The following function may used to detect if an image is upside down or not.
The function will check the BitmapData.Stride value and returned value is of a Boolean type and if the returned value of BitmapData.Stride is greater than zero...then the bitmap is upside down. For further details about BitmapData class, please check this link.

Function

VB.NET
//   ''' <summary>
   ''' Check image is upside down 
    ''' </summary>
    ''' <param name="originalBitmap">bitmap</param> 
    ''' <returns>True, if the image is upside dwn</returns> 
    ''' <remarks></remarks> 
    Private Function IsUpSideDownBitmap(originalBitmap As Bitmap) As Boolean 
        Dim bmp As Bitmap = CType(originalBitmap.Clone(), Bitmap) 
        Dim rect As Rectangle = New Rectangle(0, 0, bmp.Width, bmp.Height) 
        Dim bmpData As Imaging.BitmapData = bmp.LockBits
        (rect, Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format32bppArgb) 
        Return originalBitmap IsNot Nothing AndAlso bmpData.Stride > 0 
    End Function 

Using the Code

VB.NET
 ' Define any image
 Dim bmp As New Bitmap(My.Resources._29322)
' check image, the function shall return true if the image is upside down
 If IsUpSideDownBitmap(bmp) Then
     ' do something like rotate the image or do not draw it
     ' the image will drawn becuase the function return True
     e.Graphics.DrawImage(bmp, 0, 0)
 End If

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Egypt Egypt
Oil & Gas Engineer
C# & VB.net
Coding For Fun Only

Comments and Discussions

 
-- There are no messages in this forum --