472,100 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,100 software developers and data experts.

Why is the image in picturebox ugly?

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Alberto | last post: by
1 post views Thread by marco | last post: by
2 posts views Thread by marco | last post: by
6 posts views Thread by Patrick Dugan | last post: by
7 posts views Thread by lgbjr | last post: by
4 posts views Thread by John Swan | last post: by
4 posts views Thread by rodchar | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.