2013-08-16 179 views
2

我有一個程序,顯示jpgs在小圖片盒中的預覽。這些圖像的加載速度很慢,每個圖像幾乎需要一秒鐘。這是因爲圖像很大(8/9 mB)。我需要快速加載它們,例如通過加載圖片的大拇指。我想避免把所有內容都放入內存中,因爲可能有數百張圖片。快速在picturebox中加載圖像

你對此有何建議?

THanks

+0

它可能很慢,因爲每個圖像正在被圖片框調整大小,並且調整大小是CPU密集型操作。正確大小的縮略圖無疑會解決問題。你是通過網絡獲取你的圖片嗎?如果是這樣,請讓您的服務器生成縮略圖。 – dandan78

+0

nope,它在本地驅動器上... – sharkyenergy

回答

1

您需要提前調整圖片大小。

創建縮略圖的速度同樣緩慢,因爲在開始製作縮略圖之前,您需要閱讀整個文件。

你可以做的就是,就像windows一樣,創建一個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外殼請求圖標;

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 
+0

是有道理的..有沒有甚至解決這個問題? – sharkyenergy

+0

謝謝,是否有一個Windows自動執行的命令?或讀出來?通過使用Windows標準我的意思是..如果我讓驅動器充滿縮略圖,用戶會殺了我.. – sharkyenergy

+0

最好的事情,當然,掛鉤到現有的系統。我知道windows創建一個tumb_db文件,其中包含該文件夾中每個圖像的tumbs。你可以嘗試找出一種方法來讀取窗口的tumb數據庫...你不必重新發明輪子:) – Lectere