【问题标题】:CoverFlow in WPF how to include?CoverFlow 在 WPF 中如何包含?
【发布时间】:2013-01-20 21:01:17
【问题描述】:

我知道还有其他关于 WPF 中的 CoverFlow 效果的问题。我最喜欢的是Is there a good iTunes coverflow-type control for WPF?。

我下载了“第 7 部分”,它在 WPF 和 C# 中。

现在,我几乎从不使用第三方库,尤其是 GUI 库,而且我不知道如何在我的项目中使用该模板..

那么,基本上,我怎样才能将它正确地包含在我的项目中?在References中取了图书馆后我该怎么办?

如果您知道更好的 CoverFlow 模板(并且免费),您能告诉我哪个吗?

请帮忙(小提示,我的项目在 VB.NET 中,但我认为在 .dll 中没有任何关系)

【问题讨论】:

    标签: .net wpf windows vb.net coverflow


    【解决方案1】:

    您正在使用的示例CoverFlow 组件不能完全用作独立控件。有一个名为ThumbnailManager 的接口需要实现。我首先右键单击工具箱并选择选择项目,然后导航到包含Coverflow 组件Ded.Tutorial.Wpf.CoverFlow.Part7.FlowComponent.dll 的 .dll 文件并选择它,然后我能够删除它,我能够让它在 VB 中工作FlowControl 从工具箱进入我的主窗口。然后我将作者用来创建ThumbnailManager 的C# 代码转换为VB。

    ThumbnailManager.vb

    Imports System.IO.IsolatedStorage
    Imports System.IO
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports Ded.Tutorial.Wpf.CoverFlow.Part7.FlowComponent
    Imports Ded.Tutorial.Wpf.CoverFlow.Part7.FlowComponent.FlowControl
    
    Namespace Ded.Tutorial.Wpf.CoverFlow.Part7.FlowComponent
        Public Class ThumbnailManager : Implements IThumbnailManager
            Private ReadOnly store As IsolatedStorageFile
    
            Private Shared Function AmazonCut(myImage As Image) As Image
                If (myImage.Width <> myImage.Height) Then
                    Return myImage
                End If
                Dim bmp As Bitmap = New Bitmap(myImage)
                Dim size As Integer = myImage.Height
                Dim white As Integer = System.Drawing.Color.FromKnownColor(KnownColor.White).ToArgb()
                Dim i As Integer = 0
                While (i < size / 2)
                    If (Not bmp.GetPixel(i, i).ToArgb().Equals(white)) Then Exit While
                    If (Not bmp.GetPixel(i, size - 1 - i).ToArgb().Equals(white)) Then Exit While
                    If (Not bmp.GetPixel(size - 1 - i, i).ToArgb().Equals(white)) Then Exit While
                    If (Not bmp.GetPixel(size - 1 - i, size - 1 - i).ToArgb().Equals(white)) Then Exit While
                    i += 1
                End While
                If (i > 0) Then
                    i += 8
                    Dim zone As Rectangle = New Rectangle(i, i, size - 2 * 1, size - 2 * i)
                    Return bmp.Clone(zone, System.Drawing.Imaging.PixelFormat.DontCare)
                End If
                Return bmp
            End Function
    
            Private Function GetThumbnail(path As String) As Byte()
                Dim source As Image = Image.FromFile(path)
                source = AmazonCut(source)
                Dim height As Integer = source.Height
                Dim width As Integer = source.Width
                Dim factor As Integer = (height - 1) \ 250 + 1
                Dim smallHeight As Integer = height \ factor
                Dim smallWidth As Integer = width \ factor
                Dim thumb As Image = source.GetThumbnailImage(smallWidth, smallHeight, Nothing, IntPtr.Zero)
                Using ms As New MemoryStream
                    thumb.Save(ms, ImageFormat.Png)
                    ms.Flush()
                    ms.Seek(0, SeekOrigin.Begin)
                    Dim result(CInt(ms.Length)) As Byte
                    ms.Read(result, 0, CInt(ms.Length))
                    Return result
                End Using
    
            End Function
    
            Public Sub New()
                store = IsolatedStorageFile.GetUserStoreForAssembly
            End Sub
    
            Public Function GetThumbnail(host As String, filepath As String) As System.Windows.Media.ImageSource Implements IThumbnailManager.GetThumbnail
                Dim thumbName As String = Path.GetFileName(filepath)
                If (store.GetFileNames(thumbName).Length = 0) Then
                    Using Stream As New IsolatedStorageFileStream(thumbName, FileMode.CreateNew, store)
                        Dim data() As Byte = GetThumbnail(filepath)
                        Stream.Write(data, 0, data.Length)
                    End Using
                End If
                Using Stream As New IsolatedStorageFileStream(thumbName, FileMode.Open, store)
                    Dim myImage As BitmapImage = New BitmapImage()
                    myImage.BeginInit()
                    myImage.CacheOption = BitmapCacheOption.OnLoad
                    myImage.StreamSource = Stream
                    myImage.EndInit()
                    myImage.Freeze()
                    Return myImage
                End Using
            End Function
        End Class
    End Namespace
    

    然后,我在 MainWindow.xaml.vb 中添加了一个附加类和他正在使用的加载方法。在组件工作之后,我还必须将 MainWindow.g.vb 中的 Ded.Tutorial.Wpf.CoverFlow.Part7.FlowComponent.FlowControl 更改为 Global.Ded.Tutorial.Wpf.CoverFlow.Part7.FlowComponent.FlowControl。

    MainWindow.xaml.vb

    Imports System.IO
    Imports System.Drawing.Imaging
    Imports WpfApplication1.Ded.Tutorial.Wpf.CoverFlow.Part7
    
    
    Class MainWindow
    
        Public Sub New()
    
            ' This call is required by the designer.
            InitializeComponent()
    
            FlowControl1.Cache = New FlowComponent.ThumbnailManager
            Load("C:\Users\Marks-6520\Pictures\Alaska Trip")
            slider.Minimum = 0
            slider.Maximum = FlowControl1.Count - 1
        End Sub
    
        Public Sub Load(imagePath As String)
            Dim imageDir As DirectoryInfo = New DirectoryInfo(imagePath)
            Dim images As List(Of FileInfo) = New List(Of FileInfo)(imageDir.GetFiles("*.jpg"))
            images.Sort(New FileInfoComparer)
            For Each f As FileInfo In images
                FlowControl1.Add(Environment.MachineName, f.FullName)
            Next
        End Sub
    
    
        Private Sub slider_ValueChanged(sender As System.Object, e As System.Windows.RoutedPropertyChangedEventArgs(Of System.Double))
    
            FlowControl1.Index = Convert.ToInt32(slider.Value)
    
        End Sub
    End Class
    
    Public Class FileInfoComparer : Implements IComparer(Of FileInfo)
    
        Public Function Compare(x As System.IO.FileInfo, y As System.IO.FileInfo) As Integer Implements System.Collections.Generic.IComparer(Of System.IO.FileInfo).Compare
            Return String.Compare(x.FullName, y.FullName)
        End Function
    End Class
    

    【讨论】:

    • 我收到错误,例如“绘图不是系统的成员”“FromFile”不是 System.Windows.Controls.Image 的成员。您似乎使用的不是 WPF 框架代码,而是旧 Windows .Forms.
    • @user1714647 这是直接从您的封面流示例代码翻译而来的。您只需将其包含在您的参考文献中。
    • @user1714647 控件的原始作者使用 System.Drawing 命名空间来使用一些 GDI 函数。这就是它包含在代码中的原因。
    • 太好了,我取得了进展,但我现在有两个问题:Avira 将我的程序视为特洛伊木马 (lol),我收到错误“没有缩略图缓存”。当我运行程序时。我刚刚设置了一个包含 3 张图片的文件夹。
    • @user1714647 是在分配图像目录之前在 Forms 构造函数中分配缓存。它应该看起来像这样FlowControl1.Cache = New FlowComponent.ThumbnailManager
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多