Wednesday, August 1, 2012

VB.NET Function to Rotate Row Image with Bitmap in Silverlight


Couldn't find a good one to rotate images in Silverlight. The build-in RenderTransform is only for rendering on page instead of updating original. Here's the function I wrote for rotating the original images, it can be saved to bytes.

Public Function iImageRotate(ByRef bi As BitmapImage, ByVal CounterclockwiseAngle As Integer) As WriteableBitmap
        Try
            Dim wb As WriteableBitmap = New WriteableBitmap(bi)
            Dim wb_arr(,) As Integer
            ReDim Preserve wb_arr(wb.PixelWidth, wb.PixelHeight)
            For c As Integer = 0 To wb.PixelWidth
                For r As Integer = 0 To wb.PixelHeight
                    wb_arr(c, r) = wb.Pixels(wb.PixelWidth * r + c)
                Next
            Next
            Dim wb2 As WriteableBitmap
            Dim wb_arr_2(,) As Integer
            If (CounterclockwiseAngle = 0 Or CounterclockwiseAngle Mod 360 = 0) Then '0 360 plus degree
                wb2 = New WriteableBitmap(wb.PixelWidth, wb.PixelHeight)
                ReDim Preserve wb_arr_2(wb.PixelWidth, wb.PixelHeight)
                wb_arr_2 = wb_arr
            ElseIf (CounterclockwiseAngle = 90 Or CounterclockwiseAngle Mod 450 = 0) Then '90 450 plus degree
                wb2 = New WriteableBitmap(wb.PixelHeight, wb.PixelWidth)
                ReDim Preserve wb_arr_2(wb.PixelHeight, wb.PixelWidth)
                For c As Integer = 0 To (wb_arr.GetLength(0) - 1) 'width i
                    For r As Integer = 0 To (wb_arr.GetLength(1) - 1) 'height j
                        wb_arr_2(r, wb_arr.GetLength(0) - c - 1) = wb_arr(c, r) '90 Out[j, Right-i-1] = In[i, j]
                    Next
                Next
            ElseIf (CounterclockwiseAngle = 180 Or CounterclockwiseAngle Mod 540 = 0) Then '180 540 plus degree
                wb2 = New WriteableBitmap(wb.PixelWidth, wb.PixelHeight)
                ReDim Preserve wb_arr_2(wb.PixelWidth, wb.PixelHeight)
                For c As Integer = 0 To (wb_arr.GetLength(0) - 1) 'width i
                    For r As Integer = 0 To (wb_arr.GetLength(1) - 1) 'height j
                        wb_arr_2(wb_arr.GetLength(0) - c - 1, wb_arr.GetLength(1) - r - 1) = wb_arr(c, r) '180 Out[Right-i-1, Bottom-j-1] = In[i, j]
                    Next
                Next
            ElseIf (CounterclockwiseAngle = 270 Or CounterclockwiseAngle Mod 630 = 0) Then '270 630 plus degree
                wb2 = New WriteableBitmap(wb.PixelWidth, wb.PixelHeight)
                ReDim Preserve wb_arr_2(wb.PixelWidth, wb.PixelHeight)
                For c As Integer = 0 To (wb_arr.GetLength(0) - 1) 'width i
                    For r As Integer = 0 To (wb_arr.GetLength(1) - 1) 'height j
                        wb_arr_2(wb_arr.GetLength(1) - r - 1, c) = wb_arr(c, r) '270 Out[Bottom-j-1,i] = In[i, j]
                    Next
                Next
            Else
                Return Nothing
            End If

            For r As Integer = 0 To (wb_arr_2.GetLength(1) - 1)
                For c As Integer = 0 To (wb_arr_2.GetLength(0) - 1)
                    wb2.Pixels(wb2.PixelWidth * r + c) = wb_arr_2(c, r)
                Next
            Next

            Return wb2
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

No comments: