Tag Archives: Picturebox

Extract the Associated Icon from a File Easily using VB 2008/2010

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (14 votes, average: 4.21 out of 5)
 

Microsoft added a new feature in Visual Basic 2005 (and is present in 08 as well) that will very easily get the icon associated with a specified file. In the past you would have to use the Windows APIs to extract a icon from a file. But now you can use a feature in Version 2.0 and higher of the DotNET Framework to perform this task which is what this little tip will show.

The function to use is ‘ExtractAssociatedIcon’ that is located under the System.Drawing.Icon namespace.

Public Shared Function ExtractAssociatedIcon(ByVal filePath As String) As System.Drawing.Icon

As you can see this is a very simple function. All you need to do is specify the path of the file whose icon you want to extract and handle the return value of the function which is the Icon associated with the file specified in the filePath parameter.


The code below will get the Icon associated with the file I specified and display the Icon in a Picturebox control.

picSource.Image = Drawing.Icon.ExtractAssociatedIcon(“pathAndFileToGetIcon”).ToBitmap()

Using the code above, i’m using a picturebox that I named picSource while converting the Icon to a Bitmap which can then be displayed easier in the picturebox image property. IF you want to save the icon image displayed in the picturebox control, the small piece of code below will do the trick.

picSource.Image.Save(“pathAndFileToSaveTo”)

Well, thats all there is to it I guess. Since MS added this ‘ExtractAssociatedIcon’ Function to the framework you no longer have to use the APIs to get similar results if your using Visual Basic .NET 2005 or version 2008. Have Fun!

Jason

Easy way to take a screenshot in VB 2008 and Visual Basic.NET 2010

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

The newer 2.0 version of the DotNET Framework added alot of new features. One of them is the ability to natively copy the visual contents of the screen. You can find this Sub() under the: System.Drawing.Graphics namespace named – ‘CopyFromScreen’. The ‘CopyFromScreen’ Sub has 4x overloads. You can specify the Destination coordinates to put the image and the Source coordinates to get the image. Also available is CopyPixelOperation which allows you to select the way you want to copy the pixels. System.Drawing.CopyPixelOperation.SourceCopy is the most common operation. Below is a simple demonstration to take a screenshot in your applications.

This basic sample only needs a picturebox.


'Copy the screen contents to the picturebox control. Select the X and Y positions to get the‘screens image,
'then select the X,Y positions to set the screen image to the picturebox control.
'You then simply specify how much of the image you want to copy. picScreen is the name of the picturebox I used.

picScreen.CreateGraphics.CopyFromScreen(0, 0, 0, 0, New Size(100, 100))

Draw a Line on a Picturebox Control in VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
 

The source code snip below will draw a line and be persisted onto a picturebox control using Visual Basic.NET and Higher including VB 2008 and 2010.

'Draws a line on picturebox control
'
'Put a picturebox on the form named pic
Dim bit As Bitmap = New Bitmap(pic.Width, pic.Height)

Dim g As Graphics = Graphics.FromImage(bit)

Dim myPen As Pen = New Pen(Color.Blue, 3)

g.DrawLine(myPen, 0, 0, pic.Width, pic.Height)

pic.Image = bit