This is a migrated thread and some comments may be shown as answers.

RadRotator centering images

2 Answers 70 Views
Rotator
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 15 Sep 2015, 05:49 PM

I've got a lot of images I need to load in from a directory into a project where I display a full-screened radrotator on a secondary monitor. I want to be able to show a slideshow of pictures, but I need them to properly fit the screen, and I'd like ones that are strange sizes to be horizontally and vertically centered. All of this seems to be working with my code so far except the centering part...

Here is my code:

 

Imports System.Windows.Forms
Imports Telerik.WinControls.UI
Imports System.IO
Imports System.Security.Cryptography
 
Public Class formSlideshow
 
    Private Sub ShapedForm1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.FormBorderStyle = FormBorderStyle.None
        Me.Bounds = GetSecondaryScreen().Bounds
        Dim screensize As New Drawing.Size(Me.Width, Me.Height)
        RadRotator1.Size = screensize
        RadRotator1.Interval = (CInt(Val(formSettings.Interval)) * 1000)
 
        If LoadImages() = True Then RadRotator1.Start()
    End Sub
 
    '--Get size and location of secondary monitor
    Public Function GetSecondaryScreen() As System.Windows.Forms.Screen
        For Each windowscreen As System.Windows.Forms.Screen In System.Windows.Forms.Screen.AllScreens
            If Not windowscreen Is System.Windows.Forms.Screen.PrimaryScreen Then
                Return windowscreen
            End If
        Next
        Return Screen.PrimaryScreen
    End Function
 
    '--Load images into radRotator
    Public Function LoadImages() As Boolean
        Try
            Dim imageslist As New List(Of String)
 
            '--Get employees personal images
            For Each employeeimage As String In Directory.GetFiles(formSettings.EmployeeDir)
                If Path.GetExtension(employeeimage) = ".jpeg" Or Path.GetExtension(employeeimage) = ".png" Or Path.GetExtension(employeeimage) = ".jpg" Then
                    imageslist.Add(employeeimage)
                End If
            Next
 
            '--Get default images
            For Each defaultimage As String In Directory.GetFiles(formSettings.DefaultDir)
                If Path.GetExtension(defaultimage) = ".jpeg" Or Path.GetExtension(defaultimage) = ".png" Or Path.GetExtension(defaultimage) = ".jpg" Then
                    imageslist.Add(defaultimage)
                End If
            Next
 
            '--Randomize order of images
            imageslist.Sort(New Randomizer(Of String)())
 
            '--Add images to radRotator item list
            For Each imagefile As String In imageslist
                Dim newimage As Image = Image.FromFile(imagefile)
                Dim rotatoritem As New RadImageItem()
                rotatoritem.Image = ScaleImage(newimage, 768, 1366)
                RadRotator1.Items.Add(rotatoritem)
            Next
 
            Return True
 
        Catch ex As Exception
            Return False
        End Try
 
        Return False
    End Function
 
    '--Shrink images to fit screen, proportionately
    Public Function ScaleImage(ByVal OldImage As Image, ByVal TargetHeight As Integer, ByVal TargetWidth As Integer) As System.Drawing.Image
 
        Try
            Dim NewHeight As Integer = TargetHeight
            Dim NewWidth As Integer = NewHeight / OldImage.Height * OldImage.Width
 
            If NewWidth > TargetWidth Then
                NewWidth = TargetWidth
                NewHeight = NewWidth / OldImage.Width * OldImage.Height
            End If
 
            Return New Bitmap(OldImage, NewWidth, NewHeight)
 
        Catch ex As Exception
            Return Nothing
        End Try
 
        Return Nothing
    End Function
End Class
 
 
Public Class Randomizer(Of T)
    Implements IComparer(Of T)
 
    '--Insures different instances are sorted in different orders
    Private Shared Salter As New Random() '--Only as random as your seed
    Private Salt As Integer
    Public Sub New()
        Salt = Salter.Next(Integer.MinValue, Integer.MaxValue)
    End Sub
 
    Private Shared sha As New SHA1CryptoServiceProvider()
    Private Function HashNSalt(ByVal x As Integer) As Integer
        Dim b() As Byte = sha.ComputeHash(BitConverter.GetBytes(x))
        Dim r As Integer = 0
        For i As Integer = 0 To b.Length - 1 Step 4
            r = r Xor BitConverter.ToInt32(b, i)
        Next
 
        Return r Xor Salt
    End Function
 
    Public Function Compare(x As T, y As T) As Integer _
        Implements IComparer(Of T).Compare
 
        Return HashNSalt(x.GetHashCode()).CompareTo(HashNSalt(y.GetHashCode()))
    End Function
End Class

2 Answers, 1 is accepted

Sort by
0
Alex
Top achievements
Rank 1
answered on 15 Sep 2015, 05:51 PM
I'm also under the impression there's an easier built-in way to resize pictures proportionately but the way I'm doing it (manually) seems to still work
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 18 Sep 2015, 06:16 AM
Hello Alex,

Thank you for writing.
 
In order to center the image, you can set the RadImageItem.PositionOffset property. Thus, considering the screen bound and image size, you can adjust the image position. 

I hope this information helps. Should you have further questions I would be glad to help.
 
Regards,
Dess
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Rotator
Asked by
Alex
Top achievements
Rank 1
Answers by
Alex
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or