Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a label displaying the file name for the thumbnail image and when you run the code it displays correctly. However when the label with the file name changes how do I re-paint the thumbnail image with the new file name
my code:
Public Function ThumbnailCallback() As Boolean
        Return True
End Function
Private Sub GroupBox1_Paint(ByVal sender As Object, ByVal e As _ System.Windows.Forms.PaintEventArgs) Handles GroupBox1.Paint
        GetThumbnail(e)
End Sub
Private Sub GetThumbnail(ByVal e As PaintEventArgs)
    Dim callback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
    Dim image As Image = New Bitmap(label1.text)
    Dim pThumbnail As Image = image.GetThumbnailImage(200, 200, callback, New IntPtr())
    e.Graphics.DrawImage(pThumbnail, 20, 20, pThumbnail.Width, pThumbnail.Height)
End Sub
Private Sub lblCurrentFile_TextChanged(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles lblCurrentFile.TextChanged
        GetThumbnail(e)' this does not work it creates an exception error
        ' what code do I place here to get the thumbnail image to re-paint the new file name supplied by label1.text

End Sub

So, really I need someone to tell me what code needs to be in the label1_textChanged event to re-paint the thumbnail image with the new file name contained in labal1.text......?????

help...!
Posted
Updated 27-Mar-11 18:51pm
v2

You can't use EventArgs of Label as PaintEventArgs of GroupBox.
Try GroupBox1.Invalidate() in TextChanged which will cause the GroupBox to be painted.

Check if that helps.
 
Share this answer
 
v2
Comments
StefanoFranko 28-Mar-11 17:19pm    
Thankyou so much... it works perfectly... your a champion.
Stefano
Prerak Patel 28-Mar-11 23:53pm    
You are welcome.
This might be over complicated but here goes.

Declare
VB
Friend Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Integer, ByVal lpszExeFileName As String, ByVal nIconIndex As Integer) As Integer
Friend Declare Function ExtractIconEx Lib "shell32.dll" Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal nIconIndex As Int32, ByRef phiconLarge As Int32, ByRef phiconSmall As Int32, ByVal nIcons As Int32) As Int32
Friend Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Integer) As Integer

Now have this function.
Note OMReg is a Class that reads the Registry, but you get the idea.
VB
Friend Function FindIconA(ByVal Handle As Integer, ByVal Ext As String) As Integer
   Dim a, xName As String, i, r As Integer
   Try
      xName = OMReg.ReadKey(Ext, "", "", False, IniReader.RegKeys.ClassesRoot)
      a = OMReg.ReadKey(xName & "\defaulticon", "", "", False, IniReader.RegKeys.ClassesRoot)
      If a.StartsWith("%%") Then
         xName = OMReg.ReadKey(xName & "\defaulticon", "old", "", False, IniReader.RegKeys.ClassesRoot) '  get icon file
         i = InStr(xName, ",")
         If i = 0 Then i = 1 '  error
         r = CInt(xName.Substring(i))
      ElseIf xName.Substring(0, 1) = "%" Then
         r = CInt(a.Substring(1))
      Else
         xName = a
         i = InStr(xName, ",")
         If i = 0 Then i = 1
         r = CInt(xName.Substring(i))
      End If
      xName = xName.Substring(0, i - 1) & vbNullChar                                   '  Extract the associated icon
      i = ExtractIconEx(xName, -1, 0&, 0&, 0&)                                         '  returns total number of icons in file
      If r > i Then r = 1
      i = ExtractIcon(Handle, xName, r)                                                '  retrieves handle to icon
   Catch ex As Exception
      r = ExtractIcon(Handle, "Shell32.dll", 0)
   End Try
   Return i
End Function

Bear with me.
VB
Dim i As Integer = FindIconA(Handle, ".dwg")
Dim icn As Icon = Icon.FromHandle(New IntPtr(i))
Button.Image = icn.ToBitmap
DestroyIcon(i)

Nearly there.
Button has property
VB
Public Property Image() As Image
   Get
      Return gImg
   End Get
   Set(ByVal value As Image)
      gImg = value
      Dim e As New System.PaintEventArgs
      OnPaint(e)
      RaiseEvent ImageChanged(Me, e)
   End Set
End Property

and finally
VB
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    e.Graphics.DrawImage(gImg, 0, 0, Width, Width)
    MyBase.OnPaint(e)
End Sub


Of course I may have answered the wrong question.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900