快速加载图片框中的图像

时间:2013-08-16 08:25:28

标签: vb.net image picturebox performance

我有一个程序,可以在小图片框中显示jpgs的预览。这些图像的加载速度很慢,每个图像需要几秒钟。那是因为图像很大(8/9 mB)。我需要快速加载它们,例如加载图片的拇指。我想避免将所有内容放入内存,因为可能有数百张图片。

你对此有何建议?

感谢

1 个答案:

答案 0 :(得分:1)

您需要提前调整图片大小。

创建tumbnails同样会很慢,因为你需要先读取整个文件,然后再开始制作tumbnail。

你可以做的就是像窗户一样创建一个tumbnail'数据库',你可以在其中存储每张图片的tumbs。如果需要,只能使用全尺寸图片。

所以,如果你喜欢,

picture001.jpg
picture002.jpg
picture003.jpg

为每个人创建tumbs;

picture001.jpg
picture001_tumb.jpg
picture002.jpg
picture002_tumb.jpg
picture003.jpg
picture003_tumb.jpg

因此在加载picuters检测时,如果_tumb.jpg存在,如果没有创建并存储它。需要在后台工作人员中完成哪一项课程,因为您需要主应用程序才能做出响应......

您也可以使用此代码从Windows shell请求图标;

Imports System.Runtime.InteropServices
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports System.Text

' http://www.vbforums.com/showthread.php?617626-How-do-I-extract-a-256X256-image-out-of-an-icon&highlight=IShellItemImageFactory

Module GetShellIcon
    <Flags()> _
    Public Enum SIIGBF
        SIIGBF_RESIZETOFIT = 0
        SIIGBF_BIGGERSIZEOK = 1
        SIIGBF_MEMORYONLY = 2
        SIIGBF_ICONONLY = 4
        SIIGBF_THUMBNAILONLY = 8
        SIIGBF_INCACHEONLY = 16
    End Enum

    Public Enum SIGDN As UInteger
        NORMALDISPLAY = 0
        PARENTRELATIVEPARSING = &H80018001UI
        PARENTRELATIVEFORADDRESSBAR = &H8001C001UI
        DESKTOPABSOLUTEPARSING = &H80028000UI
        PARENTRELATIVEEDITING = &H80031001UI
        DESKTOPABSOLUTEEDITING = &H8004C000UI
        FILESYSPATH = &H80058000UI
        URL = &H80068000UI
    End Enum

    <ComImportAttribute(), GuidAttribute("bcc18b79-ba16-442f-80c4-8a59c30c463b"),     InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
    Public Interface IShellItemImageFactory
        Sub GetImage(ByVal size As SIZE, ByVal flags As SIIGBF, ByRef phbm As IntPtr)
    End Interface

    <ComImport()> <Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")>     <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
    Public Interface IShellItem
        Sub BindToHandler(ByVal pbc As IntPtr, <MarshalAs(UnmanagedType.LPStruct)>     ByVal bhid As Guid, <MarshalAs(UnmanagedType.LPStruct)> ByVal riid As Guid, ByRef ppv As     IntPtr)
        Sub GetParent(ByRef ppsi As IShellItem)
        Sub GetDisplayName(ByVal sigdnName As SIGDN, ByRef ppszName As IntPtr)
        Sub GetAttributes(ByVal sfgaoMask As UInt32, ByRef psfgaoAttribs As UInt32)
        Sub Compare(ByVal psi As IShellItem, ByVal hint As UInt32, ByRef piOrder As     Integer)
    End Interface

    <DllImport("shell32.dll", CharSet:=CharSet.Unicode, PreserveSig:=False)> _
    Public Sub SHCreateItemFromParsingName(<MarshalAs(UnmanagedType.LPWStr)> ByVal     pszPath As String, ByVal pbc As IntPtr, <MarshalAs(UnmanagedType.LPStruct)> ByVal riid As     Guid, <MarshalAs(UnmanagedType.Interface, IidParameterIndex:=2)> ByRef ppv As IShellItem)
    End Sub

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure SIZE
        Public cx As Integer
        Public cy As Integer
        Public Sub New(ByVal cx As Integer, ByVal cy As Integer)
            Me.cx = cx
            Me.cy = cy
        End Sub
    End Structure

    Public Function GetShellIcon(ByVal Path As String, MySIIGBF As SIIGBF, Optional ByVal Width As Integer = 256, Optional ByVal Height As Integer = 256) As Bitmap

        Dim ppsi As IShellItem = Nothing
        Dim hbitmap As IntPtr = IntPtr.Zero
        Dim uuid As New Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")

        Dim bs As Bitmap

        SHCreateItemFromParsingName(Path, IntPtr.Zero, uuid, ppsi)
        DirectCast(ppsi, IShellItemImageFactory).GetImage(New SIZE(Width, Height), MySIIGBF, hbitmap)
        bs = System.Drawing.Bitmap.FromHbitmap(hbitmap)
        bs.MakeTransparent(Color.Black)

        Return bs

    End Function

End Module