[WPSL] [C#]如何旋转ListBox的项目 [英] [WPSL][C#]How to rotate the items of a ListBox

查看:110
本文介绍了[WPSL] [C#]如何旋转ListBox的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我将图像绑定到列表框,在OnOrientationChange事件中我想旋转图像以直立显示它。这是列表框和代码的XAML ...

Hi. I'm binding images to a listbox and in OnOrientationChange event I want to rotate the images to display it upright. Here's the XAML of the Listbox and Code...

<!--My Friends ListBox-->
                <ListBox Visibility="Collapsed" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Auto"  x:Name="MyFriendsList" VerticalAlignment="Top" Height="120" Grid.Row="0" Grid.ColumnSpan="3" Grid.Column="0">
            
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel VerticalAlignment="Center" Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Image Source="{Binding}" Stretch = "Uniform" Margin="5,5,5,5"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>





protected override void OnOrientationChanged(OrientationChangedEventArgs e)
        {
            //get friends
            this.Loaded += async (s, e) =>
            {
                var images = await GetImages();
                MyFriendsList.ItemsSource = images;
            };
        }

private async System.Threading.Tasks.Task<ObservableCollection<ImageSource>> GetImages()
        {
            ObservableCollection<ImageSource> images = new ObservableCollection<ImageSource>();
            Windows.Storage.StorageFolder folder;
            IReadOnlyList<StorageFile> files = null;
            StorageFile myphoto = null;

            try
            {
                IsolatedStorageFile.GetUserStoreForApplication().CreateDirectory("myfriends");
                folder = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFolderAsync("myfriends");
                files = await folder.GetFilesAsync();
            }
            catch (Exception ex)
            {
                this.Dispatcher.BeginInvoke(delegate()
                {
                    txtDebug.Text = "No friends to display.\n" + ex.Message;
                });
            }

            if (files.Count < 1 || files == null)
            {
                //go back, no friends yet
                return images;
            }
            foreach (var file in files)
            {
                if (file.Name.EndsWith(".jpg"))
                {
                    BitmapImage bimage = new BitmapImage();
                    bimage.SetSource(await file.OpenStreamForReadAsync());
                    images.Add(bimage);
                }
            }
            return images;
        }





 private Stream RotateStream(Stream stream, int angle)
        {
            stream.Position = 0;
            if (angle % 90 != 0 || angle < 0) throw new ArgumentException();
            if (angle % 360 == 0) return stream;

            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(stream);
            WriteableBitmap wbSource = new WriteableBitmap(bitmap);

            WriteableBitmap wbTarget = null;
            if (angle % 180 == 0)
            {
                wbTarget = new WriteableBitmap(wbSource.PixelWidth, wbSource.PixelHeight);
            }
            else
            {
                wbTarget = new WriteableBitmap(wbSource.PixelHeight, wbSource.PixelWidth);
            }

            for (int x = 0; x < wbSource.PixelWidth; x++)
            {
                for (int y = 0; y < wbSource.PixelHeight; y++)
                {
                    switch (angle % 360)
                    {
                        case 90:
                            wbTarget.Pixels[(wbSource.PixelHeight - y - 1) + x * wbTarget.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth];
                            break;
                        case 180:
                            wbTarget.Pixels[(wbSource.PixelWidth - x - 1) + (wbSource.PixelHeight - y - 1) * wbSource.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth];
                            break;
                        case 270:
                            wbTarget.Pixels[y + (wbSource.PixelWidth - x - 1) * wbTarget.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth];
                            break;
                    }
                }
            }
            MemoryStream targetStream = new MemoryStream();
            wbTarget.SaveJpeg(targetStream, wbTarget.PixelWidth, wbTarget.PixelHeight, 0, 100);
            return targetStream;
        


每次设备旋转时旋转图像的简单方法是什么?先感谢您。 ps你能否指出我能做些什么更简单。

What's a simple way to rotate the images everytime the device rotates? Thank you in advance. ps Can you also point out if there is something I can do simpler.

推荐答案

欢迎来到开发环球Windows应用论坛! 

请阅读粘贴帖子,尤其是  发布指南:主题行标签   已知的
Windows 10 SDK和工具的问题 

Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools 

我不清楚你到底想要实现的目标,但是你呢?应用一个可能会好得多旋转变换而不是旋转像素缓冲区。

It's not clear to me exactly what you're trying to achieve here, but you'll probably be much better off applying a rotation transform rather than rotating the pixel buffer.


这篇关于[WPSL] [C#]如何旋转ListBox的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆