By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,685 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,685 IT Pros & Developers. It's quick & easy.

Why is the image in picturebox ugly?

P: n/a
yxq
Please add the manifest file!

************************************************** ***********
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Module Module1
Public Class Icon
'================================================= ==========================
==========
' Enumerations

'================================================= ==========================
==========
<Flags()> Private Enum SHGFI
SmallIcon = &H1
LargeIcon = &H0
Icon = &H100
DisplayName = &H200
Typename = &H400
SysIconIndex = &H4000
UseFileAttributes = &H10
End Enum

Public Enum IconSize
SmallIcon = 1
LargeIcon = 0
End Enum
'================================================= ==========================
==========
' Structures

'================================================= ==========================
==========
<StructLayout(LayoutKind.Sequential)> _
Private Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As Integer
<MarshalAs(UnmanagedType.LPStr, SizeConst:=260)> Public
szDisplayName As String
<MarshalAs(UnmanagedType.LPStr, SizeConst:=80)> Public
szTypeName As String

Public Sub New(ByVal B As Boolean)
hIcon = IntPtr.Zero
iIcon = 0
dwAttributes = 0
szDisplayName = vbNullString
szTypeName = vbNullString
End Sub
End Structure
'================================================= ==========================
==========
' API Calls

'================================================= ==========================
==========
Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal
pszPath As String,

ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal
cbFileInfo As Integer, ByVal

uFlagsn As SHGFI) As Integer
'================================================= ==========================
==========
' Functions and Procedures...

'================================================= ==========================
==========
Public Shared Function GetDefaultIcon(ByVal Path As String, Optional
ByVal IconSize As

IconSize = IconSize.SmallIcon, Optional ByVal SaveIconPath As String = "")
As Icon
Dim info As New SHFILEINFO(True)
Dim cbSizeInfo As Integer = Marshal.SizeOf(info)
Dim flags As SHGFI = SHGFI.Icon Or SHGFI.UseFileAttributes
flags = flags + IconSize
SHGetFileInfo(Path, 256, info, cbSizeInfo, flags)
GetDefaultIcon = Icon.FromHandle(info.hIcon)
If SaveIconPath <> "" Then
Dim FileStream As New IO.FileStream(SaveIconPath,
IO.FileMode.Create)
GetDefaultIcon.Save(FileStream)
FileStream.Close()
End If
End Function 'GetDefaultIcon(ByVal Path As String, Optional ByVal
IconSize As

IconSize = IconSize.SmallIcon, Optional ByVal SaveIconPath As String = "")
As Icon

'================================================= ==========================
==========
Public Shared Function ImageToIcon(ByVal SourceImage As Image) As
Icon
' converts an image into an icon
Dim TempBitmap As New Bitmap(SourceImage)
ImageToIcon = Icon.FromHandle(TempBitmap.GetHicon())
TempBitmap.Dispose()
End Function 'ImageToIcon(ByVal SourceImage As Image) As Icon

'================================================= ==========================
==========

End Class
Public Declare Unicode Function PickIconDlg _
Lib "shell32.dll" Alias "#62" ( _
ByVal hwndOwner As IntPtr, _
ByVal lpstrFile As System.Text.StringBuilder, _
ByVal nMaxFile As Integer, _
ByRef lpdwIconIndex As Integer) _
As Integer

Public Declare Function ExtractIcon _
Lib "shell32.dll" Alias "ExtractIconA" ( _
ByVal hInst As IntPtr, _
ByVal lpszExeFileName As String, _
ByVal nIconIndex As Integer) _
As IntPtr

Public Declare Function DestroyIcon _
Lib "user32.dll" (ByVal hIcon As Integer) _
As Integer
End Module
************************************************** **************************
*****

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles

MyBase.Load
Dim ObjLVI As ListViewItem
Dim arrItem(1) As String
Dim eachDrv As String
Dim ObjSize As New Size()
ObjSize.Height = 32
ObjSize.Width = 32

ImageList1.ImageSize = ObjSize
ImageList1.ColorDepth = ColorDepth.Depth32Bit
ListView1.LargeImageList = ImageList1

For Each eachDrv In System.Environment.GetLogicalDrives
arrItem(0) = eachDrv
ObjLVI = New ListViewItem(arrItem)
ImageList1.Images.Add(Icon.GetDefaultIcon(arrItem( 0),
Icon.IconSize.LargeIcon, ""))
ObjLVI.ImageIndex = ImageList1.Images.Count - 1

ListView1.Items.Add(ObjLVI)
Next
End Sub

Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As

System.EventArgs) Handles ListView1.SelectedIndexChanged
Try
PictureBox1.Image =
ImageList1.Images(ListView1.SelectedItems(0).Image Index)
Catch
End Try
End Sub
************************************************** **************************
**********

Thanks
Nov 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.